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

# Guard concurrent writes

When your system and people in the Circularo web app may touch the same document, an unconditional write can silently overwrite their change. Document reads return the current version as an `ETag`, and writes accept it back in `If-Match` — three requests in total.

## Before you begin

* **A document to work with.** The example uses one created beforehand.

## Step 1: Read the version from the ETag header

Document reads return the current version in the `ETag` response header:

```http Request theme={null}
GET /documents/aQ7xKvN2mRt5ZpB8cWdE
Authorization: Bearer YOUR_API_KEY
```

```http Response theme={null}
HTTP/2 200
ETag: "4"

{
  "id": "aQ7xKvN2mRt5ZpB8cWdE",
  "title": "Supplier contract",
  "trashed": false,
  ...
}
```

Keep the `ETag` value — the next steps use it to guard a write.

## Step 2: Attempt a write guarded by a wrong version

Pass a version in the `If-Match` header to make a write conditional. If it does not match the document's current version — typically because somebody changed the document after you read it — the write is rejected instead of overwriting their change:

```http Request theme={null}
DELETE /documents/aQ7xKvN2mRt5ZpB8cWdE
Authorization: Bearer YOUR_API_KEY
If-Match: "999"
```

```http Response theme={null}
HTTP/2 412

{
  "status": 412,
  "type": "RESOURCE:PRECONDITION_FAILED",
  "message": "The resource's current version does not match the If-Match header; fetch the resource again and retry with its current ETag.",
  "requestId": "c58e2f7b9a044d168c3e5b7f2a94d0e1"
}
```

The `412` says the document moved on since the version you supplied. Nothing changed on the server, so you are free to read the document again and decide what to do with the change you found.

A `412` can also come from the platform's own background processing. Shortly after an upload, a signature or the first download of the document's content, the platform itself updates the document (thumbnail, text extraction, read receipt). React to every `412` the same way: read the document again and retry, a bounded number of times.

## Step 3: Retry with the current version

With the version from the `ETag` you read earlier, the same delete succeeds:

```http Request theme={null}
DELETE /documents/aQ7xKvN2mRt5ZpB8cWdE
Authorization: Bearer YOUR_API_KEY
If-Match: "4"
```

```http Response theme={null}
HTTP/2 204
```

`If-Match` is optional everywhere: omit it and writes apply unconditionally, last write wins. Use it when several actors may touch the same document.

## Complete example

A conditional write that re-reads and retries, a bounded number of times, when it loses the race, as a Node.js script. Error handling is minimal for brevity — in production, inspect the error envelope of non-2xx responses.

```js theme={null}
// your environment's base URL
const BASE_URL = "https://sandbox.circularo.com/api/v2";
const API_KEY = "YOUR_API_KEY";
const AUTH_HEADER = { "Authorization": `Bearer ${API_KEY}` };
const documentId = "aQ7xKvN2mRt5ZpB8cWdE"; // the document to delete

const currentVersion = async () => {
  const res = await fetch(`${BASE_URL}/documents/${documentId}`, { headers: AUTH_HEADER });
  // error handling kept minimal for brevity
  if (!res.ok) throw new Error(`Read failed: ${res.status}`);
  return res.headers.get("ETag");
};

let res;
for (let attempt = 1; attempt <= 5; attempt++) {
  const version = await currentVersion();
  res = await fetch(`${BASE_URL}/documents/${documentId}`, {
    method: "DELETE",
    headers: { ...AUTH_HEADER, "If-Match": version }
  });
  if (res.status !== 412) break;
  // somebody - or the platform's own processing - changed the document in the meantime: look at it again before insisting
}

console.log(`Delete finished with ${res.status}`);
```

## Next steps

* See a `412` next to the other error classes — [Handle errors](https://developers.circularo.com/guides/robust-integrations/errors.md).
* Guard the writes that change a document's content — [Annotate and seal a document](https://developers.circularo.com/guides/documents-and-files/annotate-and-seal.md).
* Which writes accept `If-Match` is documented per endpoint in the API reference.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.