Skip to main content
A CodeFragment holds the generated source files for an assistant message and links them to a running E2B sandbox. The fragments API lets you write file changes directly to that sandbox and persist them to the database, enabling inline code editing without re-running the full AI pipeline.
This endpoint requires a Pro subscription. Requests from accounts without the inline_code_edit feature flag return 403 Forbidden.

PATCH /api/fragments/:fragmentId

Update one or more files in a code fragment. ForgeAI writes each file to the E2B sandbox, merges the new contents with the existing files map, and persists the result to the database.

Path parameters

fragmentId
string
required
The ID of the CodeFragment to update. Minimum 3 characters.

Request body

files
object
required
A map of relative file paths to their new contents. Keys are paths relative to the project root (e.g. "app/page.tsx"), values are the full file content strings.
{
  "app/page.tsx": "export default function Page() { return <h1>Hello</h1>; }",
  "app/globals.css": "body { margin: 0; }"
}
projectId
string
required
The ID of the project that owns this fragment. Minimum 3 characters.
sandboxId
string
Fallback E2B sandbox ID to use if the fragment does not have a sandboxId stored in the database. Not required when the fragment already has a sandboxId.

Behavior

  1. Looks up the existing fragment by fragmentId.
  2. Resolves the sandbox: uses the fragment’s stored sandboxId, falling back to body.sandboxId.
  3. Writes each file in body.files to the E2B sandbox at the resolved project path.
  4. Merges body.files into the fragment’s existing files map (new keys are added, existing keys are overwritten).
  5. Persists the merged files map to the database.
  6. Returns the updated CodeFragment.

Response

Returns the updated CodeFragment object.
id
string
required
Unique fragment identifier (UUID).
messageId
string
required
ID of the parent message.
sandboxUrl
string
required
Public URL of the live E2B sandbox.
sandboxId
string
E2B sandbox identifier.
title
string
required
Human-readable title for the fragment.
files
object
required
The complete, merged map of relative file paths to file contents after the update.
imageUrl
string
Screenshot of the sandbox, or null.
designSpec
object
Structured design specification, or null.
createdAt
string
required
ISO 8601 creation timestamp.
updatedAt
string
required
ISO 8601 last-updated timestamp.

Example

curl --request PATCH \
  --url /api/fragments/frag_333 \
  --header 'Content-Type: application/json' \
  --header 'Authorization: Bearer <session_token>' \
  --data '{
    "files": {
      "app/page.tsx": "export default function Page() { return <h1>Updated</h1>; }"
    },
    "projectId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
  }'
{
  "id": "frag_333",
  "messageId": "msg_222",
  "sandboxUrl": "https://sbx_xyz789.e2b.dev",
  "sandboxId": "sbx_xyz789",
  "title": "To-Do List App",
  "files": {
    "app/page.tsx": "export default function Page() { return <h1>Updated</h1>; }",
    "app/globals.css": "body { margin: 0; }"
  },
  "imageUrl": null,
  "designSpec": null,
  "createdAt": "2024-04-05T12:00:45.000Z",
  "updatedAt": "2024-04-05T15:20:00.000Z"
}

Build docs developers (and LLMs) love