Web API design best practices - Azure Architecture Center
Learn the best practices for designing web APIs that support platform independence and service evolution.
Implement asynchronous methods
Sometimes a POST, PUT, PATCH, or DELETE method might require processing that takes time to complete. If you wait for completion before you send a response to the client, it might cause unacceptable latency. In this scenario, consider making the method asynchronous. An asynchronous method should return HTTP status code 202 (Accepted) to indicate that the request was accepted for processing but is incomplete.
Expose an endpoint that returns the status of an asynchronous request so that the client can monitor the status by polling the status endpoint. Include the URI of the status endpoint in the Location header of the 202 response. For example:
HTTP
Copy
HTTP/1.1 202 Accepted
Location: /api/status/12345
If the client sends a GET request to this endpoint, the response should contain the current status of the request. Optionally, it can include an estimated time to completion or a link to cancel the operation.
HTTP
Copy
HTTP/1.1 200 OK
Content-Type: application/json
{
"status":"In progress",
"link": { "rel":"cancel", "method":"delete", "href":"/api/status/12345" }
}
If the asynchronous operation creates a new resource, the status endpoint should return status code 303 (See Other) after the operation completes. In the 303 response, include a Location header that gives the URI of the new resource:
HTTP
Copy
HTTP/1.1 303 See Other
Location: /api/orders/12345
For more information, see Provide asynchronous support for long-running requests and Asynchronous Request-Reply pattern.