# Document Manage Async (https://developers.doctavian.com/openapi/latest/reference/Documents/operations-documents-document-manage-async)

## `POST /documents/document/manage/async`

**Document Manage Async**

Runs the same file management as `documents-document-manage`, but asynchronously. Every rule of the synchronous operation applies unchanged; see its description for the request.

This endpoint returns HTTP `200` with an empty body as soon as the request is accepted and queued. That response only acknowledges receipt; it does not mean the operation succeeded.

**The result arrives at your callback URL, not in the response to this call.** Once the operation finishes, Doctavian sends a `POST` to the URL formed by joining the `iss` and `dest` claims of the `X-Client-Authorization` token supplied on this request, authenticating itself through the external connector named by that token's `connector_id` claim.

On success the callback body is the `DocumentManageResponse` envelope — the same body `documents-document-manage` returns. On failure it is the `ErrorResponse` envelope, carrying the failure's own code: unlike the synchronous operation, no `…_FUNCTION_FAILED` entry is added after it. Correlate a callback with its request through `externalContext`, which is echoed back unchanged.

A `400` from this endpoint means the request was rejected before being queued, so no callback follows it. Usage limits are checked once the request is processed, so a reached limit arrives at the callback as an `ErrorResponse` with `LIMIT_BREACH`, not as an HTTP `403` from this endpoint.

### Parameters
- `X-Client-Authorization` (header, string) — This does not authenticate the async request itself — that's `Authorization`. It authenticates Doctavian's callback to your service once the async result is ready. Generate it by calling `POST /common/client/token` with the claims below inside a `clientToken` object. That endpoint signs and encrypts the payload for you and returns the ready-to-use token in `result.data.clientToken.token` — you never construct or sign this token yourself. Required claims: `connector_id` (the id of the external connector, a GUID, that Doctavian uses to authenticate to your service), `sub` (a placeholder kept for backward compatibility, whose value Doctavian does not use — copy the `connector_id` value into it), `iss` (base URL of the service receiving the callback), `dest` (the callback route, e.g. `/temp/callback` — combined with `iss` to form the full callback URL). Optional claims: `oid`,`client_id`,`full_name`, `email`, `locale`, `zoneinfo`. You don't need to set `iat` or `exp` — the token endpoint handles timing. `iss` and `dest` cannot resolve back to Doctavian's own API — that combination is rejected to prevent a callback loop. If this header is missing on an async request, the response is `400 X_CLIENT_AUTH_ERROR` ("header is missing for async operations").

### Request body (`application/json`)

- `externalContext` (any) — Arbitrary JSON object supplied by the caller and echoed back unchanged in the response, on success and on error alike. Its shape is not validated, fixed or interpreted.
- `sourceDocument` (any) **required** — The existing file to load. Mandatory.
- `targetDocument` (any) **required** — The file to produce. Mandatory: a request without it fails with `500` `MANAGE_FUNCTION_FAILED`.
- `data` (object) — Optional data for the resulting file.
  - `embedded` (string) — Optional custom metadata to write into the resulting file's document properties (e.g. PDF metadata), as a JSON-serialized string of key-value pairs. This metadata isn't visible in the document's rendered content but can be read back from the file's properties. Omit it if no custom metadata is needed.

### Responses

#### `200` — The request was accepted and queued for background processing. This response has no body -- it acknowledges receipt only and says nothing about whether the operation succeeded. The resulting file's metadata, or the error if the operation fails, is delivered separately by a `POST` to the callback URL built from the `X-Client-Authorization` token's `iss` and `dest` claims. See this operation's callback documentation for both payload shapes.

#### `400` — The request was rejected before it was queued, so no callback is sent for it. A missing `X-Client-Authorization` header returns `X_CLIENT_AUTH_ERROR`. Failures that occur while the operation runs are not reported here -- they are delivered to the callback URL instead.

`application/json`:

- `error` (object) **required** — What went wrong. `statusCode` and `message` describe the failure category, `innerErrors` the specific causes.
  - `innerErrors` (array<object>) **required** — One entry per distinct problem found with the request. At least one entry is always present, and a request failing several checks reports each of them separately -- read the whole array rather than only the first entry.
    - `code` (string) **required** — Stable, machine-readable Doctavian error code identifying what went wrong, in SCREAMING_SNAKE_CASE. This is the field to branch on in code: the two message fields are prose and may be reworded, but a code, once published, keeps its meaning.
    - `message` (string) **required** — Technical explanation of this specific failure, written for developers and logs. May reference internal specifics and is not safe to show an end user -- use `userMessage` for anything user-facing.
    - `userMessage` (string) **required** — The same failure phrased for an end user and safe to display in a UI verbatim. Says what the user can do about the problem where there is something to do, and never exposes internal detail.
    - `urn` (string) — The request property this error concerns, when the error is about one property. Optional: most errors leave it out. Named by the property's server-side name in PascalCase, not the JSON field name — `TemplateName` for `template.templateName`. Not related to the `urn` that identifies a file elsewhere in the API.
    - `eventId` (string) _(format: uuid)_ — Identifier of the logged failure behind this entry. Present only when the failure was logged, so treat it as optional. Quote it together with `operationId` when reporting the problem to support.
  - `message` (string) **required** — Short summary of the failure category, mirroring the HTTP status text (e.g. `Bad Request`). It names the class of failure only; the specific cause is in `innerErrors`.
  - `statusCode` (integer) **required** — HTTP status code of the error, repeated inside the body so the envelope stays self-contained when it is logged or forwarded. Always a number, never a string, and always equal to the response's own HTTP status.
  - `externalErrors` (array<any>) — Additional error detail attached as the server produced it. Each entry can be any JSON value and its shape is not fixed: an object returned by an external service the request depended on — such as the Google Apps Script execution layer used for `gdoc`, `gsheet` and `gslide` generation — or a plain string, such as the message of an unhandled exception. Use it for diagnostics only, and branch on `innerErrors[].code` instead. Empty or absent when there is nothing to add.
- `dateTime` (string) **required** _(format: date-time)_ — UTC timestamp when the error response was generated, with between three and seven decimal places. Doctavian's own responses end in `Z`; a response from the gateway — a `401` for a missing or invalid credential, or a `404` for a request that matches no operation — has seven decimal places and ends in `+00:00` instead.
- `operationId` (string) — Identifier of this operation in Doctavian's Azure logs, generated per request and returned on both success and failure. This is the primary reference to quote when reporting a problem to support; when `eventId` is also present, quote both. Left out of a response from the gateway, which answers some requests before they reach Doctavian: a `401` for a missing or invalid credential, and a `404` for a request that matches no operation, such as one without a mandatory query parameter. Such a response has no operation to log, and its `origin` may be empty.
- `eventId` (string) _(format: uuid)_ — Identifier of the corresponding entry in Doctavian's server-side error log. Present only when the failure was handled and logged, so treat it as optional and don't rely on it being there. When reporting a problem to support, quote it together with `operationId`: `operationId` identifies the request that was made, `eventId` identifies the logged failure behind it, and having both is what lets support trace the request end to end. A failure can instead, or also, carry an `eventId` on individual `innerErrors` entries.
- `origin` (string) **required** — The HTTP method and full request URL that produced this response, e.g. `POST https://api.doctavian.com/v1/documents/document/generate`. Useful when the envelope is logged or forwarded away from the call that produced it.
- `userId` (string) _(format: uuid)_ — The unique identifier of the user or system that initiated the request. Left out of the response when the request carried no bearer token — a guest caller on an operation that does not require one, such as a recipient signing with a `tid` — because there is no signed-in user to identify. Also left out when the caller has no Doctavian user yet: `platform-subscription-resolve` and `platform-subscription-trial-start` run before the caller's user exists, so their responses carry no `userId`.
- `externalContext` (object) — Optional, arbitrary JSON object echoed back unchanged from the request's externalContext, if one was supplied. Used to correlate this response with the request that produced it.