# Write, patch or delete one file in the app's sandbox.

POST https://services.leadconnectorhq.com/ai-builder-api/cli/projects/{projectId}/sandbox/edit

Writes, patches or deletes one file in the app's sandbox. A small change to a text file is a `patch`: exact replacements, without resending the file. After a set of edits, save them with `POST .../sandbox/checkpoint`, then start a run with `externalAgent: true` to check, build and deploy them. A React Native app (`react_native_app_kernel_v1`) writes only under `apps/mobile/app-extensions/` and `apps/mobile/application-assets/`; its `routes.ts` is generated, so register its screens with `POST .../sandbox/extension-routes` instead of writing that file.

## Authentication

Send `Authorization: Bearer <token>` using:

- **Sub-Account token: OAuth access token or Private Integration Token.** Scopes: `clientportal-ai.write`

## Parameters

### Header Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `Version` | string | Yes | The API version. |

### Path Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `projectId` | string | Yes | Project identifier. |

## OpenAPI Specification

```json
{
  "openapi": "3.0.0",
  "info": {
    "title": "LeadConnector API",
    "version": "v3"
  },
  "servers": [
    {
      "url": "https://services.leadconnectorhq.com"
    }
  ],
  "paths": {
    "/ai-builder-api/cli/projects/{projectId}/sandbox/edit": {
      "post": {
        "summary": "Write, patch or delete one file in the app's sandbox.",
        "operationId": "writePatchOrDeleteOneFileInTheAppsSandbox",
        "description": "Writes, patches or deletes one file in the app's sandbox. A small change to a text file is a `patch`: exact replacements, without resending the file. After a set of edits, save them with `POST .../sandbox/checkpoint`, then start a run with `externalAgent: true` to check, build and deploy them. A React Native app (`react_native_app_kernel_v1`) writes only under `apps/mobile/app-extensions/` and `apps/mobile/application-assets/`; its `routes.ts` is generated, so register its screens with `POST .../sandbox/extension-routes` instead of writing that file.",
        "parameters": [
          {
            "name": "projectId",
            "in": "path",
            "required": true,
            "description": "Project identifier.",
            "schema": {
              "type": "string",
              "pattern": "^prj_[A-Za-z0-9_-]+$",
              "example": "prj_01HXQ8Z6J9K2"
            }
          },
          {
            "name": "Version",
            "in": "header",
            "required": true,
            "description": "The API version.",
            "schema": {
              "type": "string",
              "enum": [
                "v3"
              ]
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CliSandboxEditRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "File written in the dev sandbox.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WritePatchOrDeleteOneFileInTheAppsSandboxResponse"
                }
              }
            }
          },
          "503": {
            "description": "The app's sandbox was asleep and is starting (`sandbox_starting`). Retry in about a minute; the app's context reports `ready` once it is up."
          }
        },
        "security": [
          {
            "bearer": [
              "clientportal-ai.write"
            ]
          }
        ]
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearer": {
        "type": "http",
        "scheme": "bearer",
        "description": "A sub-account access token: a Marketplace app's OAuth token or a Private Integration Token."
      }
    },
    "schemas": {
      "CliSandboxEditRequest": {
        "type": "object",
        "required": [
          "path"
        ],
        "properties": {
          "path": {
            "type": "string",
            "description": "File to write or delete, relative to the workspace root.",
            "example": "apps/clientportal-custom-app/pages/welcome.vue"
          },
          "action": {
            "type": "string",
            "enum": [
              "update",
              "delete",
              "patch"
            ],
            "description": "`update` writes `content` (the default); `delete` removes the file; `patch` applies `edits` to a text file that exists, so a small change does not resend the file.",
            "example": "update"
          },
          "content": {
            "type": "string",
            "description": "The file's full new content, encoded as `encoding` says. Not sent with `patch`.",
            "example": "<template><h1>Welcome</h1></template>\n"
          },
          "encoding": {
            "type": "string",
            "enum": [
              "utf-8",
              "base64"
            ],
            "description": "How `content` is encoded; `utf-8` by default.",
            "example": "utf-8"
          },
          "edits": {
            "type": "array",
            "minItems": 1,
            "maxItems": 50,
            "description": "`patch` only: replacements applied in order, each to the text the one before it produced. Each `oldText` must occur exactly once in the file, or nothing is written (`422 patch_text_not_found` / `patch_text_ambiguous`): include enough surrounding text to make it unique.",
            "items": {
              "type": "object",
              "required": [
                "oldText",
                "newText"
              ],
              "properties": {
                "oldText": {
                  "type": "string",
                  "minLength": 1,
                  "description": "Text the file holds exactly once, copied exactly.",
                  "example": "<h1>Welcome</h1>"
                },
                "newText": {
                  "type": "string",
                  "description": "What replaces it; empty deletes it.",
                  "example": "<h1>Welcome back</h1>"
                }
              },
              "title": "CliSandboxTextEdit"
            },
            "example": [
              {
                "oldText": "<h1>Welcome</h1>",
                "newText": "<h1>Welcome back</h1>"
              }
            ]
          }
        },
        "title": "CliSandboxEditRequest"
      },
      "WritePatchOrDeleteOneFileInTheAppsSandboxResponse": {
        "type": "object",
        "required": [
          "ok",
          "path"
        ],
        "properties": {
          "ok": {
            "type": "boolean",
            "enum": [
              true
            ],
            "description": "The file was written or deleted.",
            "example": true
          },
          "path": {
            "type": "string",
            "description": "The file, relative to the workspace root.",
            "example": "apps/clientportal-custom-app/pages/welcome.vue"
          }
        }
      }
    }
  }
}
```

## Code Examples

**cURL**

```bash
curl -L -X POST 'https://services.leadconnectorhq.com/ai-builder-api/cli/projects/<projectId>/sandbox/edit' \
  -H 'Authorization: Bearer <TOKEN>' \
  -H 'Version: v3' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "path": "apps/clientportal-custom-app/pages/welcome.vue",
  "action": "update",
  "content": "<template><h1>Welcome</h1></template>\n",
  "encoding": "utf-8",
  "edits": [
    {
      "oldText": "<h1>Welcome</h1>",
      "newText": "<h1>Welcome back</h1>"
    }
  ]
}'
```

**Node.js**

```javascript
const options = {
  method: 'POST',
  headers: {
    Authorization: 'Bearer <TOKEN>',
    Version: 'v3',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
      "path": "apps/clientportal-custom-app/pages/welcome.vue",
      "action": "update",
      "content": "<template><h1>Welcome</h1></template>\n",
      "encoding": "utf-8",
      "edits": [
        {
          "oldText": "<h1>Welcome</h1>",
          "newText": "<h1>Welcome back</h1>"
        }
      ]
  })
};

try {
  const response = await fetch('https://services.leadconnectorhq.com/ai-builder-api/cli/projects/<projectId>/sandbox/edit', options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

**Python**

```python
import requests

url = "https://services.leadconnectorhq.com/ai-builder-api/cli/projects/{projectId}/sandbox/edit"

headers = {
    "Authorization": "Bearer <TOKEN>",
    "Version": "v3"
}

payload = {
    "path": "apps/clientportal-custom-app/pages/welcome.vue",
    "action": "update",
    "content": "<template><h1>Welcome</h1></template>\n",
    "encoding": "utf-8",
    "edits": [
        {
            "oldText": "<h1>Welcome</h1>",
            "newText": "<h1>Welcome back</h1>"
        }
    ]
}

response = requests.post(url, json=payload, headers=headers)
print(response.json())
```

**PHP**

```php
<?php
$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => 'https://services.leadconnectorhq.com/ai-builder-api/cli/projects/<projectId>/sandbox/edit',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_HTTPHEADER => [
    'Authorization: Bearer <TOKEN>',
    'Version: v3',
    'Content-Type: application/json',
  ],
  CURLOPT_POSTFIELDS => '{
  "path": "apps/clientportal-custom-app/pages/welcome.vue",
  "action": "update",
  "content": "<template><h1>Welcome</h1></template>\n",
  "encoding": "utf-8",
  "edits": [
    {
      "oldText": "<h1>Welcome</h1>",
      "newText": "<h1>Welcome back</h1>"
    }
  ]
}',
]);

$response = curl_exec($curl);
curl_close($curl);
echo $response;
```

**Java**

```java
HttpResponse<String> response = Unirest
  .post("https://services.leadconnectorhq.com/ai-builder-api/cli/projects/<projectId>/sandbox/edit")
  .header("Authorization", "Bearer <TOKEN>")
  .header("Version", "v3")
  .header("Content-Type", "application/json")
  .body("{\"path\":\"apps/clientportal-custom-app/pages/welcome.vue\",\"action\":\"update\",\"content\":\"<template><h1>Welcome</h1></template>\\n\",\"encoding\":\"utf-8\",\"edits\":[{\"oldText\":\"<h1>Welcome</h1>\",\"newText\":\"<h1>Welcome back</h1>\"}]}")
  .asString();
```

**Go**

```go
package main

import (
  "fmt"
  "net/http"
  "io"
  "strings"
)

func main() {
  url := "https://services.leadconnectorhq.com/ai-builder-api/cli/projects/<projectId>/sandbox/edit"
  payload := strings.NewReader(`{
  "path": "apps/clientportal-custom-app/pages/welcome.vue",
  "action": "update",
  "content": "<template><h1>Welcome</h1></template>\n",
  "encoding": "utf-8",
  "edits": [
    {
      "oldText": "<h1>Welcome</h1>",
      "newText": "<h1>Welcome back</h1>"
    }
  ]
}`)
  req, _ := http.NewRequest("POST", url, payload)
  req.Header.Add("Authorization", "Bearer <TOKEN>")
  req.Header.Add("Version", "v3")

  res, _ := http.DefaultClient.Do(req)
  defer res.Body.Close()
  body, _ := io.ReadAll(res.Body)
  fmt.Println(string(body))
}
```

**Ruby**

```ruby
require 'uri'
require 'net/http'

url = URI("https://services.leadconnectorhq.com/ai-builder-api/cli/projects/<projectId>/sandbox/edit")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = "Bearer <TOKEN>"
request["Version"] = "v3"
request["Content-Type"] = "application/json"
request.body = '{
  "path": "apps/clientportal-custom-app/pages/welcome.vue",
  "action": "update",
  "content": "<template><h1>Welcome</h1></template>\n",
  "encoding": "utf-8",
  "edits": [
    {
      "oldText": "<h1>Welcome</h1>",
      "newText": "<h1>Welcome back</h1>"
    }
  ]
}'

response = http.request(request)
puts response.read_body
```

**PowerShell**

```powershell
$headers = @{
  "Authorization" = "Bearer <TOKEN>"
  "Version" = "v3"
}

$body = '{
  "path": "apps/clientportal-custom-app/pages/welcome.vue",
  "action": "update",
  "content": "<template><h1>Welcome</h1></template>\n",
  "encoding": "utf-8",
  "edits": [
    {
      "oldText": "<h1>Welcome</h1>",
      "newText": "<h1>Welcome back</h1>"
    }
  ]
}'

$response = Invoke-RestMethod -Uri 'https://services.leadconnectorhq.com/ai-builder-api/cli/projects/<projectId>/sandbox/edit' -Method 'POST' -Headers $headers -Body $body -ContentType 'application/json'
$response | ConvertTo-Json
```
