Idempotency
How Idempotency-Key works on MessageOS writes.
POST /v1/messages and POST /v1/knowledge require an Idempotency-Key header of 8 to 128 characters. POST /v1/events accepts one. Other endpoints ignore the header.
A key belongs to the client, not to one endpoint or one API key. Send a new key for each new request, and the same key when you retry the same request.
A key is not forgotten after a set time. Once an answer is stored for it, the same key and body get that answer however much later you repeat them, so never use a key again for a different message.
POST /v1/messages, POST /v1/knowledge, and POST /v1/events answer a repeat the same way:
| Repeat with the same key | Answer |
|---|---|
| Same body, after the first request finished | The stored status and body, with Idempotent-Replay: true |
| Same body, while the first request is still running | 409 conflict with Retry-After: 1 |
| Different body | 422 idempotency_key_reuse |
A stored response is replayed as it was, including an error such as 409 blocked. So the same key never tries a blocked send a second time: after quiet hours end, or you change the request or the contact, send a new key to try again.
A send the messaging network did not take is the exception. Its 409 blocked is not stored, and the same key sends again. Errors says which each reason is. After provider_error the first message may have gone out, so a second send with the same key can reach the person twice.
For events, the envelope id also stops a repeat: the same id returns the first event with "status": "duplicate". With no id, a retry with the same key and body after a failed request finds the event the failed request may have stored, so it still makes one event.
After a 500 or a 503
A 500 internal_error or a 503 service_unavailable is not stored for the key. Send the same request again with the same key and the same body. When the answer has a Retry-After header, wait that many seconds first.
However often you retry POST /v1/messages, at most one message is sent. If the first request got as far as sending, a retry does not send again: it answers with that send's outcome, such as 201 with "status": "sent", or with 409 conflict and Retry-After: 1 while the first request is unsettled. Keep the same key while you get conflict. A new key is a new message.
MessageOS settles a request left unsettled after about ten minutes. When it cannot tell whether that message went out, the next retry with the key answers 409 blocked with the reason provider_error, as a send the messaging network did not confirm does, and sends nothing. After that the same key sends again, and the person can get the message twice.
