Idempotency
A write can fail in a way that leaves you unsure whether it happened: the connection drops, or your request times out. Send an Idempotency-Key header with the write, and you can send the same request again without doing the work twice.
Send a write with a key
Choose a key of your own, such as an order number, and send it in the Idempotency-Key header of a write (POST, PUT, PATCH or DELETE). The key is 1 to 255 characters. Black ignores the header on a GET request.
POST /v1/scratch-notes
Authorization: Bearer <access token>
Content-Type: application/json
Idempotency-Key: order-7781
{"text":"Hello"}
The same request with cURL. The sample writes PLACEHOLDER_ACCESS_TOKEN where your access token goes:
curl --request POST https://api.black.example/v1/scratch-notes \
--header "Authorization: Bearer PLACEHOLDER_ACCESS_TOKEN" \
--header "Content-Type: application/json" \
--header "Idempotency-Key: order-7781" \
--data '{"text":"Hello"}'
The response is the one the write always gives:
HTTP/1.1 201 Created
{"object":"scratch_note","id":"scratch_01m77n5w09jcrd8jm79y381788","livemode":false,"text":"Hello","created":1794479681}
The note address exists only on environments that are not production, and is there for you to try this out.
Retry with the same key
Send the same request again with the same key. Black returns the first answer, with the same status, Content-Type, Location and body, and adds the header Idempotent-Replayed: true. Nothing is done a second time.
curl --include --request POST https://api.black.example/v1/scratch-notes \
--header "Authorization: Bearer PLACEHOLDER_ACCESS_TOKEN" \
--header "Content-Type: application/json" \
--header "Idempotency-Key: order-7781" \
--data '{"text":"Hello"}'
HTTP/1.1 201 Created
Idempotent-Replayed: true
{"object":"scratch_note","id":"scratch_01m77n5w09jcrd8jm79y381788","livemode":false,"text":"Hello","created":1794479681}
The same request means the same method, address, query string, Black-Version and body, sent byte for byte. A body with the same fields in another order is a different request, so build the body once and send those bytes again on every retry.
Send a new key for a new request
The same key with a different request answers 409 with the code idempotency_key_reused. Nothing is done.
curl --request POST https://api.black.example/v1/scratch-notes \
--header "Authorization: Bearer PLACEHOLDER_ACCESS_TOKEN" \
--header "Content-Type: application/json" \
--header "Idempotency-Key: order-7782" \
--data '{"text":"Hello"}'
curl --request POST https://api.black.example/v1/scratch-notes \
--header "Authorization: Bearer PLACEHOLDER_ACCESS_TOKEN" \
--header "Content-Type: application/json" \
--header "Idempotency-Key: order-7782" \
--data '{"text":"Goodbye"}'
HTTP/1.1 409 Conflict
{"error":{"type":"invalid_request_error","code":"idempotency_key_reused","message":"This Idempotency-Key was already used for a different request. Send a new key for a new request.","param":"Idempotency-Key","request_id":"req_01m77n5w3axszft2968cqnf6vm"}}
Send a new key for every new request. See Errors.
Wait while a request is in progress
While Black handles a request with a key, another request with the same key answers 409 with the code idempotency_request_in_progress. Wait a moment, then send the request again with the same key: it returns the first answer once that one has finished.
Retry after a server error
An answer with a status of 500 or more is not remembered, and nothing of the write is saved. Send the request again with the same key: Black carries it out as a new request, once.
Correct a refused request
An answer that refuses the request, such as a 422, is remembered and returned again on a retry. Correct the request and send it with a new key.
Know how long a key lasts
Black remembers a key for 24 hours. After that the key is new, and a request with it is carried out again.
Keys are kept apart for each environment. The same key in your live environment and in your test environment are two keys, and so are the same key of two accounts.
Fix a key that is not valid
An empty key, or one longer than 255 characters, answers 400 with the code idempotency_key_invalid. Send a key of 1 to 255 characters.