Skip to content

Handling errors

When a request fails, the answer has an HTTP status and a body like this one, served as application/problem+json (RFC 9457):

{
"type": "https://passkaar.com/developers/errors/insufficient_balance",
"title": "The card does not hold enough",
"status": 422,
"detail": "There aren’t enough points on this card — 195 left.",
"code": "insufficient_balance",
"request_id": "87c65c3fb36a590f35b399389926c789"
}
  • Branch on code. It never changes. title and detail are words for a person and may.
  • Log request_id. It is also the X-Request-Id header. Quote it when you write to Passkaar and we can find the request.
  • type is the code’s page, with what it means and what to do: https://passkaar.com/developers/errors/insufficient_balance opens /errors/insufficient_balance/ in these docs, and every code has its page at /errors/{code}/.
  • A code you do not know is still an error: treat it by its HTTP status. New codes are added without notice.

Every code is listed on the errors page. This guide makes the ones you will meet first happen, against your test shop, after the earlier guides.

Every guide starts here
export PASSKAAR_API=https://api.passkaar.com/integrations/v1
printf 'Your key: '; read -rs PASSKAAR_KEY; echo; export PASSKAAR_KEY
Terminal window
CARD_ID=$(jq -r '.card.card_id' enrolment.json); CUSTOMER=$(jq -r '.customer.external_id' enrolment-online.json)
REF=INV-$(date +%s); echo "$REF"

Retry these, with the same Idempotency-Key

Section titled “Retry these, with the same Idempotency-Key”
Code Status What to do
rate_limited 429 Wait the seconds in Retry-After, then send again.
request_in_progress 409 The same key is still being worked on. Wait a moment and send again.
internal_error 500 Send again after a short wait, then a longer one.
unavailable 503 As internal_error.
No answer at all A timeout or a dropped connection. Send again; you get the first result if it was recorded.

Because every write carries an Idempotency-Key and a reference, a retry never gives or spends twice. Every other code gives the same answer until the request or the card changes, so retrying it as it is does nothing.

Hit the rate limit by sending ten reads at once:

Terminal window
for i in $(seq 1 10); do
curl -sS -o /dev/null -w '%{http_code} ' "$PASSKAAR_API/programs" -H "Authorization: Bearer $PASSKAAR_KEY" &
done; wait; echo
429 429 429 429 429 429 429 429 200 200

The refused ones answered:

{
"type": "https://passkaar.com/developers/errors/rate_limited",
"title": "Too many requests",
"status": 429,
"code": "rate_limited",
"request_id": "66ec4b91aa8a1869b4167f39dfb33436"
}

with the header retry-after: 1.

Fix the request: the same key, a different body

Section titled “Fix the request: the same key, a different body”

An Idempotency-Key belongs to one request. Sending another body under it is refused, so a bug that reuses keys cannot record the wrong sale:

Terminal window
KEY=$(uuidgen)
curl -sS -o /dev/null "$PASSKAAR_API/awards" -H "Authorization: Bearer $PASSKAAR_KEY" \
-H "Content-Type: application/json" -H "Idempotency-Key: $KEY" \
-d "{\"card\": {\"card_id\": \"$CARD_ID\"}, \"reference\": \"$REF\", \"bill\": {\"total\": 2450, \"currency\": \"PKR\"}}"
curl -sS "$PASSKAAR_API/awards" -H "Authorization: Bearer $PASSKAAR_KEY" \
-H "Content-Type: application/json" -H "Idempotency-Key: $KEY" \
-d "{\"card\": {\"card_id\": \"$CARD_ID\"}, \"reference\": \"$REF\", \"bill\": {\"total\": 2600, \"currency\": \"PKR\"}}" | jq .

422:

{
"type": "https://passkaar.com/developers/errors/idempotency_key_reused",
"title": "The idempotency key was used with a different request",
"status": 422,
"code": "idempotency_key_reused",
"request_id": "f493eb0eafc96fcabf36e3addf97688e"
}

A new attempt at a different request needs a new key.

A reference is one sale. The same invoice number on another customer’s card is refused, because it means two sales were mixed up. $REF was just recorded on Ayesha’s card; send it for Bilal:

Terminal window
curl -sS "$PASSKAAR_API/awards" -H "Authorization: Bearer $PASSKAAR_KEY" \
-H "Content-Type: application/json" \
-d "{\"card\": {\"external_id\": \"$CUSTOMER\"}, \"reference\": \"$REF\", \"bill\": {\"total\": 2450, \"currency\": \"PKR\"}}" | jq .
{
"type": "https://passkaar.com/developers/errors/reference_reused",
"title": "The reference was used for a different card",
"status": 422,
"code": "reference_reused",
"request_id": "c799038c527aac053e1cc08d9c22b40a"
}

The same reference on the same card is not an error: it answers the first sale with replayed: true.

Terminal window
curl -sS "$PASSKAAR_API/redemptions" -H "Authorization: Bearer $PASSKAAR_KEY" \
-H "Content-Type: application/json" \
-d "{\"card\": {\"card_id\": \"$CARD_ID\"}, \"reference\": \"$REF-spend\", \"spend\": {\"type\": \"points\", \"points\": 5000}}" | jq .
{
"type": "https://passkaar.com/developers/errors/insufficient_balance",
"title": "The card does not hold enough",
"status": 422,
"detail": "There aren’t enough points on this card — 391 left.",
"code": "insufficient_balance",
"request_id": "00572df8c0ac5f9f2603bfecf5ff1a04"
}

Show the customer their balance from a lookup, and let them spend less.

A field missing, misspelled or of the wrong kind answers invalid_request, with each problem in errors:

Terminal window
curl -sS "$PASSKAAR_API/cards/lookup" -H "Authorization: Bearer $PASSKAAR_KEY" \
-H "Content-Type: application/json" \
-d "{\"card\": {\"card_id\": \"$CARD_ID\"}}" | jq .
{
"type": "https://passkaar.com/developers/errors/invalid_request",
"title": "The request is not valid",
"status": 400,
"code": "invalid_request",
"request_id": "ac2270181b2da131e1337933b6d8be23",
"errors": [
{
"path": "/",
"message": "name the card by exactly one of serial, card_id, phone, email or external_id"
}
]
}

A lookup takes the card’s name itself as the body ({"card_id": "…"}), where awards and spends put it under card. Fix the request; sending it again unchanged gives the same answer.

Another shop’s card, a card that does not exist, and a mistyped id all answer the same:

Terminal window
curl -sS "$PASSKAAR_API/cards/00000000-0000-4000-8000-000000000000" \
-H "Authorization: Bearer $PASSKAAR_KEY" | jq .
{
"type": "https://passkaar.com/developers/errors/not_found",
"title": "Not found",
"status": 404,
"code": "not_found",
"request_id": "b727c8f7793a37a98a8bd8772c511212"
}

Every problem with the key, its connection, the shop’s account or what the connection may do answers one 401 unauthorized, saying nothing more. Check the key, then ask the shop’s owner whether the connection is paused or may do what you asked; see What the shop can do to your connection.

Code Means
card_required Spending was asked by phone or email. Ask for the card.
ambiguous A phone or email matches more than one customer or card. Ask for the card, or send program.
operation_not_allowed The card’s type does not do that, such as spending a VIP card.
daily_cap_reached, give_limit_reached The card has had all it may get today.
bill_too_large Above the connection’s largest bill. Check the amount; the owner can raise it.
program_closed, customer_limit_reached The card is not taking sign-ups, or the shop’s plan is full.
shop_locked The shop cannot sign anyone up for now; existing cards keep earning and spending.

Each has its own page with what to do.