Skip to Content
Docs

Idempotency and retries

Retry API requests safely with an idempotency key.

The Pay.com API supports idempotency, so you can retry a request that may already have worked. This is useful when a request fails in transit and you don't receive a response. For example, if a request to create a charge fails due to a network error, retry it with the same idempotency key. Pay.com returns the saved response rather than creating another charge.

How it works

When you make a request with an idempotency key, Pay.com reserves the key and, once the request is processed, saves the response (status code and body). After that, a request with the same key returns the saved response instead of running the operation again, whether the original request succeeded or failed.

  • Pay.com honors the idempotency-key header on POST and DELETE requests. Use a key on every POST that creates or changes something.
  • The header is ignored on GET, PATCH, and PUT requests. Retrying a PATCH or PUT is not protected by a key.
  • Keys are scoped to your merchant account, so they can't collide with another merchant's keys.
  • Keys expire 24 hours after they are stored.

For charges, holds, refunds, and payouts, set your own reference as well. It helps you find out what was created if a request times out or receives no response.

Sending an idempotency key

Pass a unique value in the idempotency-key HTTP header. Amounts are integers in minor units (for example, 8000 is $80.00 USD).

curl -X POST https://api.sandbox.pay.com/v1/charges \
  -u ':sk-pay-com-sandbox-…' \
  --header 'idempotency-key: a0e4c2f1-8b3d-4f7a-9c1e-2d5b6a7f8e9c' \
  --header 'Content-Type: application/json' \
  --data '{
    "amount": 8000,
    "currency": "usd",
    "source": "pm_card_685125127054035968"
  }'

The response echoes the key in an Idempotency-Key header.

Generate a unique value on your client side for each new request. We recommend V4 UUIDs.

Retry behavior

Pay.com stores the response of most requests that reach processing, including payment declines and most errors. A retry with the same key, sent to the same endpoint with the same body, gets that stored response:

Original requestRetry with the same key
Succeeded (2xx)Returns the same successful response.
Failed during processing, for example a decline (400 payment_method_error) or most server errors (5xx)Returns the same error.
Still processingReturns 409 with type: idempotency_error and code: request_in_progress. Wait, then retry with the same key.
Rejected before the key was reserved (invalid JSON, a body over 1 MB, or an invalid or unauthorized API key returning 401 or 403)Nothing is saved. Fix the cause, and the same idempotency key works.

If you reuse a key on a different endpoint, or with a different request body, Pay.com returns HTTP 400 with type: idempotency_error and code: invalid_parameters, because the key is already bound to another request. A retry must send the same body to the same endpoint.

When to retry

Retry requests when the outcome is unknown or when the API instructs you to wait. Use exponential backoff between attempts and cap the total number of retries.

What you gotWhat to do
No response (timeout, 408, or dropped connection)Wait. For a charge, hold, refund, or payout, check whether the object was created. If nothing was created, retry with the same key and body.
409 request_in_progressWait, then retry with the same key.
429 or 503Wait for the seconds in Retry-After, or a few seconds when there's none, then retry with the same key and body.
502 or 504Wait, then retry with the same key and body.
500Retrying with the same key typically returns the cached error. Check whether the object was created, and send a new request with a new key only if it wasn't.
Any other 4xx, declines includedDon't retry as it is. Fix the request, or ask for another payment method, and send it with a new key.

A key doesn't protect PUT and PATCH. Retrieve the object before you repeat one, as the change may already be made. GET requests are safe to repeat, except Retrieve a network token cryptogram, which makes a new cryptogram on each call.

Find out what was created

If a request receives no response, you don't receive an object ID. Look up the reference value you passed in the request instead:

Key expiration

Idempotency keys are stored for 24 hours. After that, using the same key starts a new request rather than returning the saved response. Make sure your retry logic sends retries within 24 hours of the original request.

Was this page helpful?