# Preview, confirm and undo

> Every write an assistant or an API client asks for is previewed first and applied with a token; every change runs as a batch you can undo in one call, with the person, key or assistant behind it in the audit log.

Source: /help/en/api/preview-confirm-and-undo

Nothing an AI assistant asks for changes your books on the first call. The first call returns a **preview**: the records that would change with their values before and after, the effect on the trial balance per period, and warnings (a lock date within a week, an approval tier that will route it, an amount above your threshold). The assistant shows you the preview and calls again with the **token** when you agree. The same two steps are available to any REST client with one header, and every write that goes through, by an assistant, an API key, a rule or an import, is recorded as a **change batch** that you can undo in one call from the audit log.

## How the two steps work for an assistant

1. The assistant calls a write tool (for example `post_bank_proposals`). Alya Books runs the function up to its first write, collects what it would change and rolls everything back. The answer has `status: "preview"`, a `token`, `expiresAt` and the `preview` block: `summary` (the tool, the tier, the arguments, how many records change), `items` (one per record: entity, action, `before`, `after`, the undo path, a link), `periodImpact` (debit, credit and net per account and period for anything that posts) and `warnings`.
2. The assistant shows you the preview. If you agree, it calls the same tool with the same arguments plus `confirmToken`. The change applies and the result's `_meta` names the **batch** it ran in (`einvoicing/batchId`) and the records it touched (`einvoicing/deepLinks`).
3. A token lasts **10 minutes** and runs **once**. If a record in the preview changed in between (someone edited the draft in the app), the apply refuses `preview_stale` and the assistant asks for a fresh preview. A reused token refuses `confirmation_used`; a late one `confirmation_expired`.

The per-tool policies of [MCP scopes, tiers and tool permissions](/help/en/api/mcp-scopes-and-tool-permissions) still apply: a tool on **Blocked** never runs; **Always** skips the preview only for draft-tier tools (saving a draft, a dry run); every write-tier and approve-tier tool previews whatever its policy.

A tool that reaches an **outside service** previews its arguments only and stops before the call: submitting to the e-invoicing service provider or the tax authority (`submit_invoice`, `submit_invoices`), sending an email, syncing a bank feed or a payment processor, filling a bill from the tax engine, exporting to a cloud store and uploading a file. The preview's `summary.note` names the service and `detailed` is `false`. Nothing is sent or uploaded until the assistant calls again with the token, so a document reaches the provider once. A step that asks an AI model does run on the preview, because for a draft tool (`compose_transactions`, `propose_accruals`, `detect_schedules`) the model's answer is the preview; that run uses AI credits like any other.

## The same for REST

- Send `X-Preview: true` on any write request (`POST`, `PUT`, `PATCH`, `DELETE`). The response is the preview with the token instead of the write.
- `POST /confirm/{token}` applies that request. The response is the write route's own, with the header `X-Change-Batch` naming the batch.
- A write without the header runs at once and still names its batch in `X-Change-Batch`.

```http
PATCH /api/v1/bank/transactions/bulk
X-API-KEY: sk_live_…
X-Preview: true

{ "ids": ["…"], "patch": { "accountId": "…" } }
```

```json
{ "status": "preview", "token": "oct_…", "expiresAt": "2026-10-29T10:12:00Z",
  "preview": { "summary": { "tool": "PATCH /bank/transactions/bulk", "function": "bulk_update_bank_transactions", "itemCount": 20, "changes": [{ "entity": "bank_transaction", "action": "categorise", "count": 20 }] },
               "items": [{ "entity": "bank_transaction", "entityId": "…", "action": "categorise", "before": { "proposal": { "accountId": "6100" } }, "after": { "proposal": { "accountId": "6200" } }, "undoKind": "uncategorise", "deepLink": "/bank?transaction=…" }],
               "periodImpact": [], "warnings": [] },
  "nextStep": "Show the preview (items, period impact, warnings) to the user and call the tool again with confirmToken when they agree." }
```

## Change batches and undo

Every write through the API runs as a batch: who did it (a person, an API key, an assistant acting for a member, an agent), through which tool, and one item per record touched with its image before and after. Rules, imports and the bulk actions of the app are batches too.

1. Go to **Compliance → Batches** (or ask the assistant to `list_change_batches`). Each row shows the actor, the tool, the number of records and the status: **Applied**, **Undone** or **Partially undone**.
2. Open a batch to see every item: the record, the action (created, updated, posted, reversed, matched, categorised, approved), the fields that changed with their old and new values, and the path an undo takes.
3. Click **Undo**, give a reason and confirm. Each record goes back through its own path: postings are reversed (never edited), categorisations and matches undone, patches put back to their before image, drafts created by the batch deleted, approvals withdrawn. The undo is itself a batch (kind **undo**), so it is auditable and can be undone in turn.
4. The batch then shows who undid it, when and why. Items whose path refused (a locked period, an approval in flight) keep the refusal on the item and the batch reads **Partially undone**; fix what blocks them and undo again.

An undo refuses `batch_superseded` when a later batch, or an edit made by hand after it, touched one of the records. Undo the later change first (the message lists it) or change the records by hand. Over MCP the tool is `undo_change_batch` (it previews like any write); over REST `POST /activity/batches/{id}/undo` with `{ "reason": "…" }`, and `GET /activity/batches`, `GET /activity/batches/{id}` list and read them.

## Who did it

Every audit row and every batch names its actor: **Person** (a member in the app), **API key** (the key's name), **Assistant** (the connected app's name, for the member who approved it, shown as "Claude (for Fatima)"), **Agent** (from the agent runtime) or **System**. The CSV export of the audit log carries the actor, its kind, the batch id and the link of the record. A write that was refused (a scope or role the connection lacks, a blocked tool, a stale preview, a locked period) is recorded too, as action **refused** with the code and the sentence on what to do next, so an assistant's blocked reason is in the log.

## A write into a locked period becomes a proposal

When the record an assistant or an API client wants to write is dated in a **locked period** (a closed fiscal year, a filed VAT return, a locked journal, the organisation's lock date), the write does not fail and does not post. The answer is `status: "proposed"` with a `proposalId`, a `deepLink` and the next step: the call is kept as a **proposed change** with the preview of what it would do, and it waits for an approver.

- The proposal appears in the group **Proposed changes** of the close checklist of the period that covers the date, and in the **Approvals** inbox of every owner, admin or member allowed to reopen periods. Each card shows who asked (the assistant for which member, the API key, the person), the values before and after and the period impact.
- The person who asked cannot approve it. In the app, **Approve and apply** is disabled on your own proposal, with the note "You proposed this change; another approver must decide."; an AI assistant or an API client that tries to approve its own member's proposal is refused with `same_user`. The approver clicks **Approve and apply**: Alya Books opens the period for exactly the proposal's dates and journal, checks the records are still as they were (`preview_stale` otherwise), runs the change as one batch in the requester's name with the approver recorded, relocks the period in the same transaction and marks the task done. **Decline** asks for a reason and tells the requester.
- In the app the same happens by hand: when the journal editor, the bank triage, a bill or an expense meets a locked period, it offers **Propose this change** instead of the error.
- An owner or admin can switch this off under Settings → Accounting (**Locked-period writes: refuse**); the API then answers `period_locked` as before.
- Amounts and dates of bank-synced lines (feed, statement, processor) are never editable over the API: `update_bank_transaction` refuses `bank_line_immutable`; the memo, the proposal fields and the splits stay editable.

## Good to know

- The preview runs the real function inside a transaction that is rolled back, so it reflects exactly what the apply will do, including validation refusals (those come back as errors on the first call, with no token).
- A route that writes outside a ledger function (a plain table write) still previews: the preview lists the arguments and marks `detailed: false`; the change is captured as a batch when it applies.
- Reads never ask for a token, and `dry_run_*` and `preview_*` tools stay what they are.
- In the app, a preview that would change no record and post nothing says "Nothing changes" and offers only **Close**: there is nothing to confirm.
- Warnings: `near_period_lock` (the entry is dated within 7 days after a lock date), `approval_tier` (a posting approval tier will route it), `large_amount` (above the organisation's large-transaction threshold), `routed_to_approval` (the change opens an approval request instead of posting).

## Related

- [MCP scopes, tiers and tool permissions](/help/en/api/mcp-scopes-and-tool-permissions)
- [Connect Alya Books to Claude or ChatGPT](/help/en/api/connect-ai-assistants)
- [Audit log](/help/en/account-and-setup/audit-log)
- [Error handling](/help/en/api/error-handling)
