Skip to content
twitch.apiv2.0

Reference / API groups

twitch.eventsub

EventSub: subscriptions, and the WebSocket, webhook and conduit listeners.

Every method of twitch.eventsub, with its parameters, what it returns and the token it needs.

twitch.eventsub.subscribe(params, options?): Promise<EventSubSubscription<string, EventSubCondition<T>>>

Creates a subscription by hand. You rarely need it: the listeners returned by websocket, webhook and conduit subscribe for you.

Token: user for the WebSocket transport, app for webhooks and conduits. The scopes depend on the subscription type.

NameTypeDescription
type requiredTSubscription type, such as channel.follow.
condition requiredEventSubCondition<T>What to listen to: which channel, which user… Its shape depends on the type.
transport requiredEventSubTransportInputWhere Twitch delivers the events.

options: Request options for this call: as, token, signal, timeout, retries.

Promise<EventSubSubscription<string, EventSubCondition<T>>>

Fields of EventSubSubscription
NameTypeDescription
idstringID of the subscription.
statusEventSubSubscriptionStatusCurrent state. Only enabled subscriptions deliver events.
typestringSubscription type, such as channel.follow.
versionstringVersion of the subscription type.
conditionEventSubCondition<T>Condition the subscription was created with.
created_atstringWhen the subscription was created, as an RFC 3339 timestamp.
transportEventSubTransportWhere the events are delivered.
costnumberHow much the subscription counts against your cost limit.
await twitch.eventsub.subscribe({
type: "stream.online",
condition: { broadcaster_user_id: "141981764" },
transport: { method: "webhook", callback: "https://example.com/twitch", secret },
});

Twitch reference

twitch.eventsub.unsubscribe(id, options?): Promise<void>

Deletes a subscription. The app token is used unless you say otherwise: for a subscription created over WebSocket, pass { as: userId } with the user whose token created it.

Token: user for a WebSocket subscription, app otherwise.

id: string

options: Request options for this call: as, token, signal, timeout, retries.

Promise<void>

await twitch.eventsub.unsubscribe(subscription.id);

Twitch reference

twitch.eventsub.subscriptions(params?, options?): Paginator<EventSubSubscription<string, Record<string, string>>, GetEventSubSubscriptionsBody>

Lists your subscriptions, oldest first. With the app token you get the webhook and conduit ones. Pass { as: userId } to get the WebSocket subscriptions created with that user’s token.

Token: app or user, see above.

NameTypeDescription
statusEventSubSubscriptionStatusOnly subscriptions in this state.
type(string & {}) | keyof EventSubTypesOnly subscriptions of this type.
user_idstringOnly subscriptions whose condition mentions this user.
subscription_idstringOnly the subscription with this ID.
conduit_idstringOnly subscriptions delivered through this conduit.
afterstringCursor of the next page.

options: Request options for this call: as, token, signal, timeout, retries.

Paginator<EventSubSubscription<string, Record<string, string>>, GetEventSubSubscriptionsBody>

Fields of EventSubSubscription
NameTypeDescription
idstringID of the subscription.
statusEventSubSubscriptionStatusCurrent state. Only enabled subscriptions deliver events.
typestringSubscription type, such as channel.follow.
versionstringVersion of the subscription type.
conditionRecord<string, string>Condition the subscription was created with.
created_atstringWhen the subscription was created, as an RFC 3339 timestamp.
transportEventSubTransportWhere the events are delivered.
costnumberHow much the subscription counts against your cost limit.
const page = await twitch.eventsub.subscriptions({ status: "enabled" });
console.log(page.raw.total_cost, "of", page.raw.max_total_cost);

Twitch reference

twitch.eventsub.websocket(options?): EventSubWebSocket

Opens a WebSocket listener: the simplest way to receive events, with nothing to host. Subscriptions are created with a user token.

This method takes no parameters of its own.

options: Request options for this call: as, token, signal, timeout, retries.

EventSubWebSocket

Fields of EventSubWebSocket
NameTypeDescription
urlstring
WebSocketWebSocketConstructor
#backendSubscriptionBackend
#strategySessionStrategy
#connectionsConnection[]
#whereMap<Entry, Connection>
#seenBoundedMap<string, true>
#lifecycleEmitter<SocketEvents>
sessionIdsstring[]IDs of the sessions currently established. Empty while disconnected.
connectedbooleanWhether at least one session is established.
entriesMap<string, Entry>
#byIdMap<string, Entry>
#eventsEmitter<ListenerEvents>
sizenumberNumber of active subscriptions.
const events = twitch.eventsub.websocket();
events.on("channel.follow", { broadcaster_user_id: id, moderator_user_id: id }, (event) => {
console.log(`${event.user_name} followed`);
});

Twitch reference

twitch.eventsub.webhook(options): EventSubWebhook

Creates a webhook listener, for servers reachable from the internet. Subscriptions are created with the app token and survive restarts.

This method takes no parameters of its own.

options: Request options for this call: as, token, signal, timeout, retries.

EventSubWebhook

Fields of EventSubWebhook
NameTypeDescription
#backendSubscriptionBackend
#callbackstring
#secretstring
#subscribeboolean
#maxAgenumber
#seenMap<string, number>Message IDs already handled, with the time each one was seen. Oldest first.
entriesMap<string, Entry>
#byIdMap<string, Entry>
#eventsEmitter<ListenerEvents>
sizenumberNumber of active subscriptions.
const hooks = twitch.eventsub.webhook({ callback: "https://example.com/twitch", secret });
hooks.on("stream.online", { broadcaster_user_id: "141981764" }, (event) => {
console.log(`${event.broadcaster_user_name} went live`);
});
export default { fetch: (request: Request) => hooks.handle(request) };

Twitch reference

twitch.eventsub.conduit(options?): EventSubWebSocket

Opens a WebSocket that serves one shard of a conduit. Subscriptions are created with the app token, so one process can follow any number of channels without holding a connection per user, and they survive restarts. Run one process per shard to spread the load.

This method takes no parameters of its own.

options: Request options for this call: as, token, signal, timeout, retries.

EventSubWebSocket

Fields of EventSubWebSocket
NameTypeDescription
urlstring
WebSocketWebSocketConstructor
#backendSubscriptionBackend
#strategySessionStrategy
#connectionsConnection[]
#whereMap<Entry, Connection>
#seenBoundedMap<string, true>
#lifecycleEmitter<SocketEvents>
sessionIdsstring[]IDs of the sessions currently established. Empty while disconnected.
connectedbooleanWhether at least one session is established.
entriesMap<string, Entry>
#byIdMap<string, Entry>
#eventsEmitter<ListenerEvents>
sizenumberNumber of active subscriptions.
const events = twitch.eventsub.conduit();
events.on("channel.chat.message", { broadcaster_user_id: channelId, user_id: botId }, (event) => {
console.log(event.message.text);
});

Twitch reference