This example uses event subscriptions to send two Stream events, video_state.errored and live_input.errored, to a queue. A Worker reads the queue and forwards each error to an alert endpoint, such as a chat webhook or an incident management service.
- A Cloudflare account ↗︎ with Stream enabled
- The Wrangler CLI
- An HTTPS endpoint that accepts JSON
POSTrequests
npx wrangler queues create stream-alertsyarn wrangler queues create stream-alertspnpm wrangler queues create stream-alertsCreate a new Worker project with C3 (create-cloudflare CLI):
npm create cloudflare@latest -- stream-alertsyarn create cloudflare stream-alertspnpm create cloudflare@latest stream-alertsFor setup, select the following options:
- For What would you like to start with?, choose
Hello World example. - For Which template would you like to use?, choose
Worker only. - For Which language do you want to use?, choose
TypeScript. - For Do you want to use git for version control?, choose
Yes. - For Do you want to add an AGENTS.md file to help AI coding tools understand Cloudflare APIs?, choose
Yes. - For Do you want to deploy your application?, choose
No(we will be making some changes before deploying).
Then, move into your new project directory:
cd stream-alertsIn your Wrangler configuration file, register the Worker as a consumer of the queue:
{
"$schema": "./node_modules/wrangler/config-schema.json",
"name": "stream-alerts",
"main": "src/index.ts",
// Set this to today's date
"compatibility_date": "2026-12-03",
"queues": {
"consumers": [{ "queue": "stream-alerts" }],
},
}"$schema" = "./node_modules/wrangler/config-schema.json"
name = "stream-alerts"
main = "src/index.ts"
# Set this to today's date
compatibility_date = "2026-12-03"
[[queues.consumers]]
queue = "stream-alerts"Store the endpoint URL as a secret:
npx wrangler secret put ALERT_WEBHOOK_URLyarn wrangler secret put ALERT_WEBHOOK_URLpnpm wrangler secret put ALERT_WEBHOOK_URLReplace the contents of src/index.ts with the following code:
interface StreamErrorEvent {
type: "cf.stream.video_state.errored" | "cf.stream.live_input.errored";
payload: {
id: string;
status: string | { errorReasonCode: string; errorReasonText: string };
};
metadata: {
eventTimestamp: string;
};
}
export default {
async queue(batch, env): Promise<void> {
for (const message of batch.messages) {
const { type, payload, metadata } = message.body;
const response = await fetch(env.ALERT_WEBHOOK_URL, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
event: type,
id: payload.id,
reason: payload.status,
timestamp: metadata.eventTimestamp,
}),
});
if (response.ok) {
message.ack();
} else {
console.error(`Alert endpoint returned ${response.status}`);
message.retry({ delaySeconds: 30 });
}
}
},
} satisfies ExportedHandler<Env, StreamErrorEvent>;If your service expects a specific request format, change the body. For example, Slack incoming webhooks ↗︎ expect a text field, and Discord webhooks ↗︎ expect a content field.
Run the following command to generate the Env type for your secret:
npx wrangler typesyarn wrangler typespnpm wrangler typesnpx wrangler deployyarn wrangler deploypnpm wrangler deploynpx wrangler queues subscription create stream-alerts --source stream --events video_state.errored,live_input.erroredyarn wrangler queues subscription create stream-alerts --source stream --events video_state.errored,live_input.erroredpnpm wrangler queues subscription create stream-alerts --source stream --events video_state.errored,live_input.erroredUpload a file that is not a video, such as a text file renamed to .mp4. Stream sends video_state.errored, and your endpoint receives a request body similar to the following:
{
"event": "video_state.errored",
"id": "6b9e68b07dfee8cc2d116e4c51d6a957",
"reason": "ERR_NON_VIDEO: The file was not recognized as a valid video file.",
"timestamp": "2026-12-01T02:41:03.208Z"
}