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

# Edit source files with string replacement

> Atomically applies ordered exact-string edits to up to 50 files and 200 edits in total as one head version. Each `oldStr` must match once unless `replaceAll` is true, and any failing edit aborts the whole call. Set `dryRun` to validate and count replacements without committing. Requires the active lease.



## OpenAPI

````yaml /api-reference/openapi.json post /websiteSourceCode/files/str-replace
openapi: 3.1.0
info:
  title: Cactal API
  version: 1.0.0
  description: >-
    The Cactal public API. Create, edit, publish, and operate websites
    programmatically. Authenticate every request with an API key sent as
    `Authorization: Bearer <key>`.
servers:
  - url: https://api.cactal.ai/v1
security:
  - apiKey: []
tags:
  - name: Documentation
    description: >-
      Search and read the Cactal product documentation for concepts, guides,
      agent workflows, platform behavior, and API operations.
  - name: Feedback
    description: >-
      Submit free-form product feedback from users and agents to the Cactal
      feedback inbox.
  - name: Websites
    description: >-
      Create and manage websites — the top-level resource that owns source code,
      content, assets, domains, and analytics.
  - name: Website editors
    description: >-
      Grant, list, and revoke website-scoped editor access, and invite
      collaborators to a single website by email.
  - name: API keys
    description: >-
      Create, scope, rotate, and revoke the API keys that authenticate
      programmatic and agent access.
  - name: Source code
    description: >-
      Read and edit the framework source files of a website draft. Mutations
      require an edit lease.
  - name: Publishing
    description: Validate, build, publish, and roll back website versions.
  - name: CMS collections
    description: >-
      Define the content model: collections of structured content owned by a
      website.
  - name: CMS fields
    description: Manage the typed fields that make up a collection schema.
  - name: CMS items
    description: >-
      Create, query, publish, and organize the content entries inside a
      collection.
  - name: Domains
    description: >-
      Manage platform subdomains and custom domains, including DNS verification
      and the primary domain.
  - name: Assets
    description: Upload and manage website files and images served from the Cactal CDN.
  - name: Media generation
    description: >-
      Generate reference-guided website images that are stored as ordinary
      Cactal CDN assets.
  - name: Project Context
    description: >-
      Upload private durable reference material that the website agent can
      search, read, and inspect.
  - name: Analytics
    description: >-
      Read first-party traffic analytics for a website and export datasets as
      CSV.
  - name: Organizations
    description: Manage organizations, members, and organization-wide invitations.
  - name: Audit log
    description: >-
      Read the immutable record of actions performed in an organization by users
      and API keys.
  - name: Billing
    description: >-
      Read billing state and manage plans, site capacity, and prepaid usage
      balance. Every billing operation requires the organization owner role,
      which API keys cannot hold — today these operations are performed from the
      dashboard, return 403 for API-key callers, and are hidden from MCP tool
      lists.
paths:
  /websiteSourceCode/files/str-replace:
    post:
      tags:
        - Source code
      summary: Edit source files with string replacement
      description: >-
        Atomically applies ordered exact-string edits to up to 50 files and 200
        edits in total as one head version. Each `oldStr` must match once unless
        `replaceAll` is true, and any failing edit aborts the whole call. Set
        `dryRun` to validate and count replacements without committing. Requires
        the active lease.
      operationId: websiteSourceCode.files.strReplace
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                websiteId:
                  type: string
                  minLength: 1
                files:
                  minItems: 1
                  maxItems: 50
                  type: array
                  items:
                    type: object
                    properties:
                      name:
                        type: string
                        minLength: 1
                        description: >-
                          Name of the file to edit. Singleton files are
                          addressed by their type name, for example
                          `global_css`.
                      edits:
                        minItems: 1
                        type: array
                        items:
                          type: object
                          properties:
                            oldStr:
                              type: string
                              minLength: 1
                              description: >-
                                Exact text to find. Must match exactly once
                                unless `replaceAll` is `true`.
                            newStr:
                              type: string
                              description: Replacement text.
                            replaceAll:
                              description: >-
                                Replace every occurrence instead of requiring a
                                unique match.
                              type: boolean
                          required:
                            - oldStr
                            - newStr
                        description: >-
                          Edits applied in order against the evolving content of
                          this file.
                    required:
                      - name
                      - edits
                  description: >-
                    Files to edit (up to 50, and 200 edits in total). Every edit
                    across every file commits as one version, or none of them
                    do.
                dryRun:
                  description: >-
                    Validate every edit and report the replacement counts
                    without committing.
                  type: boolean
                leaseToken:
                  type: string
                  minLength: 1
              required:
                - websiteId
                - files
                - leaseToken
      responses:
        '200':
          description: The new head version plus per-file replacement counts.
          content:
            application/json:
              schema:
                type: object
                properties:
                  version:
                    type: integer
                    description: >-
                      Head version after the edit: the previous head plus one,
                      or the unchanged head when `changed` is `false`.
                  changed:
                    type: boolean
                    description: >-
                      `false` when the edit was a no-op and no new version was
                      created.
                  files:
                    type: array
                    items:
                      type: object
                      properties:
                        name:
                          type: string
                          description: Name of the edited file.
                        replacements:
                          type: integer
                          description: Occurrences replaced in this file.
                      required:
                        - name
                        - replacements
                      additionalProperties: false
                    description: >-
                      Per-file replacement counts, in request order. Reported
                      for `dryRun` calls too, which commit nothing.
                required:
                  - version
                  - changed
                  - files
                additionalProperties: false
        '400':
          description: >-
            Validation failed. The response `message` names the first invalid
            field.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ServiceError'
        '401':
          description: Missing, invalid, expired, or revoked API key.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ServiceError'
        '403':
          description: The authenticated principal lacks the required capability.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ServiceError'
        '404':
          description: >-
            The resource does not exist or is outside the principal’s access
            scope.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ServiceError'
        '409':
          description: The request conflicts with the current state of the resource.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ServiceError'
        '429':
          description: >-
            Rate limit exceeded. Retry after the number of seconds in the
            `Retry-After` header.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ServiceError'
components:
  schemas:
    ServiceError:
      type: object
      required:
        - kind
        - message
      description: >-
        Canonical error body returned by every non-2xx response. Extra fields
        carry error-specific details.
      properties:
        kind:
          type: string
          enum:
            - validation
            - unauthorized
            - forbidden
            - not_found
            - conflict
            - rate_limited
            - internal
          description: Stable, machine-readable error category.
        message:
          type: string
          description: Human-readable explanation of the failure.
        suggestion:
          type: string
          description: >-
            What to do next when the failure has a known fix; may name the exact
            operation to call.
        validValues:
          type: array
          items:
            type: string
          description: The acceptable values for the failing field, when the set is closed.
      additionalProperties: true
  securitySchemes:
    apiKey:
      type: http
      scheme: bearer
      description: >-
        Cactal API key. Create one in the dashboard or via `POST /apiKeys`. The
        plaintext key is shown once at creation.

````