Skip to content

Commit 978aa2e

Browse files
jackbridgerTrigger.dev RepoOps
authored andcommitted
docs: add API change notifier example guide
Mono-RevId: 2b3eee37de91ef3bcec68a0fa5ff6878dc88b63a
1 parent 92ef90e commit 978aa2e

7 files changed

Lines changed: 176 additions & 0 deletions

File tree

‎docs/docs.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@
696696
"group": "Example projects",
697697
"pages": [
698698
"guides/example-projects/anchor-browser-web-scraper",
699+
"guides/example-projects/api-change-notifier",
699700
"guides/example-projects/ask-trigger-chat-agent",
700701
"guides/example-projects/attio-lead-agent",
701702
"guides/example-projects/batch-llm-evaluator",
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
---
2+
title: "API change notifier with GitHub approval"
3+
sidebarTitle: "API change notifier"
4+
description: "An AI agent that works out which API customers a merged pull request breaks, drafts a note to each of them, and sends nothing until someone merges a docs PR. Uses a hosted GitHub webhook endpoint and a webhook waiter."
5+
---
6+
7+
<iframe
8+
width="100%"
9+
height="315"
10+
src="https://www.youtube.com/embed/moSlKAyz58E"
11+
title="API change notifier with GitHub approval"
12+
allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
13+
referrerPolicy="strict-origin-when-cross-origin"
14+
allowFullScreen
15+
/>
16+
17+
In this guide, you run an AI agent, built by Chukwuka Ikeh, that starts when a merged pull request changes your OpenAPI spec. It finds the breaking changes, checks your request logs for the customers who still make those calls, and puts a migration guide entry plus a note to each of them in a docs PR. Merging the docs PR sends the notes, and closing it sends nothing. The demo company is Murmurly, a made-up speech-to-text API with six customers and 90 days of seeded request logs.
18+
19+
The guide has 3 parts:
20+
21+
1. **[Set up the project](#set-up-the-project):** clone the example, push it to a repo of your own, add your keys and start dev.
22+
2. **[Connect GitHub](#connect-github):** point a repo webhook at the hosted `github` endpoint.
23+
3. **[Run the demo](#run-the-demo):** merge a spec change, review the docs PR, and merge it to send the notes.
24+
25+
The full code is in [jackbridger/api-change-notifier](https://github.com/jackbridger/api-change-notifier).
26+
27+
## Prerequisites
28+
29+
- A [Trigger.dev](https://cloud.trigger.dev) project
30+
- Node.js 22.18 or later, and [oasdiff](https://github.com/oasdiff/oasdiff) on your PATH (`brew install oasdiff`, or a binary from its releases)
31+
- A GitHub account where you can create a repo
32+
- A Claude API key and a [Resend](https://resend.com) API key
33+
34+
## Set up the project
35+
36+
**1. Clone and install**
37+
38+
```bash
39+
git clone https://github.com/jackbridger/api-change-notifier.git
40+
cd api-change-notifier
41+
npm install
42+
cp .env.example .env
43+
```
44+
45+
**2. Push it to a repo of your own**
46+
47+
The agent watches the repo that holds the spec and the migration guide, so create an empty GitHub repo and push the project to it. Replace `YOUR_GITHUB_USERNAME` with your GitHub username.
48+
49+
```bash
50+
git remote set-url origin https://github.com/YOUR_GITHUB_USERNAME/api-change-notifier.git
51+
git push -u origin main
52+
```
53+
54+
Then create a [fine-grained token](https://github.com/settings/personal-access-tokens/new) for that repo with read and write access to **Contents** and **Pull requests**.
55+
56+
**3. Fill in `.env`**
57+
58+
- `TRIGGER_PROJECT_REF`: your project's ref, from the project settings page in the dashboard
59+
- `GITHUB_TOKEN`: the token from step 2
60+
- `ANTHROPIC_API_KEY` and `RESEND_API_KEY`: your API keys
61+
- `EMAIL_FROM`: `onboarding@resend.dev` works for testing
62+
63+
**4. Seed the request logs and start dev**
64+
65+
```bash
66+
npm run seed
67+
npm run dev
68+
```
69+
70+
The seed prints `Wrote N requests from 6 customers to data/usage.db`, where N depends on the day you run it. Once dev is running, the `github` endpoint appears on the **Webhooks** page of your Development environment.
71+
72+
## Connect GitHub
73+
74+
**1. Copy the endpoint's URL and secret**
75+
76+
In the dashboard, open the `github` endpoint and go to its **Settings** tab. Choose **Generate secret** and copy it, then copy the **Webhook URL** from the same tab.
77+
78+
**2. Add the webhook on GitHub**
79+
80+
In your GitHub repo, go to **Settings** > **Webhooks** > **Add webhook**. Paste the URL and the secret, and set **Content type** to `application/json`. Choose **Let me select individual events**, untick **Pushes** and tick **Pull requests**.
81+
82+
![GitHub webhook settings](/images/api-change-notifier-github-webhook.png)
83+
84+
GitHub's first delivery, a `ping`, shows as `FILTERED` on the endpoint's **Deliveries** tab. That's expected, because only merged pull requests get through.
85+
86+
## Run the demo
87+
88+
**1. Merge a spec change**
89+
90+
The seeded calls end on the day you ran `npm run seed`, so if that was a few days ago, run it again and restart dev. Then:
91+
92+
```bash
93+
git checkout -b move-word-timings
94+
cp demo/openapi.next.yaml openapi.yaml
95+
git commit -am "Move word timings into the transcript"
96+
git push -u origin move-word-timings
97+
```
98+
99+
Open a PR for the branch and merge it. The change removes `GET /v1/transcripts/{id}/words`, renames `speaker_labels` to `diarization` and replaces `page` with a cursor. In the seeded logs, three customers break and three don't.
100+
101+
**2. Review the docs PR**
102+
103+
The merged PR gets a comment naming the three customers, and a PR titled **Migration guide: ...** opens with the guide entry and every note in its description. In the dashboard you can follow `on-pull-request-merged`, `api-change-notifier` and `analyze-api-change`, whose trace shows each model and tool call.
104+
105+
![The analysis trace with the LLM and tool spans](/images/api-change-notifier-analysis-trace.png)
106+
107+
**3. Merge it to send the notes**
108+
109+
Merge the docs PR. The waiting run resumes, sends the three notes and comments on the original PR with who got one. The notes go to Resend's `delivered+...@resend.dev` test addresses, so you can see them in the Resend dashboard without emailing anyone.
110+
111+
![The note to Harbor & Pine in Resend](/images/api-change-notifier-email.png)
112+
113+
## How it works
114+
115+
A webhook task on the hosted endpoint starts the notifier. GitHub has no separate event for a merge, so the filter matches `closed` with `merged` set to `true`.
116+
117+
```ts src/trigger/on-pull-request-merged.ts
118+
export const onPullRequestMerged = webhook({
119+
id: "on-pull-request-merged",
120+
endpoint: github, // webhooks.endpoint.define({ id: "github", source: webhooks.github() })
121+
filter:
122+
"header.x-github-event == 'pull_request' && event.action == 'closed' && event.pull_request.merged == true && event.pull_request.base.ref == event.repository.default_branch",
123+
onEvent: async ({ event }) => {
124+
const { pull_request: pr, repository } = MergedPullRequest.parse(event);
125+
if (pr.head.ref.startsWith(BRANCH_PREFIX)) return; // one of the agent's own docs PRs
126+
if (!(await findChangedFile(repository.full_name, pr.number, config.specPath))) return; // spec unchanged
127+
128+
await apiChangeNotifier.trigger(
129+
{ repo: repository.full_name, prNumber: pr.number },
130+
{ idempotencyKey: "notifier", tags: [`pr:${pr.number}`] } // a retry won't start a second notifier
131+
);
132+
},
133+
});
134+
```
135+
136+
The notifier runs `oasdiff changelog` on the spec before and after the pull request's changes, then hands the breaking changes to an AI SDK agent with two tools over the request logs, `findCallers` and `getRecentRequests`. A removed field only breaks the customers who send it, so the agent writes a note only for customers a tool returned, and never sees an email address.
137+
138+
Then it waits for the docs PR on a [webhook waiter](/webhooks/waiters). The waiter is created before the docs PR exists, so it matches the branch name the notifier picks up front:
139+
140+
```ts src/trigger/api-change-notifier.ts
141+
const waiter = await webhooks.waiter.create(github, {
142+
match: {
143+
"header.x-github-event": "pull_request",
144+
"event.action": "closed", // merged or not; checks `merged` after waking
145+
"event.repository.full_name": repo,
146+
"event.pull_request.head.ref": branch, // the PR number doesn't exist yet
147+
"event.pull_request.head.repo.full_name": repo, // not a fork's branch with the same name
148+
},
149+
timeout: config.approvalTimeout, // 7d by default
150+
idempotencyKey: "docs-pr-closed",
151+
});
152+
153+
// opens the docs PR and comments on the merged one, then:
154+
const closed = await webhooks.waiter.wait(waiter); // suspended, no compute
155+
```
156+
157+
After a merge, `batchTriggerAndWait` starts one `send-migration-note` run per customer. Each looks up the address in your records and passes Resend an idempotency key, so a retry can't email anyone twice.
158+
159+
<Note>
160+
Under `trigger dev` the run isn't checkpointed, so your dev process stays alive for the whole wait. Deployed, the run is suspended and uses no compute. In both, the wait doesn't count toward `maxDuration`.
161+
</Note>
162+
163+
## Next steps
164+
165+
- Deploy with `npm run seed && npm run deploy`, then add `GITHUB_TOKEN`, `ANTHROPIC_API_KEY`, `RESEND_API_KEY` and `EMAIL_FROM` (on your own verified domain) to Production. Production has its own `github` endpoint, so generate its secret and point the repo's webhook at its URL.
166+
- Point the queries in [`src/lib/usage.ts`](https://github.com/jackbridger/api-change-notifier/blob/main/src/lib/usage.ts) at your own request logs. The agent needs the customer, method, route template, parameter and field names, client and time of each call, never their values. The docs PR shows each affected customer's name and note, and the comment on the merged PR names them, so use a private repo once the logs are real.
167+
- [Webhook waiters](/webhooks/waiters): pause a run until a matching webhook arrives
168+
- [Filters](/webhooks/filters): decide which deliveries start a run
169+
- [GitHub webhooks](/webhooks/providers/github): set up the GitHub source
170+
- [LLM observability](/ai/observability): model and tool call spans from the AI SDK

‎docs/guides/introduction.mdx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Example projects are full projects with example repos you can fork and use. Thes
5252
| Example project | Description | Framework | GitHub |
5353
| :-------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------- | :-------- | :------------------------------------------------------------------------------------------------------------- |
5454
| [Anchor Browser web scraper](/guides/example-projects/anchor-browser-web-scraper) | Monitor a website and find the cheapest tickets for a show. | — | [View the repo](https://github.com/triggerdotdev/examples/tree/main/anchor-browser-web-scraper) |
55+
| [API change notifier](/guides/example-projects/api-change-notifier) | An AI agent finds the API customers a merged PR breaks and waits for a docs PR merge before emailing them. | — | [View the repo](https://github.com/jackbridger/api-change-notifier) |
5556
| [Ask Trigger chat agent](/guides/example-projects/ask-trigger-chat-agent) | Teach yourself Trigger.dev with a chat agent that answers by drawing interactive node-graphs, quizzes and cards using `chat.agent()` and generative UI. | Next.js | [View the repo](https://github.com/triggerdotdev/examples/tree/main/trigger-chat-agent) |
5657
| [Attio lead research agent](/guides/example-projects/attio-lead-agent) | An AI agent that runs on every new lead in Attio, researches it with Firecrawl and writes its findings back to the record, using hosted webhooks and the AI SDK. | — | No repo yet; the code is in the guide |
5758
| [Batch LLM Evaluator](/guides/example-projects/batch-llm-evaluator) | Evaluate multiple LLM models and stream the results to the frontend. | Next.js | [View the repo](https://github.com/triggerdotdev/examples/tree/main/batch-llm-evaluator) |
401 KB
Loading
246 KB
Loading
211 KB
Loading

‎docs/webhooks/providers/github.mdx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,7 @@ A waiter only matches deliveries that arrive after it is created. Create it befo
110110

111111
Use `orgs/ORG/hooks` for an organization webhook. For a GitHub App, set the webhook URL and secret in the app's settings. If you configure an App's webhook through the API, set `content_type` to `json`.
112112
4. Trigger an event in the repository, or use the endpoint's **Test** tab, and check the endpoint's **Deliveries** tab. See [Verify the connection](/webhooks/connect#verify-the-connection) if nothing arrives.
113+
114+
## Example project
115+
116+
The [API change notifier example](/guides/example-projects/api-change-notifier) starts an AI agent when a merged pull request changes an OpenAPI spec, then uses a [waiter](/webhooks/waiters) to pause the run until a second GitHub event, the docs PR being closed, arrives.

0 commit comments

Comments
 (0)