Idempotency keys
Prevent duplicate transactions when network issues cause uncertain request outcomes
An idempotency key lets you retry a request without running it twice. Send a unique value in the idempotency-key header. If you send the same request again with the same key, Preczn returns the first request's response instead of repeating the work.
Send an idempotency key on every transaction request. A timeout or a 5xx doesn't tell you whether the transaction ran, and a retry without a key can charge the customer twice.
How a key works
POST /v1/transactions and the other supported endpoints handle keys differently. Both keep a completed request's response for 24 hours.
Process Transaction (POST /v1/transactions)
- Scope: Preczn matches the key within one merchant. For a void, capture, or refund, that's the merchant of the original transaction.
- Stored results: when the transaction's status is
A,D, orP, Preczn keeps the key for 24 hours. A retry in that time returns the first response. - Released keys: when the status is
E, or the request fails with an error, Preczn releases the key. A retry with the same key runs as a new request. - In flight: while the first request is still running, a retry with the same key returns
409. Preczn doesn't send the duplicate to the processor. - Reused keys: a retry with a different
type, or a differentamount, returns400. See the Errors for a reused key section.
Every other supported endpoint
- Scope: Preczn matches the key with the API key you send, the method, and the path. For Create Client Session, it also matches the request body.
- Stored results: Preczn keeps a successful response for 24 hours. A retry in that time returns that response, even if the retry's body is different.
- Errors: Preczn doesn't keep an error response. A retry after an error runs as a new request.
- In flight: Preczn doesn't hold a second request while the first is running. Wait for a response or a timeout before you retry.
A replayed response can be out of date
A retry returns the first response as it was sent, not the record's current state. For example, a transaction that first returned
Pstill replays asPafter it's approved. To learn a transaction's current status, read it with Get Transaction, or use webhooks.
Retry a request safely
The following diagram shows a retry after a lost response:
sequenceDiagram
participant S as Your server
participant P as Preczn
participant X as Processor
S->>P: POST /v1/transactions with idempotency-key
P->>X: Send the transaction
X-->>P: Approved
P--xS: Response lost to a timeout
S->>P: Retry with the same idempotency-key
P-->>S: First response, replayed
To retry a request safely, your server does the following:
- Generate a new key for each operation, such as one UUID for each charge.
- Send the key on the first attempt, not only on retries. Store it with your own record of the operation.
- On a timeout, a
5xx, or a409, wait and send the same request with the same key. - Use a new key only for a new operation. A reused key returns the earlier operation's response.
The following example retries a transaction with the same key and a growing delay:
const crypto = require('crypto');
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function processTransaction(body, maxAttempts = 3) {
const idempotencyKey = crypto.randomUUID(); // one key for every attempt
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
const response = await fetch('https://api.preczn.com/v1/transactions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.PRECZN_PRIVATE_KEY,
'idempotency-key': idempotencyKey,
},
body: JSON.stringify(body),
signal: AbortSignal.timeout(30000),
});
const retryable = response.status === 409 || response.status >= 500;
if (!retryable || attempt === maxAttempts) return response;
} catch (error) {
// A timeout or network error: the transaction may have run, so retry with the same key.
if (attempt === maxAttempts) throw error;
}
await sleep(500 * 2 ** attempt);
}
}Errors
A retry while the first POST /v1/transactions request is still running returns 409. Wait, then retry with the same key:
{
"statusCode": 409,
"message": "A request with this idempotency key is still being processed; retry shortly",
"error": "Conflict"
}Errors for a reused key
Preczn matches a key within one merchant. If you send POST /v1/transactions with a key that already completed a transaction for the same merchant, and the new request has a different type, Preczn returns 400 Bad Request with this body:
{
"statusCode": 400,
"message": "Idempotency Error – The provided idempotency key has already been used with a different transaction type or amount. Please ensure each unique transaction uses a distinct idempotency key.",
"error": "Bad Request"
}Preczn also returns this error when the new request sends a different amount, unless the request is a verify or leaves out amount.
For other error codes, see Errors.
Supported endpoints
These endpoints accept the idempotency-key header:
| Endpoint | API reference |
|---|---|
POST /v1/transactions | Process Transaction |
POST /v1/tokens | Create Payment Method Token |
PATCH /v1/tokens/{tokenId} | Update Token |
POST /v1/merchants | Create Merchant |
PATCH /v1/merchants/{merchantId} | Update Merchant |
DELETE /v1/merchants/{merchantId} | Delete Merchant |
POST /v1/merchants/{merchantId}/owners | Add Merchant Owner |
PATCH /v1/merchants/{merchantId}/owners/{ownerId} | Update Merchant Owner |
POST /v1/merchants/{merchantId}/owners/{ownerId}/attachments | Add Owner Attachment |
PUT /v1/merchants/{merchantId}/owners/{ownerId}/attachments/{attachmentId} | Update Owner Attachment |
POST /v1/merchants/{merchantId}/bankaccounts | Add Merchant Bank Account |
PATCH /v1/merchants/{merchantId}/bankaccounts/{bankId} | Update Bank Details |
POST /v1/merchants/{merchantId}/bankaccounts/{bankId}/documents | Add Banking Attachment |
PUT /v1/merchants/{merchantId}/bankaccounts/{bankId}/documents/{documentId} | Update Banking Attachment |
POST /v1/merchants/{merchantId}/attachments | Add Merchant Attachment |
PATCH /v1/merchants/{merchantId}/connections/{connectionId} | Update Connection Details |
POST /v1/merchants/{merchantId}/connections/{connectionId}/onboard | Onboard Merchant to Connection |
POST /v1/merchants/{merchantId}/connections/{connectionId}/close | Close Merchant Account at Connection |
POST /v1/merchants/{merchantId}/connections/{connectionId}/reset | Reset Merchant Connection |
POST /v1/merchants/{merchantId}/connections/{connectionId}/billing-groups | Add Billing Group to Merchant Connection |
DELETE /v1/merchants/{merchantId}/connections/{connectionId}/billing-groups/{billingGroupId} | Delete Billing Group from Merchant Connection |
POST /v1/forms | Create Form |
POST /v1/forms/{formId}/locations | Create a location on the form's merchant |
POST /v1/formTemplates | Create Form Template |
PATCH /v1/formTemplates/{id} | Update Form Template |
POST /v1/webhooks | Create Webhook |
PATCH /v1/webhooks/{webhookId} | Update Webhook |
DELETE /v1/webhooks/{webhookId} | Delete Webhook |
POST /v1/saq | Create SAQ |
PATCH /v1/saq/{saqId} | Update SAQ |
POST /v1/saq/sign/{saqId} | Sign PDF |
POST /v1/saq/{id}/connection/{connectionId}/send | Send Signed SAQ to Connection |
POST /v1/saq/template/upload | Upload SAQ Template PDF |
POST /v1/sessions | Create Client Session |
Updated 6 days ago
