CastHQ for developers
CastHQ is the shared record of when things happen and how sure anyone can be. Your agent subscribes to a condition on an event (its date confirmed, it is cancelled, it moved by more than three days) and CastHQ tells it when that becomes true. The work that follows stays in your runtime.
Every REST endpoint, interactive, from the OpenAPI document.
Change envelope, condition language, confidence and reconciliation rules, with JSON Schemas.
Instructions for agents: how to look a date up, check it, and wait for it.
subscriptions:read and subscriptions:write, and export it as CASTHQ_TOKEN. An agent can get the same scopes over OAuth instead.https://api.casthq.co (set CASTHQ_API to point somewhere else). Reading public events needs no token at all.{ "error": { "code", "message", "doc_url", "request_id" } }: branch on code. Send casthq-version: YYYY-MM-DD to pin a version.Each one is tested end to end against a live API, so it works as printed. Event ids are in every event's Markdown (/{topic}/{event}.md) and in the change feed.
Subscribe to an event with a condition, then long-poll. The call returns when the producer confirms the day, or at once if it already has. Run it again with the same client_ref and it resumes where it stopped.
#!/usr/bin/env bash
# Wait until an event's date is confirmed, then print it.
#
# export CASTHQ_TOKEN=chq_pat_… # scopes: subscriptions:read, subscriptions:write
# # (a test token, chq_pat_test_…, works on its sandbox)
# ./confirmed-date.sh <event id>
#
# Safe to stop and run again: the same client_ref returns the same
# subscription and its cursor, so nothing that fired in between is lost.
set -euo pipefail
API="${CASTHQ_API:-https://api.casthq.co}"
: "${CASTHQ_TOKEN:?set CASTHQ_TOKEN to a personal access token}"
EVENT="${1:?usage: $0 <event id>}"
AUTH="Authorization: Bearer $CASTHQ_TOKEN"
# 1. Subscribe. If the date is already confirmed, it fires at once.
sub=$(curl -sS --fail-with-body "$API/v1/subscriptions" \
-H "$AUTH" -H "Content-Type: application/json" \
-d '{
"target": "event:'"$EVENT"'",
"condition": { "fact": "schedule", "confidence": { "becomes_at_least": "confirmed" } },
"client_ref": "confirmed-date-'"$EVENT"'"
}' | jq -r '.data.id')
echo "subscribed $sub"
# 2. Long-poll: each call waits up to 20 s for a firing.
while :; do
firing=$(curl -sS --fail-with-body "$API/v1/subscriptions/$sub/events?wait=20" -H "$AUTH" |
jq -c '.data[0] // empty')
[ -n "$firing" ] && break
done
# 3. A firing carries the change that made the condition true, with the
# event's state just after it (an initial firing: the state at creation).
jq -r '(.change.data.after // .state) as $s |
"fired \(.id)\nstate \($s.state), schedule \($s.facts.schedule)"' <<<"$firing"
curl -sS --fail-with-body "$API/v1/events/$EVENT/status" -H "$AUTH" |
jq -r '"date \(.data.date.words) (\(.data.date.precision))"'
Run: ./confirmed-date.sh <event id>
wait_for answers straight away with the event's state now; get_events returns the firing once it is cancelled. Any MCP client makes these same two calls once https://api.casthq.co/mcp is added as a server.
#!/usr/bin/env bash
# The same wait over MCP, the way an agent does it: wait_for once, then
# get_events until it fires. Here: tell me if the event is cancelled.
#
# export CASTHQ_TOKEN=chq_pat_… # or an OAuth access token; scopes: subscriptions:read, subscriptions:write
# ./cancelled-mcp.sh <event id>
#
# An MCP client (Claude, Cursor, the Agents SDK) makes these same two tool
# calls for you once /mcp is added as a server; curl shows what goes over
# the wire.
set -euo pipefail
API="${CASTHQ_API:-https://api.casthq.co}"
: "${CASTHQ_TOKEN:?set CASTHQ_TOKEN to a personal access token}"
EVENT="${1:?usage: $0 <event id>}"
# One JSON-RPC tool call. /mcp is stateless: no session to open first.
# The answer comes as one server-sent event; its text is the tool's JSON.
tool() {
curl -sS --fail-with-body "$API/mcp" \
-H "Authorization: Bearer $CASTHQ_TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d "$(jq -n --arg name "$1" --argjson args "$2" \
'{jsonrpc: "2.0", id: 1, method: "tools/call", params: {name: $name, arguments: $args}}')" |
sed -n 's/^data: //p' | jq -r '.result.content[0].text'
}
# 1. wait_for answers at once: the event's state now, and the firing if the
# condition already holds (an event cancelled before you asked fires).
answer=$(tool wait_for '{
"target": "event:'"$EVENT"'",
"condition": { "status": { "becomes": "cancelled" } },
"client_ref": "cancelled-'"$EVENT"'"
}')
sub=$(jq -r '.subscription.id' <<<"$answer")
echo "subscribed $sub (status now: $(jq -r '.subscription.currentState.status' <<<"$answer"))"
# 2. Not yet: poll get_events. It resumes where the last call stopped.
firing=$(jq -c '.firings[0]? // empty' <<<"$answer")
while [ -z "$firing" ]; do
sleep 2
firing=$(tool get_events '{"subscription": "'"$sub"'"}' | jq -c '.data.subscriptionEvents.firings[0] // empty')
done
jq -r '"fired \(.id)\nstatus \(.change.data.after.status // .state.status)"' <<<"$firing"
Run: ./cancelled-mcp.sh <event id>
A timer condition: fires two hours before the event's start, and moves with the date if the start moves. In test mode, advance the sandbox's test clock and it fires at once instead of in two hours.
#!/usr/bin/env bash
# Be told two hours before an event starts. If its date moves, the timer
# moves with it; the firing id says which date it was for.
#
# export CASTHQ_TOKEN=chq_pat_… # scopes: subscriptions:read, subscriptions:write
# ./before-start.sh <event id>
#
# In test mode (a test token, chq_pat_test_…, on a sandbox event) you do not
# wait two real hours: advance the sandbox's test clock and it fires at once:
#
# curl -X POST "$CASTHQ_API/v1/test_clocks/<clock id>/advance" \
# -H "Authorization: Bearer $CASTHQ_TOKEN" -H "Content-Type: application/json" \
# -d '{"by": "PT23H"}'
set -euo pipefail
API="${CASTHQ_API:-https://api.casthq.co}"
: "${CASTHQ_TOKEN:?set CASTHQ_TOKEN to a personal access token}"
EVENT="${1:?usage: $0 <event id>}"
AUTH="Authorization: Bearer $CASTHQ_TOKEN"
sub=$(curl -sS --fail-with-body "$API/v1/subscriptions" \
-H "$AUTH" -H "Content-Type: application/json" \
-d '{
"target": "event:'"$EVENT"'",
"condition": { "time": { "anchor": "start", "offset": "-PT2H" } },
"client_ref": "before-start-'"$EVENT"'"
}' | jq -r '.data.id')
echo "subscribed $sub"
while :; do
firing=$(curl -sS --fail-with-body "$API/v1/subscriptions/$sub/events?wait=20" -H "$AUTH" |
jq -c '.data[0] // empty')
[ -n "$firing" ] && break
done
# A timer firing: which timer, for which start time, and the event's state then.
jq -r '"fired \(.id)\nstart \(.timer.anchor_value), timer \(.timer.offset_seconds)s\nstatus \(.state.status)"' <<<"$firing"
Run: ./before-start.sh <event id>
A continuous subscription on a topic and its sub-topics: one firing each time an event becomes publicly listed. Firing ids are stable, so a restart never repeats one.
#!/usr/bin/env bash
# Watch a topic: print each event as it is announced (becomes publicly
# listed), for as long as you leave it running.
#
# export CASTHQ_TOKEN=chq_pat_… # scopes: subscriptions:read, subscriptions:write
# ./announced.sh <topic slug or id> [how many to wait for]
#
# A continuous subscription with a cursor: every firing has a stable id, so
# a restart (same client_ref) picks up where it stopped without repeats.
set -euo pipefail
API="${CASTHQ_API:-https://api.casthq.co}"
: "${CASTHQ_TOKEN:?set CASTHQ_TOKEN to a personal access token}"
TOPIC="${1:?usage: $0 <topic> [count]}"
COUNT="${2:-0}" # 0 = forever
AUTH="Authorization: Bearer $CASTHQ_TOKEN"
sub=$(curl -sS --fail-with-body "$API/v1/subscriptions" \
-H "$AUTH" -H "Content-Type: application/json" \
-d '{
"target": "topic:'"$TOPIC"'",
"condition": { "listed": { "becomes": true } },
"mode": "continuous",
"client_ref": "announced-'"$TOPIC"'"
}' | jq -r '.data.id')
echo "subscribed $sub"
seen=0
cursor=""
while [ "$COUNT" -eq 0 ] || [ "$seen" -lt "$COUNT" ]; do
page=$(curl -sS --fail-with-body "$API/v1/subscriptions/$sub/events?wait=20${cursor:+&cursor=$cursor}" -H "$AUTH")
cursor=$(jq -r '.next_cursor' <<<"$page")
while read -r firing; do
[ -z "$firing" ] && continue
event=$(jq -r '.event' <<<"$firing")
title=$(curl -sS --fail-with-body "$API/v1/events/$event/status" -H "$AUTH" | jq -r '.data.event.title')
echo "fired $(jq -r '.id' <<<"$firing"): announced \"$title\""
seen=$((seen + 1))
done < <(jq -c '.data[]' <<<"$page")
done
Run: ./announced.sh <topic>
The dry run replays a condition over an event's history and lists the changes it would have fired at. No token needed.
#!/usr/bin/env bash
# Try a condition before you wait on it: replay it over an event's history
# and see which changes it would have fired at. Anonymous: no token needed
# for a public event. A test token (CASTHQ_TOKEN=chq_pat_test_…) also reads
# its sandbox's events.
#
# ./test-condition.sh <event id>
set -euo pipefail
API="${CASTHQ_API:-https://api.casthq.co}"
EVENT="${1:?usage: $0 <event id>}"
AUTH=()
[ -n "${CASTHQ_TOKEN:-}" ] && AUTH=(-H "Authorization: Bearer $CASTHQ_TOKEN")
curl -sS --fail-with-body "$API/v1/conditions/test" "${AUTH[@]}" \
-H "Content-Type: application/json" \
-d '{
"target": "event:'"$EVENT"'",
"condition": { "change": "schedule" }
}' |
jq '.data | {
fired: [.fired[] | "\(.id) \(.type) at \(.time)"],
scanned,
would_fire_now: .initial.would_fire_now
}'
Run: ./test-condition.sh <event id>
https://api.casthq.co/mcp/public. Read and write, OAuth: https://api.casthq.co/mcp.GET https://api.casthq.co/v1/changes?format=cloudevents..md.Every REST error's doc_url points at its row here. Codes are stable within a version and new ones may be added; retryable says whether the same request can succeed later.