> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gbox.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Read box file

> Reads the contents of a file inside the box and returns it as a string. Supports absolute or relative paths, with `workingDir` as the base for relative paths.



## OpenAPI

````yaml get /boxes/{boxId}/fs/read
openapi: 3.0.0
info:
  title: GBOX Open API
  description: GBOX Open API Documentation
  version: '1.0'
  contact: {}
servers:
  - url: https://gbox.ai/api/v1
    description: Production Server
security: []
tags: []
paths:
  /boxes/{boxId}/fs/read:
    get:
      tags:
        - File System
      summary: Read box file
      description: >-
        Reads the contents of a file inside the box and returns it as a string.
        Supports absolute or relative paths, with `workingDir` as the base for
        relative paths.
      operationId: FileSystemController_readFile
      parameters:
        - name: boxId
          required: true
          in: path
          description: Box ID
          schema:
            example: c9bdc193-b54b-4ddb-a035-5ac0c598d32d
            type: string
        - name: workingDir
          required: false
          in: query
          description: >-
            Working directory. If not provided, the file will be read from the
            `box.config.workingDir` directory.
          schema:
            title: FileSystemBase
            example: /home/user/documents
            type: string
        - name: path
          required: true
          in: query
          description: >-
            Target path in the box. If the path does not start with '/', the
            file will be read from the working directory.
          schema:
            title: ReadFile
            example: /home/user/documents/config.json
            type: string
      responses:
        '200':
          description: Read file
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ReadFileResult'
        '404':
          description: File/dir not found
        '405':
          description: Is a dir
      security:
        - bearer: []
      x-codeSamples:
        - lang: JavaScript
          source: |-
            import GboxSDK, { isFileOperator } from "gbox-sdk";

            const gboxSDK = new GboxSDK({
              apiKey: process.env["GBOX_API_KEY"] // This is the default and can be omitted
            });

            async function main() {
              const box = await gboxSDK.create({ type: "linux" });
              // prepare: write a file
              await box.fs.write({
                path: "/tmp/example.txt",
                content: "Hello, world!",
              });

              // example: read a file/directory
              const file = await box.fs.get({
                path: "/tmp/example.txt",
              });

              if (isFileOperator(file)) {
                const { content } = await file.read();
                console.info(`📄 ${file.data.name} (${file.data.size}) - ${file.data.path}`);
                console.info(`content: ${content}`);
              } else {
                console.info(`📁 ${file.data.name} - ${file.data.path} is a directory`);
              }
            }

            main();
        - lang: Python
          source: |-
            import os
            import json
            from gbox_sdk import GboxSDK
            from gbox_sdk.utils import is_file_operator

            def main():
                gbox_sdk = GboxSDK(api_key=os.environ["GBOX_API_KEY"])

                box = gbox_sdk.create(type="linux")
                # prepare: write a file
                box.fs.write(
                    path="/tmp/example.txt",
                    content="Hello, world!"
                )

                # example: read a file/directory
                file = box.fs.get(
                    path="/tmp/example.txt"
                )

                if is_file_operator(file):
                    content = file.read()
                    print(f"📄 {file.data.name} ({file.data.size}) - {file.data.path}")
                    print(f"content: {content}")
                else:
                    print(f"📁 {file.data.name} - {file.data.path} is a directory")

            if __name__ == "__main__":
                main()
        - lang: Go
          source: "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n\n\t\"github.com/gbox/gbox-sdk-go\"\n)\n\nfunc main() {\n\tgboxSDK := gbox.NewGboxSDK(os.Getenv(\"GBOX_API_KEY\"))\n\n\tbox, err := gboxSDK.Create(context.Background(), gbox.CreateOptions{Type: \"linux\"})\n\tif err != nil {\n\t\tlog.Fatalf(\"Failed to create linux box: %v\", err)\n\t}\n\n\t// prepare: write a file\n\t_, err = box.Fs.Write(context.Background(), &gbox.WriteFileRequest{\n\t\tPath:    \"/tmp/example.txt\",\n\t\tContent: \"Hello, world!\",\n\t})\n\tif err != nil {\n\t\tlog.Fatalf(\"Failed to write file: %v\", err)\n\t}\n\n\t// example: read a file/directory\n\tfile, err := box.Fs.Get(context.Background(), &gbox.GetFileRequest{\n\t\tPath: \"/tmp/example.txt\",\n\t})\n\tif err != nil {\n\t\tlog.Fatalf(\"Failed to get file: %v\", err)\n\t}\n\n\tif gbox.IsFileOperator(file) {\n\t\tcontent, err := file.Read(context.Background())\n\t\tif err != nil {\n\t\t\tlog.Fatalf(\"Failed to read file: %v\", err)\n\t\t}\n\t\tfmt.Printf(\"📄 %s (%s) - %s\\n\", file.Data().Name, file.Data().Size, file.Data().Path)\n\t\tfmt.Printf(\"content: %s\\n\", content)\n\t} else {\n\t\tfmt.Printf(\"📁 %s - %s is a directory\\n\", file.Data().Name, file.Data().Path)\n\t}\n}"
components:
  schemas:
    ReadFileResult:
      type: object
      properties:
        content:
          type: string
          description: Content of the file
          title: ReadFileResult
          example: |-
            {
              "name": "My Project",
              "version": "1.0.0"
            }
      title: Read File Result
      description: Response containing file content
      required:
        - content
  securitySchemes:
    bearer:
      scheme: bearer
      bearerFormat: JWT
      type: http
      description: >-
        Enter your API Key in the format: Bearer <token>. Get it from
        https://gbox.ai

````