# `PUT /v1/workspace/apps/{appId}/file` — Write a file

Part of the [Bkper Managed Agent API](https://bkper.com/docs/api/managed-agent.md). Read its guide for authentication, conventions, and examples.

Write the whole new text of one file into the working copy, over the version read (`previous`), never forced. 409 file_changed when it changed since, such as by the agent; 422 file_invalid (with reason) when it cannot be written: a path, not text, too large. In context a draft agents would not load yet is written: the listing warns of it, and Save refuses it. Nothing is published until Save; the agent sees it at once. 409 workspace_busy while Git works in the repository (the agent's, or a sync): nothing changed, try again. For the App's owner and developers (403 workspace_forbidden), when Bkper hosts its source: its folder in /workspace/apps. Saving never deploys.

## Operation contract

```json
{
  "operationId": "putAppSourceFile",
  "tags": [
    "Workspace"
  ],
  "summary": "Write a file",
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "parameters": [
    {
      "name": "appId",
      "in": "path",
      "required": true,
      "schema": {
        "type": "string",
        "pattern": "^[A-Za-z0-9_-]{1,64}$"
      }
    }
  ],
  "description": "Write the whole new text of one file into the working copy, over the version read (`previous`), never forced. 409 file_changed when it changed since, such as by the agent; 422 file_invalid (with reason) when it cannot be written: a path, not text, too large. In context a draft agents would not load yet is written: the listing warns of it, and Save refuses it. Nothing is published until Save; the agent sees it at once. 409 workspace_busy while Git works in the repository (the agent's, or a sync): nothing changed, try again. For the App's owner and developers (403 workspace_forbidden), when Bkper hosts its source: its folder in /workspace/apps. Saving never deploys.",
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "$ref": "#/components/schemas/PutWorkspaceFile"
        }
      }
    }
  },
  "responses": {
    "200": {
      "description": "Success",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/WorkspaceWritten"
          }
        }
      }
    },
    "400": {
      "description": "Rejected; never contains credentials or upstream payloads.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "401": {
      "description": "Rejected; never contains credentials or upstream payloads.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "403": {
      "description": "Rejected; never contains credentials or upstream payloads.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "404": {
      "description": "Rejected; never contains credentials or upstream payloads.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "409": {
      "description": "Rejected; never contains credentials or upstream payloads.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "410": {
      "description": "Rejected; never contains credentials or upstream payloads.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "411": {
      "description": "Rejected; never contains credentials or upstream payloads.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "413": {
      "description": "Rejected; never contains credentials or upstream payloads.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "422": {
      "description": "Rejected; never contains credentials or upstream payloads.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "503": {
      "description": "Rejected; never contains credentials or upstream payloads.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  }
}
```

## Schemas

### PutWorkspaceFile

```json
{
  "type": "object",
  "properties": {
    "path": {
      "type": "string",
      "minLength": 1,
      "maxLength": 512,
      "description": "A repository-relative file path, such as AGENTS.md or .agents/skills/month-end-close/SKILL.md."
    },
    "content": {
      "type": "string",
      "maxLength": 262144,
      "description": "The whole new text."
    },
    "previous": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^[0-9a-f]{40}$"
        },
        {
          "type": "null"
        }
      ],
      "description": "The `sha` read (or written last), or null for a new file. If the file changed since, such as by the agent, 409 file_changed: read it again. Never forced."
    }
  },
  "required": [
    "path",
    "content",
    "previous"
  ],
  "additionalProperties": false
}
```

### WorkspaceWritten

```json
{
  "type": "object",
  "properties": {
    "path": {
      "type": "string"
    },
    "size": {
      "type": "number",
      "description": "Bytes."
    },
    "sha": {
      "type": "string",
      "description": "Its new Git blob ID: the next write's `previous`."
    }
  },
  "required": [
    "path",
    "size",
    "sha"
  ],
  "description": "A file written into the working copy: not published until Save."
}
```

### Error

```json
{
  "type": "object",
  "properties": {
    "error": {
      "type": "object",
      "properties": {
        "code": {
          "type": "string",
          "description": "Stable code, e.g. not_found, idempotency_conflict, session_working, usage_limit_exceeded. Open."
        },
        "type": {
          "description": "Category, e.g. invalid_request_error, authentication_error, usage_limit_error. Open.",
          "type": "string"
        },
        "message": {
          "type": "string"
        },
        "host": {
          "description": "host_in_use: the host another service holds.",
          "type": "string"
        },
        "service": {
          "description": "host_in_use: the id of the service that holds the host.",
          "type": "string"
        },
        "reason": {
          "description": "file_invalid: why the change, or a picture, was refused. Open: path | text | size | skill | skills | count | type.",
          "type": "string"
        },
        "paths": {
          "description": "file_exists from an add: every path of the request where a file is and `replace` was not set, all at once, so the person can be asked once. publish_conflict: the files someone published changes to the same lines of.",
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "publishers": {
          "description": "publish_conflict: who published those changes, by name.",
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "required": [
        "code"
      ]
    }
  },
  "required": [
    "error"
  ]
}
```

## Authentication

```json
{
  "bearerAuth": {
    "type": "http",
    "scheme": "bearer"
  }
}
```
