Idempotency keys and request timestamps
Overview
Networks fail. A call that places an order or moves money can time out after DriveWealth has already processed it. If you send the call again, you could create a second order or a second deposit.
Two optional headers protect you from that. Used together, they protect a retry for days after the first request. Close to the 4-day limit, check the current state before you retry.
| Header | Its job |
|---|---|
Idempotency-Key | Recognizes a retry. DriveWealth returns the response to the first request instead of running the operation again. |
dw-request-timestamp | Records when you first created the request. DriveWealth rejects a request that is too old, so a late replay cannot run as a new request. |
Send both headers on every request that places an order or moves moneyCreate the key and the timestamp once, when you first create the request. Reuse the same two values on every retry of that request.
The two headers
Read how each header behaves before you add it to your code.
Idempotency-Key
The key identifies one operation. If DriveWealth sees the same key again, it knows the request is a retry.
- You send a request with an
Idempotency-Keyheader. - DriveWealth processes the request and saves the response with the key.
- If the same key arrives again, DriveWealth returns the saved response. It does not run the operation a second time.
- Keys are kept for 4 days. After that, a request with the same key is treated as a new request.
| Rule | What it means |
|---|---|
| The key is optional. | Without it, a retry is a new request. Always send one for orders and money movement. |
| Use a UUID v4. | A key shorter than 16 characters is rejected. |
| Use one key for each operation. | Make a new key for each new order, deposit, or withdrawal. |
| Reuse a key only to retry the same request. | Send the same request again, with the same body. |
| Never reuse a key for a different request. | A different request that carries an old key may not run. The API may return the response to the first request instead of an error. |
Do not send a key on GET requests. | GET requests do not change anything. |
dw-request-timestamp
The timestamp tells DriveWealth when you first created the request. DriveWealth checks how old the request is before it runs.
| Rule | What it means |
|---|---|
| The header is optional. | Without it, DriveWealth does not check the age of the request. |
| Use an ISO-8601 UTC time. | For example, 2026-11-24T15:04:05.123Z. |
| Set it once, when you first create the request. | Send the same value on every retry. Do not refresh it. |
| A request older than 4 days is rejected. | DriveWealth does not run it. You get a 400 response. This is the same 4 days that a key is kept. |
| A time more than 5 minutes ahead is rejected. | Compared with DriveWealth's clock. You get a 400 response. Keep your servers' clocks in sync. |
| Use the real time you created the request. | Do not set the time ahead to allow for clock differences. A time that is ahead makes a stale retry pass the age check for that much longer. |
| It does not replace the key. | The timestamp checks age. Only the key recognizes a retry. |
How the two work together
The key protects a retry for 4 days. The timestamp protects it after that. Here is the same request retried at different times.
| When you retry | What you send | What happens |
|---|---|---|
| Within 4 days of the first request | The same key and timestamp | DriveWealth returns the saved response. The operation does not run a second time. |
| More than 4 days later | The same key and timestamp | The key has expired, and the timestamp is older than 4 days. DriveWealth rejects the request, so nothing runs. Do not rely on this in the first minutes after the 4 days end. See the note below the table. |
| After 4 days | The same key, no timestamp | The key has expired and nothing checks the age. The request runs as a new request. An order or a money movement can happen twice. |
| At any time | No key | The request is a new request. The timestamp cannot tell that it is a retry. |
This is why a strong integration sends both. The key handles the common case. The timestamp handles the case where a queue, a manual replay, or a long outage sends an old request days later.
The two 4-day limits start at different points. The key's limit starts when DriveWealth first receives the request. The timestamp's limit starts at the time you set. If your timestamp is a few minutes ahead of the real time, a retry just after the key expires can still pass the age check and run as a new request. Do not count on a rejection near the 4-day limit. Check the current state first, as described in Check the current state.
The recommended pattern
- Create the key and the timestamp once, at the moment you first create the request: a new UUID v4 and the current UTC time.
- Save both with your own record of the operation, before you send anything. After a crash or restart, you can then retry with the same values.
- Send both headers on every attempt, with the same request body.
- Retry with the same key and timestamp after a timeout, a network error, a
429, or a5xx. Wait between attempts with exponential backoff and jitter, and stop after a fixed number of attempts. - Never replace the key or the timestamp to "try again". A new key tells DriveWealth that this is a new operation. If the first attempt did go through, you now have two.
- When you are not sure what happened, check the current state before you send anything again. See Check the current state.
- For a new operation, create a new key and a new timestamp.
- If you send requests through a queue, store the key and the timestamp in each message when you create it. A consumer that sends the message days later is then rejected instead of running the request a second time.
Send both headers
This example calls POST /funding/deposits to create a deposit. Put your request body in the DEPOSIT_JSON shell variable. Create the key and the timestamp once, and reuse both if you retry.
KEY=$(uuidgen | tr 'A-Z' 'a-z')
CREATED_AT=$(date -u +"%Y-%m-%dT%H:%M:%S.000Z")
curl --location 'https://bo-api.drivewealth.io/back-office/funding/deposits' \
--header 'Authorization: Bearer YOUR_ACCESS_TOKEN' \
--header 'dw-client-app-key: YOUR_APP_KEY' \
--header 'Content-Type: application/json' \
--header "Idempotency-Key: $KEY" \
--header "dw-request-timestamp: $CREATED_AT" \
--data "$DEPOSIT_JSON"Retry with the same key and timestamp
Create both values once, outside your retry loop. Retry on a timeout, a network error, a 429, or a 5xx, and send the same values each time.
import { randomUUID } from "node:crypto";
async function createWithRetry(url, body, headers, { maxAttempts = 5, baseMs = 1000, capMs = 60000 } = {}) {
// Create the key and the timestamp once. In production, save both with your own record first.
const key = randomUUID();
const createdAt = new Date().toISOString();
let delay = baseMs;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
const response = await fetch(url, {
method: "POST",
headers: {
...headers,
"Content-Type": "application/json",
"Idempotency-Key": key,
"dw-request-timestamp": createdAt,
},
body: JSON.stringify(body),
});
if (response.status !== 429 && response.status < 500) return response;
} catch (error) {
// Network error or timeout. The request may have been processed. Retry with the same key.
}
if (attempt === maxAttempts) break;
await new Promise((resolve) => setTimeout(resolve, delay / 2 + Math.random() * (delay / 2)));
delay = Math.min(delay * 2, capMs);
}
throw new Error("Retries exhausted. Check the current state before you send anything again.");
}import random
import time
import uuid
from datetime import datetime, timezone
import requests
def create_with_retry(url, body, headers, max_attempts=5, base=1.0, cap=60.0):
# Create the key and the timestamp once. In production, save both with your own record first.
key = str(uuid.uuid4())
created_at = datetime.now(timezone.utc).isoformat(timespec="milliseconds").replace("+00:00", "Z")
delay = base
for attempt in range(1, max_attempts + 1):
try:
response = requests.post(
url,
json=body,
headers={**headers, "Idempotency-Key": key, "dw-request-timestamp": created_at},
timeout=30,
)
if response.status_code != 429 and response.status_code < 500:
return response
except requests.RequestException:
pass # Network error or timeout. The request may have been processed. Retry with the same key.
if attempt == max_attempts:
break
time.sleep(random.uniform(delay / 2, delay))
delay = min(delay * 2, cap)
raise RuntimeError("Retries exhausted. Check the current state before you send anything again.")When the loop gives up, do not start a new operation with a new key. Check the current state first.
What to do in each case
| What happened | What to do |
|---|---|
| The call timed out or the connection dropped. | Retry soon with the same key and timestamp. If the first call was processed, you get its response. |
You got a 429 or a 5xx. | Wait with exponential backoff and jitter, then retry with the same key and timestamp. |
You got a 4xx because of your input, such as 400 or 422. | Fix the request, then send it with a new key and a new timestamp. |
| The key is shorter than 16 characters. | The request is rejected. Use a longer key, such as a UUID v4. |
| The first request is still running. | Wait a moment and retry with the same key and timestamp. If the problem continues, check the current state before you use a new key. |
| Your timestamp is rejected as too old. | The request is more than 4 days old. Do not resend it. Check the current state to see whether it ran. |
| Your timestamp is rejected as ahead of the clock. | Fix the clock on your server, or create the timestamp again for a request that you have not sent yet. |
Do not use a new key to "try again" after an unclear resultA new key tells DriveWealth that this is a new operation. If the first attempt did go through, you now have two orders, or two deposits. When you are not sure what happened, check the current state first. Do not resend automatically while the outcome is unknown.
Check the current state
If you lost the response and cannot retry with the key, look up what happened before you send anything again. This applies when the request is more than 4 days old, when you did not send a key, and when retries keep failing.
Start with the identifiers you already recorded: your own reference for the operation, and any ID that an earlier response gave you. If the response was lost, you may not have an orderID or an orderNo, because DriveWealth returns them in the response. In that case, list the account's activity instead.
| Operation | Where to look |
|---|---|
| Order | List open orders with GET /accounts/{accountID}/summary/orders and past orders with GET /accounts/{accountID}/reports/order-history. If you have an ID, use GET /orders/{orderID} or GET /orders/byOrderNo/{orderNo}. |
| Deposit | List deposits with GET /funding/deposits. The list requires status, type, from and to, so repeat the search for every status the deposit could have reached, because its status may have changed. If you have an ID, use GET /funding/deposits/{depositID}. |
| Withdrawal | List withdrawals with GET /funding/redemptions. The list requires status, type, from and to, so repeat the search for every status the withdrawal could have reached, because its status may have changed. If you have an ID, use GET /funding/redemptions/{redemptionID}. |
- Match what you find to your own record, such as the account, the amount, and the time.
- If you find the operation, do not send it again. Follow its status.
- If you do not find it, wait a short time and look again. A request that is still in progress may not show yet, and an empty result does not prove that the request will not complete later.
- If it is still missing, treat the outcome as unknown. Do not send the request again with a new key and a new timestamp. A new key is not recognized as a retry, and the original request can still complete and create a duplicate order or money movement.
- Contact DriveWealth support with your reference, the account, the amount, and the time. Ask them to confirm what happened to the original request. Send a replacement only after you have that confirmation.
Orders are processed asynchronously, so follow an order's status with DriveWealth events or by retrieving it.
Updated 2 days ago