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

# Workspace: File Edit

> Edit a file in the agent's workspace by replacing an exact text occurrence

Edit a file in the per-agent workspace by replacing an exact text occurrence. `old_text` must match the existing file content exactly, including whitespace and indentation.

## Path Parameters

<ParamField path="agent_id" type="string" required>
  Agent ID (UUID)
</ParamField>

## Request Body

<ParamField body="path" type="string" required>
  Path to the file to edit, relative to the workspace root.
</ParamField>

<ParamField body="old_text" type="string" required>
  Exact text to find and replace. Must match existing file content, including whitespace.
</ParamField>

<ParamField body="new_text" type="string" required>
  Replacement text. Use an empty string to delete the matched content.
</ParamField>

## Response

<ResponseField name="status" type="string">
  Result status (e.g., `ok`).
</ResponseField>

<ResponseField name="path" type="string">
  Path of the file that was edited.
</ResponseField>

## Example Request

```bash theme={"dark"}
curl -X POST "https://api.xpander.ai/v1/agents/<agent-id>/workspace/file_edit" \
  -H "Content-Type: application/json" \
  -H "x-api-key: <your-api-key>" \
  -d '{
    "path": "app.py",
    "old_text": "DEBUG = True",
    "new_text": "DEBUG = False"
  }'
```

## Notes

* The match is exact and case-sensitive. If `old_text` appears more than once in the file, the request fails — include additional surrounding context in `old_text` to make the match unique.
* To apply several edits atomically, use [Workspace: Multi Edit](/api-reference/v1/agents/workspace/multi-edit) instead.

## See Also

* [Workspace: Multi Edit](/api-reference/v1/agents/workspace/multi-edit)
* [Workspace: File Write](/api-reference/v1/agents/workspace/file-write)
* [Workspace: File Read](/api-reference/v1/agents/workspace/file-read)


## OpenAPI

````yaml POST /v1/agents/{agent_id}/workspace/file_edit
openapi: 3.1.0
info:
  title: xpander.ai API Service
  description: |2-

        The xpander.ai API Service provides a unified REST API for managing AI agents,
        executing tasks, managing knowledge bases, and integrating with external systems.
        
        Features:
        - Agent Management: Create, update, deploy, and delete AI agents
        - Task Execution: Invoke agents with support for sync, async, and streaming modes
        - Knowledge Bases: Manage knowledge bases and documents for RAG workflows
        - Tools: Discover, connect, and attach tools (connectors, custom functions, MCP servers, sub-agents, workflows) to agents and workflows
        - MCP Integration: Model Context Protocol support for standardized AI interactions
        
        Authentication: All endpoints require authentication via either an API key (`x-api-key`) or an OAuth2 JWT (`Authorization: Bearer <jwt>`).
        
  version: '0.001'
servers:
  - url: https://api.xpander.ai
security: []
paths:
  /v1/agents/{agent_id}/workspace/file_edit:
    post:
      tags:
        - API v1
        - Agents
        - Agents Workspace
      summary: 'Workspace: File Edit'
      description: >-
        Edit a file in the agent's workspace by replacing exact text
        occurrences.
      operationId: Workspace__File_Edit_v1_agents__agent_id__workspace_file_edit_post
      parameters:
        - name: agent_id
          in: path
          required: true
          schema:
            type: string
            title: Agent Id
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/FileEditRequest'
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/FileEditResponse'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
      security:
        - APIKeyHeader: []
components:
  schemas:
    FileEditRequest:
      properties:
        path:
          type: string
          title: Path
          description: Relative path to the file to edit.
          examples:
            - app.py
        old_text:
          type: string
          title: Old Text
          description: >-
            The exact text to find and replace. Must match the file content
            exactly (whitespace-sensitive).
          examples:
            - DEBUG = True
        new_text:
          type: string
          title: New Text
          description: The replacement text that will substitute old_text.
          examples:
            - DEBUG = False
      type: object
      required:
        - path
        - old_text
        - new_text
      title: FileEditRequest
    FileEditResponse:
      properties:
        path:
          type: string
          title: Path
          description: The path of the edited file
        replacements_made:
          type: integer
          title: Replacements Made
          description: Number of replacements made
          default: 0
          examples:
            - 1
      type: object
      required:
        - path
      title: FileEditResponse
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError
  securitySchemes:
    APIKeyHeader:
      type: apiKey
      description: API Key for authentication
      in: header
      name: x-api-key

````