Skip to content
Français

Start work from webhooks

Verify incoming events and publish queue jobs for the matching workflow.

Prepare a queue and worker before exposing a webhook. The HTTP server verifies and queues a request; it does not run the agent. You also need an application rule deciding which verified senders may request work.

Use a webhook source to verify the incoming request, then route the accepted event to a queue job. serveTriggers() handles the HTTP request; workers execute the workflow after publication.

import type { TriggerRoute } from "@elie-laloum/outpost/queues";
import { labelAdded } from "@elie-laloum/outpost/queues";

const allowedActors = new Set(["github:octocat"]);
export const on: TriggerRoute["on"] = (event) => {
  if (!event.actor || !allowedActors.has(event.actor)) return undefined;
  const issue = labelAdded(event, "outpost:fix");
  if (!issue) return undefined;
  return {
    handler: "fix",
    runId: `${issue.repository}#${issue.number}`,
    input: { repository: issue.repository, issue: issue.number },
  };
};
import { createGithubWebhook } from "@elie-laloum/outpost/queues";
import { on } from "./label-job.ts";

export const secret = process.env.GITHUB_WEBHOOK_SECRET;
if (!secret) throw new Error("Set GITHUB_WEBHOOK_SECRET");
export const routes = [
  { path: "/github", source: createGithubWebhook({ secret }), on },
];
import {
  createSqliteTaskQueue,
  serveTriggers,
} from "@elie-laloum/outpost/queues";
import { routes } from "./github-route.ts";

export const queue = await createSqliteTaskQueue(".outpost/jobs.sqlite");
export const server = await serveTriggers({
  queue,
  port: 8787,
  routes,
  onError: (error, failure) => console.error(failure, error),
});
console.log(`Listening on ${server.url}`);
// Example output: Listening on http://127.0.0.1:8787

Adding the outpost:fix label to an issue or a pull request publishes a fix job to the queue. A worker runs it with defineWorkflowJob(): see Job queues and workers.

Drag to move · Ctrl + scroll to zoom
100 %
  • VerifyCheck the incoming signature before routing the event. (HTTP server)
    • → Queue : accepted event
    • → Reject : bad signature
  • QueueStore a job and answer the HTTP request. (Queue)
    • → Worker : job claimed
  • RejectReturn 401 without publishing a job. (HTTP server)
  • WorkerRun the workflow in a separate process. (Worker)

A job names a registered worker handler, a runId of at most 256 characters and an optional JSON input. Keep on() fast: GitHub waits 10 seconds for an answer, Slack 3 seconds.

Set GITHUB_WEBHOOK_SECRET for both processes and run node server.ts. In another terminal, run node send-webhook.ts: expect 202, then 401. No worker is needed for this check; the job stays in the queue. Replace github:octocat with authorized actors before connecting a real repository.

import { createHmac, randomUUID } from "node:crypto";

const secret = process.env.GITHUB_WEBHOOK_SECRET;
if (!secret) throw new Error("Set GITHUB_WEBHOOK_SECRET");
const body = JSON.stringify({
  action: "labeled",
  label: { name: "outpost:fix" },
  issue: { number: 42 },
  repository: { full_name: "acme/app" },
  sender: { login: "octocat" },
});
const signature = createHmac("sha256", secret).update(body).digest("hex");
for (const digest of [signature, "0".repeat(64)]) {
  const response = await fetch(
    `${process.env.WEBHOOK_URL ?? "http://127.0.0.1:8787"}/github`,
    {
      method: "POST",
      body,
      headers: {
        "content-type": "application/json",
        "x-github-event": "issues",
        "x-github-delivery": randomUUID(),
        "x-hub-signature-256": `sha256=${digest}`,
      },
    },
  );
  console.log(response.status);
}
SourceVerificationDelivery identifierevent.actor
createGithubWebhook({ secret })X-Hub-Signature-256, an HMAC of the body. JSON or form payloads.X-GitHub-Deliverygithub:<login>
createGitlabWebhook({ signingToken })webhook-signature with a whsec_ signing token (GitLab 19.0+), 5-minute window.webhook-idgitlab:<username>
createGitlabWebhook({ token })X-Gitlab-Token equals the token. The body is not signed.Idempotency-Key, else X-Gitlab-Event-UUIDgitlab:<username>
createSlackSource({ signingSecret })X-Slack-Signature over the timestamp and body, 5-minute window.trigger_idslack:<user id>
createStandardWebhook({ secret })Standard Webhooks whsec_ secret, 5-minute window.webhook-idnone

Prefer a GitLab signing token: a plain token travels as is in a header and does not sign the body. toleranceMs changes the 5-minute window. Slack sources accept slash commands and interactive payloads.

Two helpers recognize the common events and return undefined for everything else.

API reference: labelAdded, commandIssued and TriggerEvent.

Use the event contract when handling another kind of delivery.

API reference: TriggerEvent.

A verified signature proves the request comes from your GitHub, GitLab or Slack integration, not that its author may start a workflow. Anyone who can comment on a public repository can write /outpost: check event.actor against an explicit list.

import { commandIssued } from "@elie-laloum/outpost/queues";
import type { TriggerEvent, TriggerJob } from "@elie-laloum/outpost/queues";

const maintainers = new Set(["github:octocat", "slack:U012AB3CD"]);

function fromCommand(event: TriggerEvent): TriggerJob | undefined {
  const command = commandIssued(event, "/outpost");
  if (!command) return undefined;
  if (!event.actor || !maintainers.has(event.actor)) return undefined;
  return {
    handler: "fix",
    runId: `${command.repository ?? "slack"}#${command.number ?? event.delivery}`,
    input: { request: command.text },
  };
}

Pass fromCommand as a route’s on. event.actor is not an Outpost gate actor: approvals authenticate their deciders separately.

For this step, follow Operate a webhook server.