Create Purchase
Atomically purchases one or more units of an ACTIVE product from the owning user's balance.
- Protected by stricter purchase rate limits (
10/minper key,20/minper user) and fraud velocity/spend-spike checks. - Acquires a PostgreSQL
SELECT ... FOR UPDATEwallet row lock, verifies key caps (perRequestCap,monthlySpendCap) and live balance, debits funds, reserves inventory, writes an append-only ledger entry, and returns the delivered order synchronously. - Strict request schema (
additionalProperties: false): client-supplied prices or unknown fields are rejected with400 VALIDATION_ERROR.
- cURL
- JavaScript (fetch)
- Python (requests)
- PHP
curl -sS -X POST "https://api.example.com/api/v1/purchases" \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"productId": "prod_win11_pro",
"quantity": 1,
"idempotencyKey": "reseller-order-0001"
}'
const res = await fetch("https://api.example.com/api/v1/purchases", {
method: "POST",
headers: {
"x-api-key": process.env.GPKEYS_API_KEY ?? "YOUR_API_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({
"productId": "prod_win11_pro",
"quantity": 1,
"idempotencyKey": "reseller-order-0001"
}),
});
const data = await res.json();
if (!res.ok) {
throw new Error(`HTTP ${res.status}: ${data?.error?.code ?? "UNKNOWN"} - ${data?.error?.message}`);
}
console.log(data);
import os
import requests
headers = {
"x-api-key": os.environ.get("GPKEYS_API_KEY", "YOUR_API_KEY"),
"Content-Type": "application/json",
}
payload = {
"productId": "prod_win11_pro",
"quantity": 1,
"idempotencyKey": "reseller-order-0001"
}
resp = requests.post("https://api.example.com/api/v1/purchases", json=payload, headers=headers, timeout=15)
if resp.status_code == 402:
raise RuntimeError(f"Insufficient balance: {resp.json()}")
resp.raise_for_status()
print(resp.json())
<?php
$apiKey = getenv('GPKEYS_API_KEY') ?: 'YOUR_API_KEY';
$payload = <<<'JSON'
{
"productId": "prod_win11_pro",
"quantity": 1,
"idempotencyKey": "reseller-order-0001"
}
JSON;
$ch = curl_init('https://api.example.com/api/v1/purchases');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_TIMEOUT => 15,
CURLOPT_HTTPHEADER => [
'x-api-key: ' . $apiKey,
'Content-Type: application/json',
],
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "HTTP {$status}\n{$body}\n";
Request Body (application/json)
| Field | Type | Required | Constraints | Description |
|---|---|---|---|---|
productId | string | Yes | 1–64 chars | Active product ID to purchase. |
quantity | integer | No | 1–1000, default 1 | Unit quantity. Server computes priceUsdt * quantity. |
idempotencyKey | string | Yes | 8–128 chars (trimmed) | Unique key per purchase attempt so safe retries do not double-charge. |
Success Responses
First Execution (201 Created)
{
"order": {
"id": "ord_mock_001",
"orderNumber": "ORD-20261130-1001",
"apiKeyId": "key_mock_01",
"status": "DELIVERED",
"total": "12.000000",
"currency": "USDT",
"paidAt": "2026-11-30T18:10:00.000Z",
"deliveredAt": "2026-11-30T18:10:00.000Z",
"createdAt": "2026-11-30T18:10:00.000Z",
"items": [
{
"id": "item_mock_001",
"productId": "prod_win11_pro",
"title": "Windows 11 Pro Retail Key",
"unitPrice": "12.000000",
"quantity": 1,
"lineTotal": "12.000000",
"deliveryType": "LICENSE_KEY",
"deliveredPayload": {
"type": "LICENSE_KEY",
"key": "WIN11-MOCK-KEY-0001"
},
"deliveredInstructions": null,
"deliveredAt": "2026-11-30T18:10:00.000Z"
}
]
},
"created": true,
"apiKeyId": "key_mock_01",
"balance": "88.000000"
}
Idempotent Replay (200 OK)
Re-sending the same idempotencyKey returns "created": false with the original order and charges nothing.
Error Responses
| HTTP | error.code | Meaning |
|---|---|---|
400 | VALIDATION_ERROR | Invalid fields or unknown properties in request JSON |
401 | UNAUTHORIZED | Invalid, revoked, or expired key |
402 | INSUFFICIENT_BALANCE | Balance lower than order total; zero funds deducted |
403 | FORBIDDEN | Disabled key, suspended user, or idempotencyKey owned by another user |
403 | API_KEY_CAP_EXCEEDED | Exceeds key perRequestCap or monthlySpendCap |
403 | FRAUD_DETECTED | Purchase burst (RAPID_REPEAT_PURCHASE) or unusual SPENDING_SPIKE |
409 | PRODUCT_UNAVAILABLE / OUT_OF_STOCK | Product disabled or insufficient inventory |
429 | RATE_LIMITED | Exceeded 10/min key or 20/min user purchase rate limit |
500 | INTERNAL_ERROR | Unexpected failure (transaction rolled back cleanly) |
{
"error": {
"code": "INSUFFICIENT_BALANCE",
"message": "Insufficient balance",
"details": {
"available": "4.000000",
"required": "12.000000"
}
}
}