Skip to content
DocsIdempotency and retries

Theme

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:

curl -X POST https://api.nibify.app/v1/requests \
  -H "Authorization: Bearer sk_test_…" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: invoice-flow:kiwi-studio:INV-2026-0142" \
  -d '{
    "sender": { "name": "invoice-flow" },
    "notification": {
      "title": "Kiwi Studio invoice: $1,240, due Oct 30",
      "body": "Read from the PDF. File it as consulting, or fix it first.",
      "quickActions": [
        { "name": "file", "label": "File it" },
        { "name": "fix", "label": "Fix it" }
      ]
    }
  }'
  • Same key, same body: no new request. The answer is 200 instead of 201, with the request that the first call created, as it stands now. Its status may no longer be pending, if the person has already answered.
  • Same key, different body: 422 with idempotency_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 200 with the request the first call created. A retry creates nothing, so it spends nothing. A new request in that environment is a 402 with plan_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:

ts
const { request } = await nibify.getRequest(requestId);
const result = await request.wait();

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.

import { Card, Column, Nibify, Text } from '@nibify/sdk';

const nibify = new Nibify({ apiKey: 'sk_test_…' });

const surface = {
  root: 'root',
  dataModel: {},
  components: [
    Card('root', { child: 'body' }),
    Column('body', { children: ['title', 'text'] }),
    Text('title', { text: 'Kiwi Studio invoice filed', variant: 'h4' }),
    Text('text', { text: '$1,420, consulting, due Oct 30.' }),
  ],
};

await nibify.notify({
  sender: { name: 'invoice-flow' },
  notification: {
    title: 'Kiwi Studio invoice filed',
    body: '$1,420, consulting, due Oct 30.',
  },
  surface,
});

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 maxRetries times after the first attempt (3 by default): sending a request with ask(), getRequest(), and the lists.
  • Retried up to 10 times in a row, about a minute, whatever maxRetries says: waiting for the request, in ask() and request.wait(). A dropped connection says nothing about the request, so the wait carries on through a restart of the API. It stays inside its timeout and signal: a wait that ends while retrying resolves with timeout.
  • Not retried: notify(), request.cancel() and request.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:

ts
import { NibifyError } from '@nibify/sdk';

try {
  await nibify.notify({ sender, notification }); // as above
} catch (error) {
  if (error instanceof NibifyError) {
    error.status; // 429, or null when no HTTP answer came back
    error.code; // 'rate_limited'
    error.retryAfterSeconds; // set when the API sent Retry-After
  }
}

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:

ts
import { Nibify } from '@nibify/sdk';

const nibify = new Nibify({ apiKey: 'sk_test_…', maxRetries: 0 });

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.

http
HTTP/1.1 429 Too Many Requests
Retry-After: 1
Content-Type: application/json

{
  "error": {
    "code": "rate_limited",
    "message": "…"
  }
}

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.