Nostraddeveloper documentationDocumentationDeveloper console

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" }
typeWhen
ONE_TIME_PRODUCT_PURCHASEDSomeone bought a product
ONE_TIME_PRODUCT_CANCELEDA purchase was taken back in full
VOIDED_PURCHASEA refund that takes back what was bought, with refund_type (full or partial) and, for some of the quantity, voided_quantity
REFUND_REQUESTEDThe buyer asked Nostrad for a refund: send your answer
REFUND_DECLINEDNostrad declined the buyer's refund request
SUBSCRIPTION_*Each step of a subscription's life: the list
TESTSent 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:

HeaderWhat it is
X-Nostrad-Notification-IdThe notification's id, the same as notification_id
X-Nostrad-Webhook-App-IdYour App ID
X-Nostrad-Webhook-TimestampWhen it was sent, in seconds since 1970 (UTC)
X-Nostrad-Webhook-SignatureEd25519, 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>
}