Developers
Guides

Guarantee idempotency

An external order never produces two orders nor two invoices: the two keys, and how to retry.

A store integration retries: a timeout during a payment, a server restarted at the wrong moment, a background job run again. QuoShop’s rule: an external order never produces two orders, nor two invoices, however many times it is sent. Two mechanisms guarantee it.

1. The natural key: source + externalId

(store, source, externalId) is a unique index in the database. Sending the same order again:

SituationResponse
same reference, same body201, Idempotent-Replayed: true, the existing order — and its ERP posting is completed if it had failed
same reference, different body409 CONFLICT, detail: { orderId, number } — nothing is created
two simultaneous requeststhe index decides; the loser gets the winner’s order

No time limit: the same order sent again six months later is still recognised.

source tells apart the systems selling on the same store: two WooCommerce sites both number their orders from 1 — give them different sources (woocommerce-site-a, woocommerce-site-b), and never change the source once orders have been sent.

2. The Idempotency-Key header

It covers the window of the request itself (response lost after the server saved everything) and applies to every creation — orders, payments, customers:

POST/orders

# One key per operation, reused as is on every retry.
curl -X POST 'https://quosuite.com/api/public/v1/quoshop/orders' \
  -H "Authorization: Bearer $QUOSHOP_API_KEY" \
  -H 'Idempotency-Key: 3f1c2a9e-5b7d-4e8a-9c1f-2d6b8e4a7c30' \
  -H 'Content-Type: application/json' \
  -d '{
  "externalId": "10293",
  "source": "woocommerce",
  "customer": {
    "email": "client@example.com"
  },
  "lines": [
    {
      "sku": "ART-0042",
      "quantity": 1
    }
  ],
  "payment": {
    "status": "PENDING",
    "method": "COD"
  }
}'
SituationResponse
first requestprocessed, Idempotent-Replayed: false
same key, same body, first one finished (24 h)response replayed, Idempotent-Replayed: true
same key, first one in progress409 IDEMPOTENCY_IN_PROGRESS, Retry-After: 1
same key, other body422 IDEMPOTENCY_KEY_REUSED
first one failedthe key is released: the retry runs

A key: one UUID v4 per operation, generated once, stored with the operation, reused as is on every retry.

The retry algorithm

Texte
body = built once, stored with the order
key  = uuid4(), stored with the order
for attempt 1 to 8:
    response = POST /orders (body, Idempotency-Key: key)
    201                            → done (replayed or not); if erp.posted = false, resend later
    409 CONFLICT + detail.orderId  → the reference designates another order: report, create nothing
    422 IDEMPOTENCY_KEY_REUSED     → new key, same body: the natural key decides
    409 IDEMPOTENCY_IN_PROGRESS    → wait Retry-After, retry
    429, 503, 5xx, network         → wait 1 min, 5 min, 30 min, 2 h, 6 h, 12 h, 24 h (≥ Retry-After)
    401                            → pause: the key is no longer valid, wait for a new key
    other 4xx                      → do not retry: report the cause

The plugins’ PHP SDK implements it (QuoShop\Sdk\Sync\OrderExporter): it never sleeps and returns an outcome (EXPORTED, RETRY with a delay, REJECTED, CONFLICT, AUTH_FAILED…) that the CMS’s queue applies.

Other creations

  • POST /orders/{order}/payments — idempotent by the payment’s reference;
  • POST /customers — deduplicated by email: an existing account is returned as is;
  • POST /webhooks/endpoints — not idempotent: before creating, read back the one you have (GET /webhooks/endpoints/{id}), as the plugins do.