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

# Edit a file

> Replace an exact snippet in an existing file.

Replaces `oldText` with `newText` in a file.

<Warning>
  `files/edit` replaces `oldText` where it occurs **exactly once**. If it matches zero or more than one
  place, the call returns `BAD_REQUEST` — include more surrounding context so the match is unique. Use
  [Write a file](/build-api/sessions/write-file) for new files or full rewrites.
</Warning>


## OpenAPI

````yaml build-api/openapi.json POST /sessions/{sessionId}/files/edit
openapi: 3.0.1
info:
  title: Zite Build API
  description: >-
    Create, build, and deploy Zite apps programmatically — the REST equivalent
    of the Zite MCP build tools. Use it from a script, a CI pipeline, or your
    own agent.


    ## Authentication


    All requests require a Bearer API key from your Developer settings:

    ```

    Authorization: Bearer YOUR_API_KEY

    ```

    The key acts as you — it can only see and change workspaces you have access
    to, and every change is attributed to you.


    ## Base URL


    https://api.zite.com/v1


    ## Errors


    Errors return `{ "statusCode": 404, "error": "Not Found", "message":
    "Workspace not found" }`. Notable statuses: `401` (bad or missing key),
    `402` (the build tools aren't in your plan), `404`, `409` (a commit push
    conflict — pull, rebase, and commit again), and `400` for validation
    failures and publishing before a build is green.
  version: 1.0.0
servers:
  - url: https://api.zite.com/v1
    description: Zite Build API
security:
  - bearerAuth: []
tags:
  - name: Workspaces
    description: Create workspaces and inspect the apps in them.
  - name: Build sessions
    description: >-
      Open an isolated checkout of a workspace, create apps, edit files, check,
      and commit.
  - name: Deployments
    description: Publish built apps, manage versions, roll back, and read runtime logs.
paths:
  /sessions/{sessionId}/files/edit:
    post:
      tags:
        - Build sessions
      summary: Edit a file
      description: >-
        Replaces `oldText` with `newText` where it occurs exactly once. If
        `oldText` matches zero or more than one place, the call returns
        `BAD_REQUEST` — include more surrounding context so the match is unique.
        Use `PUT /sessions/{sessionId}/files` for new files or full rewrites.
      operationId: editFile
      parameters:
        - $ref: '#/components/parameters/SessionId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - path
                - oldText
                - newText
              properties:
                path:
                  $ref: '#/components/schemas/FilePath'
                oldText:
                  type: string
                  description: Text to replace. Must match exactly once in the file.
                newText:
                  type: string
                  description: Replacement text.
            example:
              path: apps/agent-console/src/App.tsx
              oldText: <h1>Old</h1>
              newText: <h1>New</h1>
      responses:
        '200':
          description: The edited file
          content:
            application/json:
              schema:
                type: object
                properties:
                  path:
                    type: string
                  replaced:
                    type: boolean
              example:
                path: apps/agent-console/src/App.tsx
                replaced: 1
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
components:
  parameters:
    SessionId:
      name: sessionId
      in: path
      required: true
      description: The build session ID from Open a session.
      schema:
        type: string
      example: ses_9f2a1c7b40e8
  schemas:
    FilePath:
      type: string
      description: Path relative to the workspace root.
      example: apps/agent-console/src/api/listTickets.ts
    Error:
      type: object
      properties:
        statusCode:
          type: integer
          example: 404
        error:
          type: string
          example: Not Found
        message:
          type: string
          example: Workspace not found
  responses:
    BadRequest:
      description: The request was malformed.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: BAD_REQUEST
              message: >-
                oldText matched 3 times; include more surrounding context so the
                match is unique.
    Unauthorized:
      description: Missing or invalid API key.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: UNAUTHORIZED
              message: Missing or invalid API key.
    NotFound:
      description: The resource does not exist or your key cannot access it.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: NOT_FOUND
              message: Workspace not found.
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: 'Enter your Zite API key. Format: Bearer <api_key>'

````