Idempotency
A network failure can leave you not knowing whether a booking was made. The Idempotency-Key header lets you retry POST /bookings without ever booking twice.
The Idempotency-Key header#
POST /bookings requires an Idempotency-Key header: 8 to 128 characters, unique per booking attempt. A missing or wrong-length key is 400 invalid_idempotency_key. A random UUID works. Other routes do not use it.
Make the key when the guest submits the booking form, store it with that attempt, and reuse it for every retry of that attempt. A new attempt (the guest changes the dates and submits again) gets a new key.
What Tulo does with it#
| You send | Tulo answers |
|---|---|
| A key Tulo has not seen | Runs the request and remembers the result for 24 hours. |
| The same key and the same body, after success | The original response again, with the header Idempotency-Replayed: true. Nothing new is booked. |
| The same key and the same body, while the first is still running | 409 idempotency_in_progress with Retry-After: 1. Wait a second and retry. |
| The same key with a different body | 409 idempotency_key_reused. Nothing runs. |
| The same key after the first request was refused or failed | Runs it again. Only a success is stored: any answer of 400 or above, 5xx included, releases the key, because Tulo answers an error only when nothing was booked. After a 4xx, fix the request first; after a 5xx, retry as is. |
A 201 with Idempotency-Persistence: degraded | The booking was made, but Tulo could not store the answer for replay. Keep this booking. Do not send the key again expecting a replay: it answers 409 idempotency_in_progress until it expires (24 hours). |
Idempotency-Persistence: degraded#
Rare: Tulo made the booking but its idempotency store failed to keep the answer. Treat the 201 as the booking and store booking.id. If your server lost that answer too, do not guess: list your bookings with GET /bookings for the dates, and only if the booking is not there, try again with a new key. If Tulo stopped in the middle of a request and never answered, the key also stays held (409 idempotency_in_progress) until it expires; reconcile the same way.
Keys are scoped to the API key that sent them, so two websites never collide. The body is compared byte for byte, which the signing rule already requires: send exactly the same bytes on a retry.
Retries and signatures#
A retry is a new request for signing purposes: new timestamp, new nonce, new signature. It keeps the same Idempotency-Key and the same body. Reusing a nonce gets 401 signature_replayed before idempotency is even checked.
In practice#
- 1.The guest submits. Create the attempt in your database with a new key.
- 2.Serialise the body once and keep it with the attempt.
- 3.Send it, signed. On a timeout, send the stored body again with the stored key and a fresh signature.
- 4.On
201(first time or replayed) storebooking.idand show the confirmation.
