Python Twitch API

PyPI verion PyPI verion Twitch API version Documentation Status

This is a full implementation of the Twitch API its Webhook and PubSub in python 3.7.

It handles stuff like keeping your user token refreshed and adds a easy way to interface with all parts of the Twitch API (EventSub is not there yet but planned).

Install with pip install twitchAPI

Links

Example use

from twitchAPI.twitch import Twitch
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.types import AuthScope
from twitchAPI.webhook import TwitchWebHook
from pprint import pprint

def callback_stream_changed(uuid, data):
    print('Callback Stream changed for UUID ' + str(uuid))
    pprint(data)

def callback_user_changed(uuid, data):
    print('Callback User changed for UUID ' + str(uuid))
    pprint(data)

# create instance of twitch API
twitch = Twitch('my_app_id', 'my_app_secret')
twitch.authenticate_app([])

# get ID of user
user_info = twitch.get_users(logins=['my_username'])
user_id = user_info['data'][0]['id']

# get user auth token
target_scope = [AuthScope.BITS_READ]
auth = UserAuthenticator(twitch, target_scope, force_verify=False)
# this will open your default browser and prompt you with the twitch verification website
token, refresh_token = auth.authenticate()
# add User authentication
twitch.set_user_authentication(token, target_scope, refresh_token)

# starting a webhook
hook = TwitchWebHook("https://my.cool.ip:8080", 'your app id', 8080)
hook.authenticate(twitch)  # this will use the highest authentication set, which is the user authentication.
# some hooks don't require any authentication, which would remove the requirement to set up a https reverse proxy
# if you don't require authentication just dont call authenticate()
hook.start()

# the hook has to run before you subscribe to any events since the twitch api will do a handshake this this webhook as soon as you subscribe
success, uuid_stream = hook.subscribe_stream_changed('your user id', callback_stream_changed)
print(f'was subscription successfull?: {success}')
success, uuid_user = hook.subscribe_user_changed('your user id', callback_user_changed)
print(f'was subscription successfull?: {success}')

# now we are fully set up and listening to our webhooks, lets wait for a user imput to stop again:
input('Press enter to stop...')

# lets unsubscribe again
success = hook.unsubscribe(uuid_user)
print(f'was unsubscription successfull?: {success}')
# since hook.unsubscribe_on_stop is true, we dont need to unsubscribe manually, so lets just stop
hook.stop()