Skip to content

Send Stream error alerts

Use event subscriptions and Queues to send an alert to your own endpoint when a video or live input fails.

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.

Prerequisites

1. Create a queue

npx wrangler queues create stream-alerts

2. Create a Worker

Create a new Worker project with C3 (create-cloudflare CLI):

npm create cloudflare@latest -- stream-alerts

For 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-alerts

3. Configure the Worker

In 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_URL

4. Write the queue handler

Replace the contents of src/index.ts with the following code:

src/index.tsts
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 types

5. Deploy the Worker

npx wrangler deploy

6. Subscribe the queue to error events

npx wrangler queues subscription create stream-alerts --source stream --events video_state.errored,live_input.errored

7. Test

Upload 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"
}

Was this helpful?