> ## 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.

# Write box file

> Creates or overwrites a file. Creates necessary directories in the path if they don't exist. If the target path already exists, the write will fail.



## OpenAPI

````yaml post /boxes/{boxId}/fs/write
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/write:
    post:
      tags:
        - File System
      summary: Write box file
      description: >-
        Creates or overwrites a file. Creates necessary directories in the path
        if they don't exist. If the target path already exists, the write will
        fail.
      operationId: FileSystemController_writeFile
      parameters:
        - name: boxId
          required: true
          in: path
          description: Box ID
          schema:
            example: c9bdc193-b54b-4ddb-a035-5ac0c598d32d
            type: string
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              oneOf:
                - $ref: '#/components/schemas/WriteFile'
                - $ref: '#/components/schemas/WriteFileByBinary'
      responses:
        '200':
          description: Write file
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/File'
        '409':
          description: Path is already a directory
      security:
        - bearer: []
      x-codeSamples:
        - lang: JavaScript
          source: |-
            import GboxSDK 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" });

              // Write a text file
              const newFile = await box.fs.write({
                path: "/home/user/example.txt",
                content: "Hello, World!\nThis is a test file created with GboxSDK.",
              });

              console.log(`Write result: ${JSON.stringify(newFile.data, null, 2)}`);

              // Create a JSON configuration file
              const config = {
                name: "My Application",
                version: "1.0.0",
                settings: {
                  debug: true,
                  port: 3000,
                },
              };

              await box.fs.write({
                path: "/home/user/config.json",
                content: JSON.stringify(config, null, 2),
              });

              console.log("Configuration file created successfully");
            }

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

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

                box = gbox_sdk.create(type="linux")

                # Write a text file
                new_file = box.fs.write(
                    path="/home/user/example.txt",
                    content="Hello, World!\nThis is a test file created with GboxSDK."
                )

                print(f"Write result:")
                print(f"  File: {new_file.data.name}")
                print(f"  Path: {new_file.data.path}")
                print(f"  Size: {new_file.data.size}")
                print(f"  Mode: {new_file.data.mode}")
                print(f"  Last Modified: {new_file.data.last_modified}")

                # Create a JSON configuration file
                config = {
                    "name": "My Application",
                    "version": "1.0.0",
                    "settings": {
                        "debug": True,
                        "port": 3000
                    }
                }

                box.fs.write(
                    path="/home/user/config.json",
                    content=json.dumps(config, indent=2)
                )

                print("Configuration file created successfully")

            if __name__ == "__main__":
                main()
        - lang: Go
          source: "package main\n\nimport (\n\t\"context\"\n\t\"encoding/json\"\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// Write a text file\n\tnewFile, err := box.Fs.Write(context.Background(), &gbox.WriteFileRequest{\n\t\tPath:    \"/home/user/example.txt\",\n\t\tContent: \"Hello, World!\\nThis is a test file created with GboxSDK.\",\n\t})\n\tif err != nil {\n\t\tlog.Fatalf(\"Failed to write file: %v\", err)\n\t}\n\n\tnewFileJSON, _ := json.MarshalIndent(newFile.Data, \"\", \"  \")\n\tfmt.Printf(\"Write result: %s\\n\", string(newFileJSON))\n\n\t// Create a JSON configuration file\n\tconfig := map[string]interface{}{\n\t\t\"name\":    \"My Application\",\n\t\t\"version\": \"1.0.0\",\n\t\t\"settings\": map[string]interface{}{\n\t\t\t\"debug\": true,\n\t\t\t\"port\":  3000,\n\t\t},\n\t}\n\n\tconfigJSON, err := json.MarshalIndent(config, \"\", \"  \")\n\tif err != nil {\n\t\tlog.Fatalf(\"Failed to marshal config: %v\", err)\n\t}\n\n\t_, err = box.Fs.Write(context.Background(), &gbox.WriteFileRequest{\n\t\tPath:    \"/home/user/config.json\",\n\t\tContent: string(configJSON),\n\t})\n\tif err != nil {\n\t\tlog.Fatalf(\"Failed to write config file: %v\", err)\n\t}\n\n\tfmt.Println(\"Configuration file created successfully\")\n}"
components:
  schemas:
    WriteFile:
      type: object
      properties:
        workingDir:
          type: string
          description: >-
            Working directory. If not provided, the file will be read from the
            `box.config.workingDir` directory.
          title: FileSystemBase
          example: /home/user/documents
        path:
          type: string
          description: >-
            Target path in the box. If the path does not start with '/', the
            file will be written relative to the working directory. Creates
            necessary directories in the path if they don't exist. If the target
            path already exists, the write will fail.
          title: WriteFile
          example: /home/user/documents/output.txt
        content:
          type: string
          description: 'Content of the file (Max size: 512MB)'
          title: WriteFile
          example: |-
            Hello, World!
            This is file content.
      title: Write File
      description: Request parameters for writing content to a file
      required:
        - path
        - content
    WriteFileByBinary:
      type: object
      properties:
        workingDir:
          type: string
          description: >-
            Working directory. If not provided, the file will be read from the
            `box.config.workingDir` directory.
          title: FileSystemBase
          example: /home/user/documents
        path:
          type: string
          description: >-
            Target path in the box. If the path does not start with '/', the
            file will be written relative to the working directory. Creates
            necessary directories in the path if they don't exist. If the target
            path already exists, the write will fail.
          title: WriteFileByBinary
          example: /home/user/documents/output.txt
        content:
          type: string
          description: 'Binary content of the file (Max file size: 512MB)'
          format: binary
          example: Select file
      title: Write File By Binary
      description: Request parameters for writing binary content to a file
      required:
        - path
        - content
    File:
      type: object
      properties:
        type:
          type: string
          enum:
            - file
          description: File type indicator
          title: File
        name:
          type: string
          description: Name of the file
          title: File
          example: example.txt
        path:
          type: string
          description: Full path to the file in the box
          title: File
          example: /path/to/example.txt
        size:
          type: string
          description: Size of the file
          title: File
          example: 10MB
        mode:
          type: string
          description: File metadata
          title: File
          example: '755'
        lastModified:
          format: date-time
          type: string
          description: Last modified time of the file
          title: File
          example: '2021-01-01T00:00:00Z'
      title: File
      description: File system file representation
      required:
        - type
        - name
        - path
        - size
        - mode
        - lastModified
  securitySchemes:
    bearer:
      scheme: bearer
      bearerFormat: JWT
      type: http
      description: >-
        Enter your API Key in the format: Bearer <token>. Get it from
        https://gbox.ai

````