Skip to content

Development environment: content is being written, nothing points to production.

Documentation contents

Guides

Errors and idempotency

Read a v2 or v3 error, know when to retry, and replay a creation without duplicates with Idempotency-Key.

HTTP codes

CodeMeaningWhat to do
200, 201SuccessCarry on.
400Invalid request (missing field, format)Fix the request; do not retry it as is.
401Missing, wrong or revoked API keyCheck the key and the environment (sandbox or production).
403The key may not do thisinsufficient_scope: create a key with the scope named in scope. api_key_not_allowed: the route takes a dashboard session only (provider credentials, members, keys).
404Resource not foundCheck the reference; in v2, paymentRef has 24 hexadecimal characters.
409Same request still in progress (v3)Wait a second, then replay with the same Idempotency-Key.
422Business rule refusedRead the message; fix the data.
429Too many requestsRetry after the Retry-After delay.
5xxIncident at Konnect or at a providerRetry with a growing delay (for example 1, 2, 4, 8 seconds).

Error format

v2, the same as the current platform:

{
  "errors": [
    {
      "code": "is_invalid",
      "target": "field",
      "message": "Amount must be an integer number between 100 and 10000000.",
      "source": { "field": "amount" }
    }
  ]
}

for a validation error (HTTP 422, one entry per failed rule), and

{
  "errors": [
    {
      "code": "NOT_FOUND",
      "target": "common",
      "message": "Payment not found",
      "source": {},
      "extra": 1
    }
  ]
}

for a business error (401 AUTHENTICATE_TOKEN_INVALID, 403 FORBIDDEN, 404 NOT_FOUND, 406 NOT_ACCEPTABLE, 422 UNPROCESSABLE_ENTITY). Read errors[0].message; the amount message says 10000000 but the accepted maximum is 30000000.

v3: a stable code for your program and a message for a human.

{ "error": "idempotency_key_reused", "message": "This key was used with another request body." }

Branch your code on error, never on message (which can change).

Idempotency (v3)

A network cut at the wrong time leaves a question: was the payment created? With the Idempotency-Key header you can replay the same request without risking a duplicate.

  • Generate one key per business operation (for example a UUID v4 stored with the order), 1 to 255 visible ASCII characters.
  • Replaying the same key with the same body returns the original response, with the header Idempotent-Replayed: true.
  • The same key with another body, or on another route, returns 422 idempotency_key_reused.
  • A replay while the first request is still running returns 409 idempotency_request_in_progress.
  • A malformed key returns 400 invalid_idempotency_key.
  • Responses are kept for 24 hours. A failed request is not kept: you can replay it with the same key.
curl -X POST "$KONNECT_API_URL/payments" \
  -H "x-api-key: $KONNECT_API_KEY" \
  -H "Idempotency-Key: 3f1c9a52-2d4e-4b8f-9a60-7c1d2e3f4a5b" \
  -H "Content-Type: application/json" \
  -d '{ "amount": 120000, "currency": "TND", "orderId": "order-1001" }'

v3 payment route to come

POST /payments shows the v3 payment creation planned for phase 2. The x-api-key header (kpk_... keys) and the idempotency mechanism are already in place: see the v3 reference.

Retrying safely in v2

v2 does not accept Idempotency-Key. To avoid a double payment:

  • Do not replay init-payment after a timeout without knowing whether it went through: create a new payment for a new attempt of the buyer instead, and keep the same orderId.
  • Deliver an order once, by checking in your database that it is not already paid before marking it paid.