# Delivery API > Deliver orders from your own system. Sellstamp asks it what a customer bought, stores what it answers, and shows it to the customer. Push, callback or pull, one state machine. Page: https://sellstamp.com/docs/developers/delivery-api ## How it works 1. Create a product and choose **Delivery API** as what the customer receives. 2. Under **Store settings → Delivery API**, give the HTTPS address your system takes requests on, or choose to pull. Copy the signing secret: it is shown once. 3. When a customer pays, Sellstamp sends a signed `delivery.requested` to your address, or puts the item in the pull queue. 4. Your system answers with the content, or delivers it later through the API. The customer sees it on their order page. Each order item is delivered exactly once. Its states are `pending`, `requested`, `delivered`, `failed` and `revoked`. A paid order never fails because your system did: the item waits, you are told, and you can retry, deliver by hand or refund. The customer only ever reads “Processing”, “Delivery pending”, “Delivered”, or that the merchant will contact them. ## The request A `POST` with a JSON body and these headers: - **X-Sellstamp-Event**: delivery.requested, delivery.revoked or delivery.test - **X-Sellstamp-Signature**: t=,v1=."> — one v1 per live secret during a rotation - **Idempotency-Key**: delivery_, the same on every retry of one item ``` { "id": "01a1…", // the delivery id "event": "delivery.requested", "orderId": "…", "orderNumber": 1042, "orderItemId": "…", "productId": "…", "variantId": null, "sku": "GIFT-25", "quantity": 1, "currency": "EUR", "unitPriceCents": 2500, "locale": "en", "customerEmail": "…", // only if the store turned it on "test": false } ``` ## Answering - **200 with items**: Delivered. The content is stored encrypted and shown to the customer. - **202, or 200 with {"status":"accepted"}**: Accepted. Deliver it later through the callback. - **Anything else**: A failed attempt: a timeout, a 4xx or 5xx, invalid JSON, invalid content, or more than 64 KB. Sellstamp retries with backoff (8 attempts over about 12 hours, or 12 over about 2 days), then marks it failed and tells the merchant. ``` { "items": [ { "type": "key", "value": "AAAA-BBBB-CCCC" }, { "type": "link", "url": "https://…", "label": "Download", "expiresAt": "2026-12-01T00:00:00Z" }, { "type": "credentials", "username": "…", "password": "…", "note": "…" }, { "type": "text", "value": "Instructions …" } ] } ``` Content is always shown as text, never as HTML, and links must be https. Five failures in a row pause requests to your address for five minutes; they resume on their own. ## Verify the signature Reject a request whose timestamp is more than five minutes old, then compare in constant time. ``` import { createHmac, timingSafeEqual } from "node:crypto"; function verify(header, rawBody, secret) { const parts = header.split(",").map((p) => p.split("=")); const t = Number(parts.find(([k]) => k === "t")?.[1]); if (!t || Math.abs(Date.now() / 1000 - t) > 300) return false; const want = Buffer.from(createHmac("sha256", secret).update(`${t}.${rawBody}`).digest("hex")); return parts.some(([k, v]) => k === "v1" && v.length === want.length && timingSafeEqual(Buffer.from(v), want)); } ``` ``` import hmac, hashlib, time def verify(header, raw_body: bytes, secret: str) -> bool: parts = [p.split("=", 1) for p in header.split(",")] t = next((v for k, v in parts if k == "t"), None) if not t or abs(time.time() - int(t)) > 300: return False want = hmac.new(secret.encode(), f"{t}.".encode() + raw_body, hashlib.sha256).hexdigest() return any(k == "v1" and hmac.compare_digest(v, want) for k, v in parts) ``` ``` function verify(string $header, string $rawBody, string $secret): bool { $parts = array_map(fn($p) => explode("=", $p, 2), explode(",", $header)); $t = null; foreach ($parts as [$k, $v]) if ($k === "t") $t = (int) $v; if (!$t || abs(time() - $t) > 300) return false; $want = hash_hmac("sha256", "$t.$rawBody", $secret); foreach ($parts as [$k, $v]) if ($k === "v1" && hash_equals($want, $v)) return true; return false; } ``` ``` func verify(header string, rawBody []byte, secret string) bool { var t int64 var sigs []string for _, p := range strings.Split(header, ",") { kv := strings.SplitN(p, "=", 2) if len(kv) != 2 { continue } if kv[0] == "t" { t, _ = strconv.ParseInt(kv[1], 10, 64) } if kv[0] == "v1" { sigs = append(sigs, kv[1]) } } if t == 0 || math.Abs(float64(time.Now().Unix()-t)) > 300 { return false } mac := hmac.New(sha256.New, []byte(secret)) fmt.Fprintf(mac, "%d.", t) mac.Write(rawBody) want := hex.EncodeToString(mac.Sum(nil)) for _, s := range sigs { if hmac.Equal([]byte(s), []byte(want)) { return true } } return false } ``` ``` static boolean verify(String header, String rawBody, String secret) throws Exception { long t = 0; List sigs = new ArrayList<>(); for (String p : header.split(",")) { String[] kv = p.split("=", 2); if (kv.length != 2) continue; if (kv[0].equals("t")) t = Long.parseLong(kv[1]); if (kv[0].equals("v1")) sigs.add(kv[1]); } if (t == 0 || Math.abs(Instant.now().getEpochSecond() - t) > 300) return false; Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256")); byte[] want = HexFormat.of().formatHex(mac.doFinal((t + "." + rawBody).getBytes(StandardCharsets.UTF_8))).getBytes(); for (String s : sigs) if (MessageDigest.isEqual(s.getBytes(), want)) return true; return false; } ``` ## Deliver later, or pull Both need an API key with the `orders.deliver` scope, limited to your stores or products if you like. An item outside the key’s reach is a 404. Send an `Idempotency-Key` with every delivery: the same content again is the same delivery, different content after a delivery is a 409, and anything after a refund is a 409. ``` curl -X POST "$API/api/v1/public/orders/$ORDER_ID/items/$ITEM_ID/delivery" \ -H "Authorization: Bearer $KEY" -H "Idempotency-Key: my-$ITEM_ID" \ -H "content-type: application/json" \ -d '{"items":[{"type":"key","value":"AAAA-BBBB-CCCC"}]}' ``` ``` curl "$API/api/v1/public/deliveries?limit=25" -H "Authorization: Bearer $KEY" curl -X POST "$API/api/v1/public/deliveries/$ID/claim" -H "Authorization: Bearer $KEY" # 60-second lease curl -X POST "$API/api/v1/public/deliveries/$ID/deliver" -H "Authorization: Bearer $KEY" \ -H "Idempotency-Key: pull-$ID" -H "content-type: application/json" -d '{"items":[…]}' curl -X POST "$API/api/v1/public/deliveries/$ID/release" -H "Authorization: Bearer $KEY" # give it back ``` Keep availability in step with `PUT /api/v1/public/products/:productId/variants/:variantId/stock` and a key with `stock.write`, which cannot edit listings. ## Refunds - A full refund or a lost chargeback takes the content off the customer's order page at once. - Sellstamp then sends a signed delivery.revoked to your address, retried like a request. It never waits for your answer. - A delivery after a refund is refused with a 409. ## Try it locally `scripts/delivery/fake-provider.mjs` in the repository is a provider that verifies signatures and answers in several ways: `instant`, `accept`, `slow`, `error`, `garbage` and `pull`. Every route is in the [API reference](https://sellstamp.com/docs/api-reference); signed outgoing events in general are described under [Webhooks](https://sellstamp.com/docs/developers/webhooks).