إنتقل إلى المحتوى الرئيسي

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/min per key, 20/min per user) and fraud velocity/spend-spike checks.
  • Acquires a PostgreSQL SELECT ... FOR UPDATE wallet 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 with 400 VALIDATION_ERROR.
POST/api/v1/purchases
Target: https://api.example.com
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"
}'

Request Body (application/json)​

FieldTypeRequiredConstraintsDescription
productIdstringYes1–64 charsActive product ID to purchase.
quantityintegerNo1–1000, default 1Unit quantity. Server computes priceUsdt * quantity.
idempotencyKeystringYes8–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​

HTTPerror.codeMeaning
400VALIDATION_ERRORInvalid fields or unknown properties in request JSON
401UNAUTHORIZEDInvalid, revoked, or expired key
402INSUFFICIENT_BALANCEBalance lower than order total; zero funds deducted
403FORBIDDENDisabled key, suspended user, or idempotencyKey owned by another user
403API_KEY_CAP_EXCEEDEDExceeds key perRequestCap or monthlySpendCap
403FRAUD_DETECTEDPurchase burst (RAPID_REPEAT_PURCHASE) or unusual SPENDING_SPIKE
409PRODUCT_UNAVAILABLE / OUT_OF_STOCKProduct disabled or insufficient inventory
429RATE_LIMITEDExceeded 10/min key or 20/min user purchase rate limit
500INTERNAL_ERRORUnexpected failure (transaction rolled back cleanly)
{
"error": {
"code": "INSUFFICIENT_BALANCE",
"message": "Insufficient balance",
"details": {
"available": "4.000000",
"required": "12.000000"
}
}
}