ChartnautDocs

Webhooks

Webhooks tell your server that something changed: a user linked or unlinked, a session ended, a rule broke. They name it by type and id; read the object with the API before you act on it. Deliveries come at least once, in no set order, and can be late.

Add endpoints on your project's Webhooks tab in Developers. Each has its own signing secret. Put it in your server's environment as CHARTNAUT_WEBHOOK_SECRET.

Check the signature

Verify the raw body, before anything parses it:

// app/api/chartnaut/webhooks/route.ts
import { ChartnautWebhookError, verifyRequest } from "@chartnaut/node";

export async function POST(req: Request) {
  try {
    const event = await verifyRequest(req, process.env.CHARTNAUT_WEBHOOK_SECRET!);
    if (event.type === "session.ended") await journal(String(event.data.session));
    return new Response(null, { status: 204 });
  } catch (e) {
    if (e instanceof ChartnautWebhookError) return new Response(e.message, { status: 400 });
    throw e;
  }
}

With Express, mount express.raw() on the webhook route. express.json() throws the original bytes away, and the signature is over those bytes.

import express from "express";
import { Chartnaut } from "@chartnaut/node";

const cn = new Chartnaut({ webhookSecret: process.env.CHARTNAUT_WEBHOOK_SECRET });
const app = express();

app.post("/webhooks/chartnaut", express.raw({ type: "application/json" }), async (req, res) => {
  const event = cn.webhooks.verify(req.body, req.get("chartnaut-signature"));
  if (event.type === "user.disconnected") await cn.frames.forget({ user: String(event.data.user) });
  res.sendStatus(204);
});

The signature header is Chartnaut-Signature: t=<unix>,v1=<hex>: an HMAC-SHA256 of "<t>.<raw body>" with your endpoint's secret. t must be within 5 minutes of your clock. While you rotate a secret, deliveries are signed with both, and either matches.

Events

Type

Means

user.connected, user.disconnected, user.deleted

A user linked, unlinked or deleted their account

session.ended

A partner session ended

rule.breached

A session's or account's rule broke

alert.fired

A linked user's own Chartnaut alert went off (with alerts:read)

test.ping

Send test event on the Webhooks tab

Webhooks in the partner API has each event's data, the retry schedule and the delivery log.

Deduplicate on event.id. Answer with any 2xx within 10 seconds; anything else is retried for about a week. An endpoint that fails 25 times in a row over more than an hour is paused: its events wait up to 3 days, and Resume on the Webhooks tab starts it again.

Over your plan's endpoint limit

Live endpoints count against your plan across all your projects. Test endpoints count against nothing. When a downgrade leaves you with more live endpoints than your plan allows:

  1. Nothing stops at once. You get 7 days, an email, and a banner on the Webhooks tab saying the date.

  2. When the 7 days end, your oldest endpoints up to the limit keep delivering. The rest pause with the reason over_plan_limit. Their events wait 3 days, and Send test event still reaches them.

  3. Choose which to keep on the Webhooks tab picks the endpoints that stay active instead, at any time, during the 7 days too. In Developers it lists the endpoints of all your projects; on partners.chartnaut.com, the one project you are in.

  4. Resume on an endpoint paused over the limit works only when there is room: otherwise it is refused (409) with how many your plan allows and how many are delivering.

  5. Upgrade, and the pauses lift by themselves, oldest first.

An endpoint paused because it kept failing still counts towards the limit: it is one you keep, just broken.

Events from the terminal on your page (order_filled, position_closed) are hints for your interface. Webhooks and API reads are the facts.