OpenLegion · Docsllms.txt
Docs pages

Webhooks

What happens in your organization becomes an event: a run ended, an action needs approval, a workspace was suspended. Events are sent to your webhook endpoints, signed, and kept for 30 days to read with GET /events.

With the kit

Your route's webhooks map takes one handler per event type. The route verifies each delivery's signature before your handler sees it:

export const { GET, POST, DELETE } = createNextHandler({
  template,
  webhooks: {
    // Every run that ends fires exactly one of these three.
    "run.completed": async (event) => {
      // A large run arrives as a reference, { object, id }, with no status: narrow on `truncated`.
      const run = event.data.truncated ? null : event.data.object;
      await markConversationAnswered(event.data.object?.id, run?.status);
    },
    "run.failed": async (event) => {
      await logRunFailure(event.data.object?.id); // event.data.object.error has the code
    },
    "run.cancelled": async (event) => {
      await markConversationAnswered(event.data.object?.id);
    },
  },
});
  • A large object arrives as a reference, { object: { object, id }, truncated: true }: fetch it by its id.
  • A type with no handler gets 200 {received: true, handled: false}: acknowledged and dropped. npx openlegion dev says so, once per type.
  • A handler that throws gets a 500, and OpenLegion retries. The error goes to your onError.
  • Your customers' connected accounts fire connection.created, connection.updated, connection.revoked and connection.needs_reauth. The last one means the account needs connecting again: see A connection that needs reconnecting.
  • A scheduled run that didn't start fires schedule.missed: data.object is the agent, and beside it are reason and day, the UTC date it was due (YYYY-MM-DD). The reason is plan_limit (the plan's runs for the month are used up), agent_budget (the agent's daily limit) or credits (a credit cap, or the balance). It fires at most once per agent, reason and day. A run that only waits, for credits that runs still going hold, is not missed. Nor is a run of a paused agent or a suspended workspace: you already had agent.paused or workspace.suspended.
  • The agent's browser fires computer.session_ended once when a session ends: data.object is the session as the conversation's data-computer part draws it, and beside it are session, agent, run, reason (run_ended, closed, deadline, park_expired, stopped or failed) and active_seconds.

While you develop

npx openlegion dev makes a dev webhook endpoint for your project and forwards each delivery to your running app's /api/openlegion/webhooks (or --webhook-path for another route), verified and with its signature kept. Nothing on your machine listens, and there is no tunnel.

npx openlegion listen --forward-to http://localhost:3000/your/route does the same for webhooks alone, to any URL on your machine. The two share the project's dev endpoint, so starting one stops the other.

Retries, and when you were down

A delivery that doesn't get a 2xx within 5 seconds is retried after 1 min, 5 min, 30 min, 2 h, 6 h, 12 h, 24 h and 24 h: 9 attempts over about 2.9 days, each with the same event id. Dedupe on the event id.

While your app or npx openlegion dev is down, deliveries fail and wait in that schedule. GET /events has every event either way (newest first, filters type, workspace and created), so a receiver that was down can reconcile. An endpoint is disabled only after 20 failures in a row spanning at least 72 hours.

GET /webhook_endpoints/{id}/deliveries lists an endpoint's deliveries for the last 30 days, newest first, each with its attempts. POST /webhook_endpoints/{id}/deliveries/{delivery_id}/resend sends a settled one again, once, now: the same event and bytes, with a new signature.

Your endpoint, deployed

For a deployed app, make an endpoint once, with a console key that has the webhooks scope:

const endpoint = await ol.webhookEndpoints.create({
  url: "https://your.app/api/openlegion/webhooks",
  events: ["run.completed", "run.failed", "run.cancelled", "action.requires_approval"],
});
// endpoint.secret is shown once: it is your OPENLEGION_WEBHOOK_SECRET.
  • The URL must be https, on port 443, with a public host name.
  • An endpoint and its events belong to one mode: a test endpoint receives only test events.
  • POST /webhook_endpoints/{id}/test sends a ping to that endpoint alone.
  • A lost secret is replaced with ol.webhookEndpoints.rotateSecret(id, { expires_in_seconds: 0 }). Without 0, the old secret keeps signing beside the new one for a day, so you can deploy the new one first.

Verifying a delivery yourself

The kit's route does this for you. Outside it:

import { webhooks } from "@openlegion/platform";

// The raw body, exactly as received.
const event = await webhooks.constructEvent(await req.text(), req.headers, process.env.OPENLEGION_WEBHOOK_SECRET!);

constructEvent throws SignatureVerificationError, whose reason says what was wrong. The header is OpenLegion-Signature: t=<unix>,v1=<hex HMAC-SHA256(secret, "<t>.<raw body>")>, with a 5-minute tolerance. While a rotation's two secrets are live, it carries one v1= for each.