Asynchronous jobs

For some endpoints whose tasks are potentially time-consuming (e.g., updating a Model), the API does not return a 200 OK status code. Instead, a 202 Accepted status code is returned, and an asynchronous job starts in the background, which will complete shortly.

The payload of a 202 Accepted response contains the ID of the asynchronous job that started:

PUT https://site-api.datocms.com/item-types/:model_id_or_api_key HTTP/1.1
X-Api-Version: 3
Authorization: Bearer YOUR-API-TOKEN
Accept: application/json
Content-Type: application/json
{ ... }
HTTP/1.1 202 Accepted
Content-Type: application/json
{
"data": {
"type": "job",
"id": "4235"
}
}

To get the result of of the asynchronous job, you need to poll the Job result endpoint. As long as the task is in progress, the endpoint will return a 404 Not found status code. As soon as the job completes, the status will change to 200 OK:

GET https://site-api.datocms.com/job-results/:job_result_id HTTP/1.1
X-Api-Version: 3
Authorization: Bearer YOUR-API-TOKEN
Accept: application/json
HTTP/1.1 200 OK
Content-Type: application/json
{
"data": {
"type": "job_result",
"id": "34",
"attributes": {
"status": 200,
"payload": {
"data": { ... }
}
}
}
}

In the payload of the response you'll find both the status code and the payload of the original request you performed.

Important: all endpoints could return async jobs in the future!

If you are using our Content Management API directly, without any of our official clients, you need to make sure to treat all endpoints as they might return an asynchronous job.

This is a fundamental constraint of using our Content Management API. In the event that an endpoint needs to be optimized, we want to leave ourselves the ability to return an asynchronous job without having to release a new API version.

If you are implementing a custom client yourself, the easiest way to proceed is to wrap every request you make to our API to a common logic that checks if the response is a job, and if so, it polls for its result before returning anything to the caller.

Official clients and async jobs

If you are using our Javascript client, the whole async job concept is completely invisible. Everything is handled in the client itself, and the API methods return a Promise that resolves with the final result of the asynchronous job — or throw an exception if the job status code is different than 2xx.