CLI and APIs
Wait for changes over REST
Your tool or bot must react when CI ends, a pull request changes or a branch moves. A poll every few seconds wastes requests and reacts late, and a webhook needs a public server.
5 min read
The event stream sends each change the moment it happens, over one open request (Server-Sent Events). It takes the same tokens as the other REST routes.
1. Open the stream
FORGE=https://source.tanstack.com/api/v1
curl -N -H "Authorization: Bearer $TOKEN" -H "Accept: text/event-stream" \
"$FORGE/repos/acme/app/events?scope=pr&number=7"
: ok
id: 1843
id: 1843
event: checks
data: {"type":"checks","pr":7}
id: 1851
event: item
data: {"type":"item","kind":"pull","number":7,"state":"open"}
: okcomes when the stream opens, and again every 15 seconds. It keeps proxies from closing a quiet stream.- A stream with no cursor starts at now. Its first frame has only an
id: the cursor of now. - Each event is one frame:
idis a cursor,eventis the type, anddatais the event as JSON. - Without
Accept: text/event-stream, the route answers406.
2. Reconnect with the last id
The forge ends each stream after 60 seconds. Open the next one at once, with the last id that you got in the Last-Event-ID header:
curl -N -H "Authorization: Bearer $TOKEN" -H "Accept: text/event-stream" \
-H "Last-Event-ID: 1851" "$FORGE/repos/acme/app/events?scope=pr&number=7"
- The stream starts after that id, so you miss no change.
- An event can come twice after a reconnect, but none is lost. Make your handler safe to run twice for the same event.
Last-Event-IDwins over acursorin the query. Usecursorwhen your client cannot set the header.
If your id is too old, you get an event: reset frame, with the id of now. Read the state again, for example the checks of the pull request.
3. Loop in a script
This bash script keeps the stream open, prints each event as one JSON line, and keeps the last id for the next connection:
FORGE=https://source.tanstack.com/api/v1
STREAM="$FORGE/repos/acme/app/events?scope=checks&number=7"
last=""
while true; do
while IFS= read -r line; do
case "$line" in
"id: "*) last="${line#id: }" ;;
"data: "*) echo "${line#data: }" ;;
esac
done < <(curl -sN -H "Authorization: Bearer $TOKEN" -H "Accept: text/event-stream" \
-H "Last-Event-ID: $last" "$STREAM")
sleep 1
done
Done
Your tool gets each change when it happens, with one open request and no public server.
Or use the long poll
Some clients cannot read a stream, for example a tool call over MCP. Use the wait route instead. It holds your request open, and answers once with the next changes.
Get a cursor. A request with no
cursoranswers at once, with no events:curl -H "Authorization: Bearer $TOKEN" "$FORGE/repos/acme/app/events/wait?scope=pr&number=7"{ "events": [], "cursor": "1843", "reset": false }Send the cursor back:
curl -H "Authorization: Bearer $TOKEN" \ "$FORGE/repos/acme/app/events/wait?scope=pr&number=7&cursor=1843"{ "events": [ { "type": "checks", "pr": 7 }, { "type": "item", "kind": "pull", "number": 7, "state": "open" } ], "cursor": "1851", "reset": false }Send the new
cursorwith your next request, and loop.
- When changes came after your cursor, the forge answers at once.
- Else it waits for the next change, 25 seconds by default. When the time ends with no change,
eventsis empty. - If
resetistrue, your cursor was too old. Read the state again. The newcursormarks now.
The query
The stream and the wait route take the same query:
| Parameter | Value |
|---|---|
scope | repo (the default), pr, issue, checks, run or job. |
number | The issue or pull request, for pr, issue and checks. |
run | The CI run id, for run. |
job | The CI job id, for job. |
events | Only these kinds, with commas: item, checks, push, run. |
branch | Leave out the pushes and the CI runs of other branches. |
cursor | Start after this cursor. On the stream, Last-Event-ID wins over it. |
timeout | The wait route only: the longest wait, in seconds, 1 to 50. 25 by default. |
What each scope gives:
repo: every event of the repository.pr: the pull request, its checks, and the pushes to its head branch when that branch is in this repository. Norunevents: achecksevent tells you that its CI changed.issue: the issue.checks: the checks of the pull request.run: the CI run.job: thelogevents of the CI job. It takesjobonly.
A bad value gets 400, for example scope=pr with no number, timeout=60, or scope=job with number, run, events or branch.
The cursors of a job watch are the job's own. Do not send them to a watch of another scope.
The events
| Event | Meaning |
|---|---|
{ "type": "item", "kind", "number", "state" } | An issue or a pull request changed. kind is issue or pull. state is open, closed or merged when the forge read the event: to wait for a merge, stop at the event with "state": "merged", and read the state once when you open the first stream, in case the merge came before it. |
{ "type": "checks", "pr" } | The checks of a pull request changed. |
{ "type": "push", "ref", "head" } | A branch or a tag moved. head is its tip when the forge read the event, null when it was deleted. |
{ "type": "run", "id", "workflow", "ref", "headSha", "pr", "status", "conclusion" } | A CI run changed. pr is null for a run of no pull request. |
{ "type": "log", "job", "step", "lines", "finished" } | Step step of the job has lines lines now. Steps count from 0. finished is true when the step ended. Read the new lines with GET .../ci/runs/<run>/jobs/<job>/logs?step=&from=&to= (see the CI reference). |
- A push, a merge on the forge, and a branch that GitHub sync moved each give a
pushevent. - A wait route answer can also come with no events before the timeout. Send its
cursoragain.
Who gets which events
The routes need read access to the repository, as the other repository routes do. A token with a list of repositories works on those repositories only. Each kind of event also needs its own read:
item: the issue or pull request must be one that you can read.checks: read access to pull requests and to CI.push: read access to the code.run: read access to CI, and to its pull request when it has one. A run of a hidden workflow is never sent.log: read access to CI, and to the pull request of the job when it has one. The job of a hidden workflow is for admins only. Anyone else gets404, as for a job that does not exist.
The same events are the wait_for_events tool over MCP, and the src watch command in the CLI.