An async Python wrapper for Twitch that handles real-time events via WebSocket EventSub and integrates with the Helix API.
- Async/await support throughout
- Complete Twitch Helix API integration
- WebSocket EventSub for real-time events
- Built-in authentication and token management
To install the library, you can just run the following command:
# Linux/macOS
python3 -m pip install -U twitch.py
# Windows
py -3 -m pip install -U twitch.py
For the development version:
git clone https://github.com/mrsnifo/twitch.py
cd twitch.py
python3 -m pip install -U .
from twitch import App
async def main():
async with App('CLIENT_ID', 'CLIENT_SECRET') as app:
emotes = await app.application.get_global_emotes()
print(f'Found {len(emotes)} global emotes')
import asyncio
asyncio.run(main())
from twitch.eventsub import ClientApp, Event, ChannelChatMessageEvent
client = ClientApp('CLIENT_ID', 'CLIENT_SECRET')
@client.event
async def on_chat_message_v1(message: Event[ChannelChatMessageEvent]):
print(message.event.message)
@client.event
async def on_ready():
user = await client.add_user('ACCESS_TOKEN')
await client.eventsub.channel_chat_message(broadcaster_user_id=user.id, user_id=user.id)
# Uses availability-based shard selection. For multi-sharding use MultiShardClientApp.
client.run('CONDUIT_ID', shard_ids=(0,))
from twitch.eventsub import ClientUser, Event, ChannelFollowEvent
client = ClientUser('CLIENT_ID', 'CLIENT_SECRET')
@client.event
async def on_ready():
print('Client is ready!')
await client.eventsub.channel_follow()
@client.event
async def on_channel_follow_v2(message: Event[ChannelFollowEvent]):
print(f'{message.event.user.name} just followed!')
client.run('ACCESS_TOKEN')
More usage examples available in the examples folder.