# CRUD API Asynchronous Operations Extension

Version: 0.1.0 · Status: draft · License: Apache-2.0

Source: lingodata/crud@bc225ba

## 1. Asynchronous operations

The **Asynchronous operations** extension lets Client and Server agree that an operation runs in the background: the Server accepts it, returns an Operation ID, and the Client retrieves the outcome later, by polling or through a callback. It is optional: Servers that do not implement it are not required to follow any rule in this document. By default, every operation is synchronous.

This extension requires the Base Specification `^0.2.0`.

### 1.1 Negotiation

Neither Participant is required to declare its abilities. A Client that declares nothing is treated as a synchronous-only Client, and a Server that declares nothing is treated according to how it responds.

<a id="ASYNC_CLIENT_PREFER"></a>**Rule `ASYNC_CLIENT_PREFER`**: MAY

A Client that supports both modes MAY declare a preference for asynchronous processing.

<a id="ASYNC_CLIENT_REQUIRE"></a>**Rule `ASYNC_CLIENT_REQUIRE`**: MAY

A Client that supports only asynchronous processing MAY declare that it requires it.

<a id="ASYNC_CLIENT_DEFAULT"></a>**Rule `ASYNC_CLIENT_DEFAULT`**: MUST

A Client that declares neither a preference nor a requirement MUST be treated as supporting only synchronous processing.

<a id="ASYNC_SERVER_MATRIX"></a>**Rule `ASYNC_SERVER_MATRIX`**: MUST

The Server MUST respond according to the table below. A preference MUST NOT make an operation fail: when the Server cannot run the operation asynchronously, it MUST run it synchronously.

| | Client declares nothing | Client prefers async | Client requires async |
|---|---|---|---|
| **Server supports sync only** | sync | sync | [`NEGOTIATION`](./base.md#NEGOTIATION) error |
| **Server supports async only** | [`NEGOTIATION`](./base.md#NEGOTIATION) error | async | async |
| **Server supports both** | sync | async | async |

> **Note (non-normative): Rationale**
>
> The absence of a declaration is the inference: no declaration means synchronous only, so no Participant has to guess the abilities of the other.

### 1.2 Accepting an operation

<a id="ASYNC_OPERATION_ID"></a>**Rule `ASYNC_OPERATION_ID`**: MUST

When the Server runs an operation asynchronously, it MUST respond immediately that the operation was accepted, with an Operation ID: a UUIDv7 in `uuid-hex` format that identifies the operation.

<a id="ASYNC_PROTOCOL_LOOKUP"></a>**Rule `ASYNC_PROTOCOL_LOOKUP`**: MUST

Each Protocol specification MUST define how a Client retrieves the state of an operation from its Operation ID.

> **Note (non-normative)**
>
> For example, HTTP can return the URL of the operation in the `Location` header, and MCP can define a method that takes the Operation ID.

### 1.3 Operation state

The state of an operation is one of **pending** (still running), **succeeded** or **failed**.

<a id="ASYNC_POLL_PENDING"></a>**Rule `ASYNC_POLL_PENDING`**: MUST

While the operation runs, retrieving its state MUST indicate that it is pending.

<a id="ASYNC_PROGRESS"></a>**Rule `ASYNC_PROGRESS`**: MAY

A pending state MAY include progress, as the number of items done and, when known, the total. Each Protocol specification MAY define how progress is pushed to the Client as it happens.

<a id="ASYNC_POLL_SUCCESS"></a>**Rule `ASYNC_POLL_SUCCESS`**: MUST

When the operation has succeeded, retrieving its state MUST return the Result and the Metadata (such as counts, Warnings and specification versions) that a synchronous execution of the same operation would have returned.

<a id="ASYNC_POLL_FAILURE"></a>**Rule `ASYNC_POLL_FAILURE`**: MUST

When the operation has failed, retrieving its state MUST return the error data ([Base section 3.4.4](./base.md#344-error-handling)) that a synchronous execution would have returned.

<a id="ASYNC_RETENTION"></a>**Rule `ASYNC_RETENTION`**: SHOULD

The Server SHOULD declare how long it keeps the state of completed operations. After that, retrieving the state MUST fail with the [`NOT_FOUND`](./base.md#NOT_FOUND) error, with the `operation` target.

> **Tip (non-normative): Example**
>
> A `deleteByQuery` that matches 50,000 Resources is accepted with the Operation ID `0190a5f2-9a00-7c00-8000-00000000abcd`. While it runs, its state is `{ "status": "pending", "progress": { "done": 12000, "total": 50000 } }`. When it succeeds, its state carries what the synchronous response would have carried: no Resources ([`RES_EMPTY`](./base.md#RES_EMPTY)) and the Modified Count `50000`.

### 1.4 Callbacks

<a id="ASYNC_CALLBACK_URL"></a>**Rule `ASYNC_CALLBACK_URL`**: MAY

The Client MAY send a callback URL. When the operation completes, the Server MUST send its final state ([`ASYNC_POLL_SUCCESS`](#ASYNC_POLL_SUCCESS) or [`ASYNC_POLL_FAILURE`](#ASYNC_POLL_FAILURE)) to that URL.

<a id="ASYNC_CALLBACK_ERROR_URL"></a>**Rule `ASYNC_CALLBACK_ERROR_URL`**: MAY

The Client MAY send a separate callback URL for failures. When it does, the Server MUST send failed states to it instead of the callback URL.

<a id="ASYNC_CALLBACK_SIGNATURE"></a>**Rule `ASYNC_CALLBACK_SIGNATURE`**: SHOULD

Callbacks SHOULD be sent and signed as defined by [Standard Webhooks](https://www.standardwebhooks.com/), so that Clients can verify that they come from the Server.

<a id="ASYNC_CALLBACK_POLL_FALLBACK"></a>**Rule `ASYNC_CALLBACK_POLL_FALLBACK`**: MUST

Even when the Client sends a callback URL, the Server MUST keep the state of the operation retrievable by its Operation ID.

> **Caution (non-normative): Security**
>
> A callback URL makes the Server send requests to an address chosen by the Client, which can be abused to reach internal networks (server-side request forgery). Servers MAY restrict the hosts accepted as callback URLs.
