🔁 Idempotency-Key
Network retries are normal — the same POST might land twice if a transient error makes you unsure whether the first attempt succeeded. Pass Idempotency-Key on creation, and a duplicate POST returns the same payment instead of creating a second one.
What to do
- Click Run scenario. We POST
/paymentstwice with identical bodies and identicalIdempotency-Keyheaders. - Both responses below should have the same
id— proving the second call returned the cached first response, not a new payment. - Counter-test: change the body slightly between two calls with the same key → should get a 409 conflict (we don't run that here, but the docs explain).
The code
// Node.js — same key, same body, two separate POSTs
const idemKey = 'demo-' + Date.now()
const body = JSON.stringify({ amount: 500, currency: 'USD', description: 'Idem demo' })
const r1 = await fetch('https://app.sknpay.com/api/v1/payments', {
method: 'POST',
headers: { Authorization: 'Bearer '+KEY, 'Content-Type':'application/json', 'Idempotency-Key': idemKey },
body,
})
const r2 = await fetch('https://app.sknpay.com/api/v1/payments', {
method: 'POST',
headers: { Authorization: 'Bearer '+KEY, 'Content-Type':'application/json', 'Idempotency-Key': idemKey },
body,
})
const a = await r1.json(), b = await r2.json()
console.assert(a.id === b.id, 'Same idempotency-key must return same payment id')
<?php
// Same idempotency-key on two separate cURL calls returns the same payment.
$idemKey = 'demo-' . time();
$body = json_encode(['amount'=>500,'currency'=>'USD','description'=>'Idem demo']);
function postPayment($body, $idemKey) {
$ch = curl_init('https://app.sknpay.com/api/v1/payments');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('SKNPAY_API_KEY'),
'Content-Type: application/json',
'Idempotency-Key: ' . $idemKey,
],
CURLOPT_POSTFIELDS => $body,
]);
return json_decode(curl_exec($ch), true);
}
$a = postPayment($body, $idemKey);
$b = postPayment($body, $idemKey);
assert($a['id'] === $b['id']);
# Python
idem_key = f'demo-{int(time.time())}'
body = {'amount': 500, 'currency': 'USD', 'description': 'Idem demo'}
H = {'Authorization': f'Bearer {KEY}', 'Idempotency-Key': idem_key}
r1 = requests.post('https://app.sknpay.com/api/v1/payments', headers=H, json=body)
r2 = requests.post('https://app.sknpay.com/api/v1/payments', headers=H, json=body)
assert r1.json()['id'] == r2.json()['id']