Idempotency and retries
Send a request again without asking twice, what the SDK retries on its own, and what to do with a 429.
A call can fail in a way that tells you nothing: the connection drops after Nibify received the request and before you read the answer. The request about Kiwi Studio's invoice may exist or may not, and sending it again may ring the phone twice. This page covers how to send again without asking twice, what the SDK retries on its own, and what to do with a 429.
Send a request twice, get one
Give POST /v1/requests an Idempotency-Key header, and a retry with the same key is recognised as one. Choose a key that names the thing you are asking about, so that every attempt, from any process, sends the same one. Here it is the invoice's number:
- Same key, same body: no new request. The answer is
200instead of201, with the request that the first call created, as it stands now. Itsstatusmay no longer bepending, if the person has already answered. - Same key, different body:
422withidempotency_conflict, and nothing is created. Two different requests under one key are a bug in the caller, and it is reported rather than hidden. - Same key, same body, with the month's allowance spent: still
200with the request the first call created. A retry creates nothing, so it spends nothing. A new request in that environment is a402withplan_limit_reached, which no retry fixes (plan limits).
The body is compared by its fields, so key order and spacing do not matter. A key belongs to a project and an environment: the same key under sk_test_ and sk_live_ names two requests. It lasts as long as the request does.
The header is optional. Send it when you retry; without it, every call creates a request.
With the SDK
ask() sends an Idempotency-Key of its own and keeps it across its own retries, so a retry inside one ask() never asks twice. You cannot choose that key. If the job that calls ask() can itself run twice for the same invoice, two ask() calls are two requests. Then send the request with your own key over HTTP, keep the requestId, and wait for it with the SDK:
Notifications are not retried
POST /v1/notifications takes no Idempotency-Key: a notification has no lifecycle to attach one to. A retry after a lost answer could put the same notification on the phone twice, so notify() does not retry. A failure throws, and the choice is yours.
A 429 is safe to retry: nothing was created. After a network error or a 5xx, the notification may have been sent.
What the SDK retries
The SDK retries a call that failed on the network, with a 429, or with a 5xx. It waits as long as Retry-After says, or otherwise half a second, doubling up to eight seconds, with some jitter.
- Retried up to
maxRetriestimes after the first attempt (3 by default): sending a request withask(),getRequest(), and the lists. - Retried up to 10 times in a row, about a minute, whatever
maxRetriessays: waiting for the request, inask()andrequest.wait(). A dropped connection says nothing about the request, so the wait carries on through a restart of the API. It stays inside itstimeoutandsignal: a wait that ends while retrying resolves withtimeout. - Not retried:
notify(),request.cancel()andrequest.nudge(). A cancel retried after a lost answer would find the request already cancelled; a nudge retried would push twice.
Anything else, and a retry that runs out, rejects with a NibifyError:
cancel() and nudge() on a request that has already ended reject with 409, and a code that names the state: request_already_answered, request_already_expired, request_already_dismissed or request_already_cancelled.
To change how many times the SDK retries everything but the wait, pass maxRetries to the client; 0 turns those retries off:
Rate limits and 429
Each project has a limit of calls per second and a daily volume, counted over the last 24 hours. The test and live environments are counted apart, so a test loop cannot spend your live budget. All the API keys of a project share it: a second key does not get you a second budget.
Over either limit, the API answers 429 with rate_limited and a Retry-After header in seconds. The message says which limit it was and what it is.
Wait for Retry-After, then send again; the refused call was neither carried out nor counted. Waiting for an answer with GET /v1/requests/{id}/response is never counted, however long and however often.