Skip to content
briefkastencloud

SDK: embedded

Run briefkasten inside your own app, in Hono, Express, Next.js or a Worker, and reuse its webhook handling.

Mounted in your app, briefkasten does the webhook work you would otherwise write yourself: it checks each provider's signature, parses the payload into a typed event, answers the provider quickly, and calls your handlers. There is no separate gateway; your server receives the webhooks itself, so it needs a public URL.

npm install briefkasten @briefkasten/adapter-github

Define the sources and handlers

import { briefkasten } from "briefkasten";
import { github } from "@briefkasten/adapter-github";
import { linear } from "@briefkasten/adapter-linear";

export const hooks = briefkasten({
  sources: {
    github: github({ secret: process.env.GITHUB_WEBHOOK_SECRET }),
    linear: linear({ secret: process.env.LINEAR_WEBHOOK_SECRET }),
  },
});

hooks.on("github:pull_request.opened", async (event) => {
  // event.body is typed: the pull request, the repository, the sender
});

The key of each source is its name, and the last part of its URL: the GitHub webhook posts to /webhooks/github.

Hono

hooks.fetch is a standard fetch handler: it takes a Request and answers a Response.

import { Hono } from "hono";
import { hooks } from "./hooks";

const app = new Hono();
app.post("/webhooks/*", (c) => hooks.fetch(c.req.raw));

Express

hooks.node is a Node request handler. Mount it before express.json(): signatures are checked on the raw body.

import express from "express";
import { hooks } from "./hooks";

const app = express();
app.use("/webhooks", hooks.node);
app.use(express.json());

Next.js

In the App Router, a catch-all route hands the request on:

// app/webhooks/[...source]/route.ts
import { hooks } from "@/lib/hooks";

export const POST = (request: Request) => hooks.fetch(request);

Cloudflare Workers

import { hooks } from "./hooks";

export default {
  fetch(request: Request) {
    const url = new URL(request.url);
    if (url.pathname.startsWith("/webhooks/")) return hooks.fetch(request);
    return new Response("not found", { status: 404 });
  },
};

What your app answers the provider

CaseAnswer
Signature checks out202, then your handlers run
Signature missing or wrong401, and no handler runs
Unknown source in the path404
Body too large413

Handlers run after the answer, so a slow handler never makes a provider time out. A handler that throws is retried with backoff, and every event can be replayed by its id while it is kept (hooks.replay(id)).

Inside your app, or through a gateway?

Embedded is the shortest path when your app has a public URL anyway. Use the client with briefkasten cloud or a self-hosted gateway when your server should have no public entry point, or when several servers and agents need the same events.