EventSub Client#

EventSub lets you listen for events that happen on Twitch.

The EventSub client runs in its own thread, calling the given callback function whenever an event happens.

Look at the Twitch EventSub reference to find the topics you are interested in.

Requirements#

Note

Please note that Your Endpoint URL has to be HTTPS, has to run on Port 443 and requires a valid, non self signed certificate This most likely means, that you need a reverse proxy like nginx. You can also hand in a valid ssl context to be used in the constructor.

In the case that you don’t hand in a valid ssl context to the constructor, you can specify any port you want in the constructor and handle the bridge between this program and your public URL on port 443 via reverse proxy.

You can check on whether or not your webhook is publicly reachable by navigating to the URL set in callback_url. You should get a 200 response with the text pyTwitchAPI eventsub.

Listening to topics#

After you started your EventSub client, you can use the listen_ prefixed functions to listen to the topics you are interested in.

The function you hand in as callback will be called whenever that event happens with the event data as a parameter.

Code Example#

from twitchAPI.twitch import Twitch
from twitchAPI.helper import first
from twitchAPI.eventsub import EventSub
import asyncio


TARGET_USERNAME = 'target_username_here'
EVENTSUB_URL = 'https://url.to.your.webhook.com'
APP_ID = 'your_app_id'
APP_SECRET = 'your_app_secret'


async def on_follow(data: dict):
    # our event happend, lets do things with the data we got!
    print(data)


async def eventsub_example():
    # create the api instance and get the ID of the target user
    twitch = await Twitch(APP_ID, APP_SECRET)
    user = await first(twitch.get_users(logins=TARGET_USERNAME))

    # basic setup, will run on port 8080 and a reverse proxy takes care of the https and certificate
    event_sub = EventSub(EVENTSUB_URL, APP_ID, 8080, twitch)

    # unsubscribe from all old events that might still be there
    # this will ensure we have a clean slate
    await event_sub.unsubscribe_all()
    # start the eventsub client
    event_sub.start()
    # subscribing to the desired eventsub hook for our user
    # the given function will be called every time this event is triggered
    await event_sub.listen_channel_follow(user.id, on_follow)

    # eventsub will run in its own process
    # so lets just wait for user input before shutting it all down again
    try:
        input('press Enter to shut down...')
    finally:
        # stopping both eventsub as well as gracefully closing the connection to the API
        await event_sub.stop()
        await twitch.close()
    print('done')


# lets run our example
asyncio.run(eventsub_example())

Class Documentation#

class twitchAPI.eventsub.EventSub#

Bases: object

EventSub integration for the Twitch Helix API.

__init__(callback_url, api_client_id, port, twitch, ssl_context=None)#
Parameters:
  • callback_url (str) – The full URL of the webhook.

  • api_client_id (str) – The id of your API client

  • port (int) – the port on which this webhook should run

  • twitch (Twitch) – a app authenticated instance of Twitch

  • ssl_context (Optional[SSLContext]) – optional ssl context to be used

    Default: None

callback_url: str#

The full URL of the webhook.

logger: Logger#

The logger used for EventSub related log messages

secret: str#

A random secret string. Set this for added security.

Default: A random 20 character long string

wait_for_subscription_confirm: bool#

Set this to false if you don’t want to wait for a subscription confirm.

Default: True

wait_for_subscription_confirm_timeout: int#

Max time in seconds to wait for a subscription confirmation. Only used if wait_for_subscription_confirm is set to True.

Default: 30

unsubscribe_on_stop: bool#

Unsubscribe all currently active Webhooks on calling stop()

Default: True

start()#

Starts the EventSub client

Return type:

None

Raises:

RuntimeError – if EventSub is already running

async stop()#

Stops the EventSub client

This also unsubscribes from all known subscriptions if unsubscribe_on_stop is True

Return type:

None

async unsubscribe_all()#

Unsubscribe from all subscriptions

async unsubscribe_all_known()#

Unsubscribe from all subscriptions known to this client.

async unsubscribe_topic(topic_id)#

Unsubscribe from a specific topic.

Return type:

bool

async listen_channel_update(broadcaster_user_id, callback)#

A broadcaster updates their channel properties e.g., category, title, mature flag, broadcast, or language.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelupdate

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_follow(broadcaster_user_id, callback)#

A specified channel receives a follow.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelfollow

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_subscribe(broadcaster_user_id, callback)#

A notification when a specified channel receives a subscriber. This does not include resubscribes.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelsubscribe

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_subscription_end(broadcaster_user_id, callback)#

A notification when a subscription to the specified channel ends.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelsubscriptionend

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_subscription_gift(broadcaster_user_id, callback)#

A notification when a viewer gives a gift subscription to one or more users in the specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelsubscriptiongift

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_subscription_message(broadcaster_user_id, callback)#

A notification when a user sends a resubscription chat message in a specific channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelsubscriptionmessage

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_cheer(broadcaster_user_id, callback)#

A user cheers on the specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelcheer

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_raid(callback, to_broadcaster_user_id=None, from_broadcaster_user_id=None)#

A broadcaster raids another broadcaster’s channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelraid

Parameters:
  • from_broadcaster_user_id (Optional[str]) – The broadcaster user ID that created the channel raid you want to get notifications for.

  • to_broadcaster_user_id (Optional[str]) – The broadcaster user ID that received the channel raid you want to get notifications for.

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_ban(broadcaster_user_id, callback)#

A viewer is banned from the specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelban

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_unban(broadcaster_user_id, callback)#

A viewer is unbanned from the specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelunban

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_moderator_add(broadcaster_user_id, callback)#

Moderator privileges were added to a user on a specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelmoderatoradd

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_moderator_remove(broadcaster_user_id, callback)#

Moderator privileges were removed from a user on a specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelmoderatorremove

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_points_custom_reward_add(broadcaster_user_id, callback)#

A custom channel points reward has been created for the specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelchannel_points_custom_rewardadd

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_points_custom_reward_update(broadcaster_user_id, callback, reward_id=None)#

A custom channel points reward has been updated for the specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelchannel_points_custom_rewardupdate

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • reward_id (Optional[str]) – the id of the reward you want to get updates from.

    Default: None

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_points_custom_reward_remove(broadcaster_user_id, callback, reward_id=None)#

A custom channel points reward has been removed from the specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelchannel_points_custom_rewardremove

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • reward_id (Optional[str]) – the id of the reward you want to get updates from.

    Default: None

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_points_custom_reward_redemption_add(broadcaster_user_id, callback, reward_id=None)#

A viewer has redeemed a custom channel points reward on the specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelchannel_points_custom_reward_redemptionadd

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • reward_id (Optional[str]) – the id of the reward you want to get updates from.

    Default: None

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_points_custom_reward_redemption_update(broadcaster_user_id, callback, reward_id=None)#

A redemption of a channel points custom reward has been updated for the specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelchannel_points_custom_reward_redemptionupdate

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • reward_id (Optional[str]) – the id of the reward you want to get updates from.

    Default: None

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_poll_begin(broadcaster_user_id, callback)#

A poll started on a specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelpollbegin

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_poll_progress(broadcaster_user_id, callback)#

Users respond to a poll on a specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelpollprogress

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_poll_end(broadcaster_user_id, callback)#

A poll ended on a specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelpollend

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_prediction_begin(broadcaster_user_id, callback)#

A Prediction started on a specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelpredictionbegin

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_prediction_progress(broadcaster_user_id, callback)#

Users participated in a Prediction on a specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelpredictionprogress

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_prediction_lock(broadcaster_user_id, callback)#

A Prediction was locked on a specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelpredictionlock

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_prediction_end(broadcaster_user_id, callback)#

A Prediction ended on a specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelpredictionend

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_drop_entitlement_grant(organisation_id, callback, category_id=None, campaign_id=None)#

An entitlement for a Drop is granted to a user.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#dropentitlementgrant

Parameters:
  • organisation_id (str) – The organization ID of the organization that owns the game on the developer portal.

  • category_id (Optional[str]) – The category (or game) ID of the game for which entitlement notifications will be received.

    Default: None

  • campaign_id (Optional[str]) – The campaign ID for a specific campaign for which entitlement notifications will be received.

    Default: None

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_extension_bits_transaction_create(extension_client_id, callback)#

A Bits transaction occurred for a specified Twitch Extension.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#extensionbits_transactioncreate

Parameters:
  • extension_client_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_goal_begin(broadcaster_user_id, callback)#

A goal begins on the specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelgoalbegin

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_goal_progress(broadcaster_user_id, callback)#

A goal makes progress on the specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelgoalprogress

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_goal_end(broadcaster_user_id, callback)#

A goal ends on the specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelgoalend

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_hype_train_begin(broadcaster_user_id, callback)#

A Hype Train begins on the specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelhype_trainbegin

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_hype_train_progress(broadcaster_user_id, callback)#

A Hype Train makes progress on the specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelhype_trainprogress

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_hype_train_end(broadcaster_user_id, callback)#

A Hype Train ends on the specified channel.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelhype_trainend

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_stream_online(broadcaster_user_id, callback)#

The specified broadcaster starts a stream.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#streamonline

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_stream_offline(broadcaster_user_id, callback)#

The specified broadcaster stops a stream.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#streamoffline

Parameters:
  • broadcaster_user_id (str) – the id of the user you want to listen to

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_user_authorization_grant(client_id, callback)#

A user’s authorization has been granted to your client id.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#userauthorizationgrant

Parameters:
Raises:
Return type:

str

async listen_user_authorization_revoke(client_id, callback)#

A user’s authorization has been revoked for your client id.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#userauthorizationrevoke

Parameters:
Raises:
Return type:

str

async listen_user_update(user_id, callback)#

A user has updated their account.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#userupdate

Parameters:
  • user_id (str) – The user ID for the user you want update notifications for.

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_shield_mode_begin(broadcaster_user_id, moderator_user_id, callback)#

Sends a notification when the broadcaster activates Shield Mode.

Requires the MODERATOR_READ_SHIELD_MODE or MODERATOR_MANAGE_SHIELD_MODE auth scope.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/#channelshield_modebegin

Parameters:
  • broadcaster_user_id (str) – The ID of the broadcaster that you want to receive notifications about when they activate Shield Mode.

  • moderator_user_id (str) – The ID of the broadcaster or one of the broadcaster’s moderators.

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_shield_mode_end(broadcaster_user_id, moderator_user_id, callback)#

Sends a notification when the broadcaster deactivates Shield Mode.

Requires the MODERATOR_READ_SHIELD_MODE or MODERATOR_MANAGE_SHIELD_MODE auth scope.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/#channelshield_modeend

Parameters:
  • broadcaster_user_id (str) – The ID of the broadcaster that you want to receive notifications about when they deactivate Shield Mode.

  • moderator_user_id (str) – The ID of the broadcaster or one of the broadcaster’s moderators.

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_charity_campaign_start(broadcaster_user_id, callback)#

Sends a notification when the broadcaster starts a charity campaign.

Requires the CHANNEL_READ_CHARITY auth scope.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/#channelcharity_campaignstart

Parameters:
  • broadcaster_user_id (str) – The ID of the broadcaster that you want to receive notifications about when they start a charity campaign.

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_charity_campaign_progress(broadcaster_user_id, callback)#

Sends notifications when progress is made towards the campaign’s goal or when the broadcaster changes the fundraising goal.

Requires the CHANNEL_READ_CHARITY auth scope.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/#channelcharity_campaignprogress

Parameters:
  • broadcaster_user_id (str) – The ID of the broadcaster that you want to receive notifications about when their campaign makes progress or is updated.

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_charity_campaign_stop(broadcaster_user_id, callback)#

Sends a notification when the broadcaster stops a charity campaign.

Requires the CHANNEL_READ_CHARITY auth scope.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/#channelcharity_campaignstop

Parameters:
  • broadcaster_user_id (str) – The ID of the broadcaster that you want to receive notifications about when they stop a charity campaign.

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str

async listen_channel_charity_campaign_donate(broadcaster_user_id, callback)#

Sends a notification when a user donates to the broadcaster’s charity campaign.

Requires the CHANNEL_READ_CHARITY auth scope.

For more information see here: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/#channelcharity_campaigndonate

Parameters:
  • broadcaster_user_id (str) – The ID of the broadcaster that you want to receive notifications about when users donate to their campaign.

  • callback (Callable[[dict], Awaitable[None]]) – function for callback

Raises:
Return type:

str