Idempotency

Retry any request that moves money safely, with an Idempotency-Key.

Networks fail. To retry without moving money twice, every POST that moves money (a collection, a payout, a transfer, a checkout link) needs an Idempotency-Key header: any string up to 255 characters that names the thing you want to happen once.

curl https://gateway.swichi.cc/v1/collections \
  -H "Authorization: Bearer $SWICHI_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-1042" \
  -d '{"amount": 25000, "currency": "TZS", "phone": "0754 000 005", "reference": "order-1042"}'
  • A retry with the same key and the same body returns the first answer, with the same status code, and moves nothing again.
  • The same key with a different body is refused: idempotency_conflict.
  • A retry while the first request is still running gets idempotency_in_progress: wait a moment and retry with the same key.

Retrying

Every error says whether a retry can help (retryable). Retry network errors, 429 and 5xx with the same key; never retry other 4xx unchanged.

for (let attempt = 0; attempt < 4; attempt++) {
  const res = await fetch("https://gateway.swichi.cc/v1/collections", { method: "POST", headers, body })
  if (res.status !== 429 && res.status < 500) return res.json()
  const wait = Number(res.headers.get("Retry-After") ?? 2 ** attempt)
  await new Promise((r) => setTimeout(r, wait * 1000))
}

Choosing keys

Use your own ids: order-1042, payout-supplier-77-march. Do not use a random value made per attempt; it defeats the point. Keys belong to your account and mode: sandbox and live never collide.

Next steps

  • Webhooks: hear when money moves instead of polling.
  • Errors: every code and whether it is safe to retry.

On this page