Notifications reference
Notifications
Nostrad tells your server about each purchase, revoked purchase and refund request with a POST (a refund that leaves access in place sends nothing) to your notifications address, set under Monetize in the console: https://, on port 443 or 1024 and up, on the public internet. Answer 200 to 206 within 15 seconds. Redirects are never followed.
{ "version": "1.0", "notification_id": "6f1c...", "app": "app_...", "environment": "production",
"event_time": 1793000000000, "type": "ONE_TIME_PRODUCT_PURCHASED",
"purchase_token": "pt_...", "order_id": "NOS.1234-5678-9012-34567", "product": "coins_100" }
type | When |
|---|---|
ONE_TIME_PRODUCT_PURCHASED | Someone bought a product |
ONE_TIME_PRODUCT_CANCELED | A purchase was taken back in full |
VOIDED_PURCHASE | A refund that takes back what was bought, with refund_type (full or partial) and, for some of the quantity, voided_quantity |
REFUND_REQUESTED | The buyer asked Nostrad for a refund: send your answer |
REFUND_DECLINED | Nostrad declined the buyer's refund request |
SUBSCRIPTION_* | Each step of a subscription's life: the list |
TEST | Sent by Send test notification or POST /v1/notifications/test |
Each is signed as job webhooks are, with the notification's id where a webhook has the job's:
| Header | What it is |
|---|---|
X-Nostrad-Notification-Id | The notification's id, the same as notification_id |
X-Nostrad-Webhook-App-Id | Your App ID |
X-Nostrad-Webhook-Timestamp | When it was sent, in seconds since 1970 (UTC) |
X-Nostrad-Webhook-Signature | Ed25519, in hex, over the notification id, a newline, the App ID, a newline, the timestamp, a newline, and the SHA-256 of the raw body as 64 lower-case hex characters |
// Checks a Nostrad purchase notification. Web Crypto only: Cloudflare Workers, Deno, Node 20 or later.
const MY_APP_ID = "<your App ID>";
const hex = (b) => [...new Uint8Array(b)].map((x) => x.toString(16).padStart(2, "0")).join("");
const bytes = (h) => new Uint8Array(h.match(/../g).map((x) => parseInt(x, 16)));
let keys = null, fetchedAt = 0; // the public keys: fetched once, again only when none matches (at most every 5 minutes)
async function verifies(signature, message) {
for (const k of keys ?? []) {
const key = await crypto.subtle.importKey("jwk", { kty: k.kty, crv: k.crv, x: k.x }, { name: "Ed25519" }, false, ["verify"]);
if (await crypto.subtle.verify({ name: "Ed25519" }, key, bytes(signature), message)) return true;
}
return false;
}
async function nostradNotification(request) {
const id = request.headers.get("x-nostrad-notification-id");
const appId = request.headers.get("x-nostrad-webhook-app-id");
const timestamp = request.headers.get("x-nostrad-webhook-timestamp");
const signature = request.headers.get("x-nostrad-webhook-signature");
const raw = await request.text();
if (!id || appId !== MY_APP_ID || !timestamp || !/^[0-9a-f]{128}$/.test(signature ?? "")) return null;
if (Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) return null;
const digest = hex(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(raw)));
const message = new TextEncoder().encode(`${id}\n${appId}\n${timestamp}\n${digest}`);
if (!(await verifies(signature, message))) {
if (Date.now() - fetchedAt < 300_000) return null; // not from Nostrad: answer 400
fetchedAt = Date.now();
keys = (await (await fetch("https://gateway.nostrad.ai/.well-known/nostrad-webhooks.json")).json()).keys;
if (!(await verifies(signature, message))) return null;
}
const n = JSON.parse(raw);
return n.notification_id === id && n.app === appId ? n : null; // then check the purchase with GET /v1/purchases/<token>
}
- A notification your server doesn't answer with 200 to 206 is sent again 1 hour, 12 hours, 24 hours, 48 hours and 72 hours after the try before, then given up. A test-environment notification is tried once.
- The same notification can arrive more than once: act on each
notification_idonce. - A notification says something happened; the purchase itself is the truth. Check it with
GET /v1/purchases/<token>before acting. GET /v1/notificationslists the last 180 days, oldest first, 20 a page (startandendin milliseconds,typeorpurchase_token,cursorfromnext_cursor): each withnotification(the body),delivered,pending,gave_upand itsattempts(when, your server's status, what went wrong).POST /v1/notifications/testanswers{ "test_token": "nt_..." };GET /v1/notifications/test/<test_token>says whether it arrived. With no address set it answers 409no_notify_url.