---
title: Webhooks and SSE
subtitle: 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.
description: Documenting webhooks (OpenAPI 3.1 webhooks, HOOK pages, WebhookPayloadSnippet, signature verification guidance) and server-sent events (text/event-stream, x-fern-streaming, streaming examples) in ButterIX.
---

## 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 `HOOK` badge 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 `tags` to move them to another group).

```yaml title="openapi/payments.yml"
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 backoff
```

### Show the payload in guides

```mdx
<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

<AccordionGroup>
  <Accordion title="Explain signature verification" icon="lucide:shield-check" defaultOpen>
    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.
  </Accordion>
  <Accordion title="Explain retries and idempotency" icon="lucide:repeat">
    Spell out which responses count as success, the retry interval and count, and that the event id can be used for deduplication.
  </Accordion>
  <Accordion title="Give realistic examples" icon="lucide:file-json">
    Use realistically shaped data in `example` so readers can copy it directly to test their own handlers.
  </Accordion>
</AccordionGroup>

<Info>
  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](/api-reference/git/github-webhook) — because it's an interface the platform exposes, not an event the platform sends.
</Info>

## Server-sent events (SSE)

An endpoint is treated as **streaming** when either of these is true:

- The response `content` includes `text/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](/api-reference/runtime/ask-ai) endpoint is an SSE endpoint:

<EndpointResponseSnippet endpoint="POST /_biti/ai/chat" />

The corresponding spec excerpt:

```yaml title="openapi/platform.yml"
/_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"}
```

<Tip>
  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](/api-reference/runtime/ask-ai) endpoint does it.
</Tip>

### Consume SSE in the browser

`EventSource` only supports GET. For POST SSE endpoints, read the stream with `fetch`:

```ts title="ask.ts"
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;
  }
}
```
