Skip to content

The TypeScript library

The library is a thin layer over the API for Node.js 22 and later, on a server. It does three things you would otherwise write yourself:

  1. Types for every route. Its client is generated from the same OpenAPI contract as the reference, so a misspelt field, a missing reference or an error code that does not exist is a compile error, not a 400 at the counter.
  2. Retries that never give twice. A write sent with no Idempotency-Key gets one, and every retry of it carries the same key, so the API answers the first result instead of writing again.
  3. A one-line webhook check. verifyWebhook checks the signature exactly as Verifying a webhook describes, and answers the event.
import { createPasskaar } from 'passkaar-sdk';
const passkaar = createPasskaar({ key: process.env.PASSKAAR_KEY! });
const { data, error } = await passkaar.POST('/awards', {
body: {
card: { card_id: 'GB9-H2Z-QF2' },
reference: 'INV-1049',
bill: { total: 1200, currency: 'PKR' },
},
});
if (error) console.log(error.code, error.detail);
else console.log(data.replayed ? 'already recorded' : 'recorded', data.earned, data.balance);

The client talks to https://api.passkaar.com/integrations/v1 unless you pass baseUrl. Every answer is { data, error, response }: data for a 2xx, otherwise error, the problem with its code.

The library retries what Handling errors says to retry, and nothing else:

What happened What the library does
No answer (a dropped connection, or 30 seconds with nothing) Sends again, with the same Idempotency-Key.
429 rate_limited Waits the seconds in Retry-After, then sends again.
500, 502, 503, 504 Waits a moment, longer each time, then sends again.
409 request_in_progress Waits a moment, then sends again: the first answer comes back.
Any other 4xx Hands it back at once. Sending it again would give the same answer.

It tries three more times by default, waiting up to half a second, then up to one, then up to two, and gives up early on a Retry-After longer than a minute. A write the API cannot replay (sending a test event to a webhook endpoint) is retried only after a 429, which the API refuses before doing anything. Change any of it with retries, baseDelayMs, maxDelayMs, maxRetryAfterMs and timeoutMs.

To retry across a restart of your own system, keep the key with the sale and pass it yourself:

await passkaar.POST('/awards', {
body: {
card: { card_id: 'GB9-H2Z-QF2' },
reference: 'INV-1049',
bill: { total: 1200, currency: 'PKR' },
},
params: { header: { 'Idempotency-Key': sale.idempotencyKey } },
});

A key is remembered for 24 hours. After that the reference still stops a second award: the API answers the first one with replayed: true.

import { verifyWebhook, WebhookVerificationError } from 'passkaar-sdk';
// rawBody: the request body exactly as it arrived, as a string or a Buffer.
try {
const event = verifyWebhook(rawBody, request.headers, process.env.PASSKAAR_WEBHOOK_SECRET!);
// event.type is 'award.created', 'card.claimed', …, and event.data is typed for it.
} catch (error) {
if (error instanceof WebhookVerificationError) return response.writeHead(400).end();
throw error;
}

It checks the three headers, that webhook-timestamp is within five minutes of your clock, and the signature in constant time. Pass both secrets, as an array, while you change yours; after rolling the secret Passkaar signs with both for 24 hours. error.reason says what failed: missing_header, invalid_timestamp, timestamp_outside_tolerance, no_matching_signature or invalid_body. A secret that is not a whsec_ secret throws a TypeError instead, so a mistake in your own settings is never taken for a delivery someone forged. Passkaar retries anything that is not a 2xx, so an event refused while your settings were wrong arrives again once they are right.