---
title: "Edit source files with string replacement"
method: POST
path: "/websiteSourceCode/files/str-replace"
tags: ["Source code"]
---

# Edit source files with string replacement

`POST /websiteSourceCode/files/str-replace`

## Request

- Base URL: `https://api.cactal.ai/v1`
- URL: `https://api.cactal.ai/v1/websiteSourceCode/files/str-replace`
- Auth: HTTP bearer

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. Takes the edit lease when no `leaseToken` is sent.

## Request body

- object
  - `websiteId` string, required
  - `files` object[], required: 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.
    - `name` string, required: Name of the file to edit. Singleton files are addressed by their type name, for example `global_css`.
    - `edits` object[], required: Edits applied in order against the evolving content of this file.
      - `oldStr` string, required: Exact text to find. Must match exactly once unless `replaceAll` is `true`.
      - `newStr` string, required: Replacement text.
      - `replaceAll` boolean: Replace every occurrence instead of requiring a unique match.
  - `dryRun` boolean: Validate every edit and report the replacement counts without committing.
  - `leaseToken` string: Omit to have Cactal take the edit lease for you; pass a token from websiteSourceCode.lease.acquire to take over from another editor.

## Response `200`

The new head version plus per-file replacement counts.

- object
  - `version` integer, required: Head version after the edit: the previous head plus one, or the unchanged head when `changed` is `false`.
  - `changed` boolean, required: `false` when the edit was a no-op and no new version was created.
  - `files` object[], required: Per-file replacement counts, in request order. Reported for `dryRun` calls too, which commit nothing.
    - `name` string, required: Name of the edited file.
    - `replacements` integer, required: Occurrences replaced in this file.

## Other responses

- `400`: Validation failed. The response `message` names the first invalid field.
- `401`: Missing, invalid, expired, or revoked API key.
- `403`: The authenticated principal lacks the required capability.
- `404`: The resource does not exist or is outside the principal’s access scope.
- `409`: The request conflicts with the current state of the resource.
- `429`: Rate limit exceeded. Retry after the number of seconds in the `Retry-After` header.

---

[API](https://skmtc.dev/cactal/apis/cactal-api.md) · [All operations](https://skmtc.dev/cactal/apis/cactal-api/llms.txt) · [OpenAPI document](https://skmtc.dev/cactal/apis/cactal-api/revisions/9cd0fb5eacdd?raw)
