Webhooks and SSE
Describe the events you send to users with OpenAPI 3.1 webhooks and streaming responses with text/event-stream, and ButterIX generates dedicated pages and examples for them.
Webhooks
OpenAPI 3.1 adds a top-level webhooks field for describing requests that your service sends to users. ButterIX generates a page for each webhook:
- The sidebar and title use a
HOOKbadge instead of an HTTP method; - The main body of the page is a Payload field table instead of request parameters;
- The right side shows a payload example, with no "Try it" button;
- Webhooks go into a "Webhooks" group by default (use
tagsto move them to another group).
openapi: 3.1.0
webhooks:
payment.succeeded:
post:
summary: Payment succeeded
x-fern-sdk-method-name: payment-succeeded
description: Sent when a payment's status becomes succeeded. Verify the source with the `Acme-Signature` header.
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/PaymentEvent"
example:
id: evt_1Q2w3E
type: payment.succeeded
created: 1763596800
data:
id: pay_9xK2
amount: 1999
currency: cny
responses:
"200":
description: Return 2xx to acknowledge receipt; otherwise the event is retried with exponential backoffShow the payload in guides
<WebhookPayloadSnippet webhook="payment.succeeded" />webhook can be the webhook name, an operationId, or a key of the form WEBHOOK payment.succeeded; example selects a named example.
Tips for good webhook docs
Explain signature verification
Give the signature algorithm and verification code for each language in the webhook's description or on a separate guide page. Put the verification code in a <CodeGroup> and the reader's language choice stays in sync with the API examples.
Explain retries and idempotency
Spell out which responses count as success, the retry interval and count, and that the event id can be used for deduplication.
Give realistic examples
Use realistically shaped data in example so readers can copy it directly to test their own handlers.
The ButterIX platform itself receives webhooks from GitHub (for automatic deployments). In this site's API reference that is an ordinary endpoint — Receive GitHub webhook — because it's an interface the platform exposes, not an event the platform sends.
Server-sent events (SSE)
An endpoint is treated as streaming when either of these is true:
- The response
contentincludestext/event-stream; - The operation has
x-fern-streaming: true.
A streaming endpoint's page notes that it "returns an event stream", and response examples are shown in event-stream format. This site's Ask AI chat endpoint is an SSE endpoint:
data: "event: sources\ndata: [{\"n\":1,\"title\":\"Internationalization\",\"url\":\"/guides/features/i18n\"}]\n\nevent: text\ndata: {\"t\":\"Add en to languages in docs.yml [1], \"}\n\nevent: text\ndata: {\"t\":\"then put the translations under translations/en/.\"}\n\nevent: done\ndata: {\"model\":\"claude-opus-5-5\",\"unanswered\":true}\n"{
"error": "string",
"code": "string"
}{
"error": "string",
"code": "string"
}{
"error": "string",
"code": "string"
}{
"error": "string",
"code": "string"
}{
"error": "string",
"code": "string"
}{
"error": "string",
"code": "string"
}The corresponding spec excerpt:
/_biti/ai/chat:
post:
summary: Ask AI chat (SSE)
responses:
"200":
description: Event stream
content:
text/event-stream:
schema:
type: string
example: |
event: sources
data: [{"n":1,"title":"Internationalization","url":"/guides/features/i18n"}]
event: text
data: {"t":"Add en to languages in docs.yml [1], "}
event: done
data: {"model":"claude-opus-5-5"}In the description, use a table listing each event's name and data structure — that table is what readers need most. See how the Ask AI chat endpoint does it.
Consume SSE in the browser
EventSource only supports GET. For POST SSE endpoints, read the stream with fetch:
const output = document.querySelector("#answer")!;
const res = await fetch("/_biti/ai/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ messages: [{ role: "user", content: "How do I add an English version?" }], locale: "en" }),
});
const reader = res.body!.pipeThrough(new TextDecoderStream()).getReader();
let buffer = "";
for (;;) {
const { value, done } = await reader.read();
if (done) break;
buffer += value;
const events = buffer.split("\n\n");
buffer = events.pop()!;
for (const raw of events) {
const event = /^event: (.+)$/m.exec(raw)?.[1];
const data = JSON.parse(/^data: (.+)$/m.exec(raw)?.[1] ?? "null");
if (event === "text") output.textContent += data.t;
}
}