Idempotent Requests
Retry a request safely after a timeout or a dropped connection, without creating a second charge or refund.
Why it matters
Networks fail. Your server may send a charge and lose the connection before the response arrives. You cannot tell whether the charge was made, and sending it again could charge your customer twice.
An idempotency key solves this. Send the same key with the retry, and Wire Gh returns the result of the first request instead of running it again.
How to use it
Add an Idempotency-Key header to any POST request. Use a value that is unique to the action, such as your order number plus an attempt number.
curl https://api.wiregh.com/charge \
-H "Authorization: Bearer YOUR_SECRET_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: order-1042-attempt-1" \
-d '{ "email": "customer@email.com", "amount": 5000, "mobile_money": { "phone": "0551234987", "provider": "mtn" } }' \
-X POSTconst response = await fetch('https://api.wiregh.com/charge', {
method: 'POST',
headers: {
Authorization: 'Bearer ' + process.env.WIREGH_SECRET_KEY,
'Content-Type': 'application/json',
'Idempotency-Key': 'order-1042-attempt-1'
},
body: JSON.stringify({
email: 'customer@email.com',
amount: 5000,
mobile_money: { phone: '0551234987', provider: 'mtn' }
})
});<?php
$ch = curl_init('https://api.wiregh.com/charge');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode([
'email' => 'customer@email.com',
'amount' => 5000,
'mobile_money' => ['phone' => '0551234987', 'provider' => 'mtn'],
]),
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('WIREGH_SECRET_KEY'),
'Content-Type: application/json',
'Idempotency-Key: order-1042-attempt-1',
],
]);
$result = json_decode(curl_exec($ch), true);- Keys are 8 to 80 characters long, using letters, digits, underscores, hyphens, colons or full stops.
- Keys are remembered for 24 hours, separately for test and live mode.
- A replayed response has the header
Idempotent-Replayed: true.
What happens on a retry
| You send | We do |
|---|---|
| The same key and the same body, after the first request finished | Return the first response again. Nothing new happens. |
| The same key while the first request is still running | Return 409 with code idempotency_conflict. Wait a moment and retry. |
| The same key with a different body | Return 409 with code idempotency_conflict. Use a new key for a new action. |
The same key after the first request got a 500 or 503 error | Run the request again, because the first one did not complete. |
Sending your own reference gives a second layer of protection: a reference can only be used once, so a duplicate request fails with duplicate_reference instead of creating a second transaction.