• GuideBreaking change in 2.0.0: the tax ID is required
  • GuideAccess
  • GuideKey scopes
  • GuideLocking a key to your network addresses
  • GuideErrors
  • GuideRate limits
  • GuideSafe retries (Idempotency-Key)
  • GuidePages
  • GuideWebhooks
  • GuideCheck the signature
  • GuideA receiver to start from
  • GuideSandbox
  • RecipeYour first call in 5 minutesMake a sandbox key and list your merchants.
  • RecipeStart a review and wait for review.readyStart a website review, get told when it is done, read the findings and download the report.
  • RecipeVerify webhook signaturesProve a delivery came from Kvali, answer fast and handle retries once.
  • RecipeSend a merchant's documentsUpload a registration certificate or bank letter in two steps.
  • RecipeMonitoring: checks, changes, alerts, violationsLearn what monitoring found about your live merchants, without missing or repeating a row.
  • RecipeRetries, idempotency, pages and rate limitsA small client that retries safely, pages through lists and stays under the limit.
  • RecipeLock a key to your network addressesMake a stolen key useless outside your own network.
  • RecipeFrom sandbox to liveThe checks to pass in the sandbox, then the switch to production keys.

↑ ↓ to move · Enter to open · Esc to close

Retries, idempotency, pages and rate limits

A small client that retries safely, pages through lists and stays under the limit.

15 minScope: read

What you get

  • Writes that never run twice, whatever the network does.
  • A loop over every merchant, page by page.
  • A client that stays under 120 requests a minute.
  1. Send an Idempotency-Key on every write

    On every POST and PATCH, send a new UUID as Idempotency-Key and keep it for the retries of that request. The same key and the same request answer the first answer again, with Idempotent-Replayed: true. The same key with another body is 422 idempotency_mismatch; while the first is still running, 409 idempotency_in_progress.

    Code
    curl -X PATCH "https://aikvali.com/api/v1/merchants/3f0c2a52-6a1e-4c1b-9d55-2f7f5b1e9a10" \
      -H "Authorization: Bearer $KVALI_KEY" \
      -H "Content-Type: application/json" \
      -H "Idempotency-Key: $(uuidgen)" \
      -d '{"status":"live"}'
  2. Retry only what is safe to retry

    429 and 5xx are safe to retry; any other 4xx means fix the request. On 429, wait Retry-After seconds. Every answer carries RateLimit-Limit (120 a minute per key), RateLimit-Remaining and RateLimit-Reset.

    Code
    const BASE = "https://aikvali.com/api/v1";
    
    export async function kvali(path, { method = "GET", body } = {}) {
      const idempotencyKey = method === "GET" ? undefined : crypto.randomUUID(); // one key for every retry
      for (let attempt = 1; ; attempt++) {
        const res = await fetch(BASE + path, {
          method,
          headers: { Authorization: "Bearer " + process.env.KVALI_KEY, ...(body && { "Content-Type": "application/json" }), ...(idempotencyKey && { "Idempotency-Key": idempotencyKey }) },
          body: body && JSON.stringify(body),
        });
        if (res.ok) return res.json();
        const retry = res.status === 429 || res.status >= 500;
        if (!retry || attempt === 5) throw new Error((await res.json()).error.code);
        const wait = Number(res.headers.get("Retry-After")) || 2 ** attempt;
        await new Promise((r) => setTimeout(r, wait * 1000));
      }
    }
  3. Page through a list

    Lists return at most 100 rows a page (limit, default 25). Merchants and reviews run newest first: pass the last page's next_before as before. The monitoring lists and violations use since and next_cursor instead (see the monitoring recipe).

    Code
    let before = null;
    do {
      const page = await kvali("/merchants?limit=100" + (before ? "&before=" + encodeURIComponent(before) : ""));
      for (const m of page.data) console.log(m.id, m.legal_name, m.health.verdict);
      before = page.next_before;
    } while (before);