Skip to main content

twitch-sdk

PyPI Python License: MIT

Complete Twitch Helix API SDK with Pydantic validation for 170+ endpoints.

Features

  • Full API Coverage - All Twitch Helix endpoints (chat, streams, moderation, etc.)
  • Pydantic Validation - Type-safe request/response models with automatic validation
  • Async/Await - Built for modern Python async applications
  • EventSub WebSocket - Real-time event subscriptions built-in
  • Auto Token Refresh - Handles OAuth token management via twitch-client

Installation

pip install twitch-sdk

Quick Start

import asyncio
from twitch_sdk import TwitchSDK
from twitch_sdk.schemas.streams import GetStreamsRequest

async def main():
    async with TwitchSDK() as sdk:
        # Get live streams for "Just Chatting"
        params = GetStreamsRequest(game_id=["509658"])
        streams = await sdk.streams.get_streams(sdk.http, params)

        for stream in streams.data:
            print(f"{stream.user_name}: {stream.title} ({stream.viewer_count} viewers)")

asyncio.run(main())

Credentials Setup

Create ~/.twitch-secrets/.env with your Twitch credentials:

TWITCH_CLIENT_ID=your_client_id
TWITCH_CLIENT_SECRET=your_client_secret
TWITCH_ACCESS_TOKEN=your_access_token
TWITCH_REFRESH_TOKEN=your_refresh_token

See twitch-client for detailed setup instructions.

Usage Examples

Send Chat Message

from twitch_sdk import TwitchSDK
from twitch_sdk.schemas.chat import SendMessageRequest

async def main():
    async with TwitchSDK() as sdk:
        params = SendMessageRequest(
            broadcaster_id="123456",
            sender_id="123456",
            message="Hello from the SDK!"
        )
        result = await sdk.chat.send_chat_message(sdk.http, params)
        print(f"Message sent: {result.data[0].is_sent}")

Create a Poll

from twitch_sdk import TwitchSDK
from twitch_sdk.schemas.polls import CreatePollRequest, PollChoiceInput

async def main():
    async with TwitchSDK() as sdk:
        params = CreatePollRequest(
            broadcaster_id="123456",
            title="Favorite game?",
            choices=[
                PollChoiceInput(title="Option A"),
                PollChoiceInput(title="Option B"),
            ],
            duration=60,
        )
        poll = await sdk.polls.create_poll(sdk.http, params)
        print(f"Poll created: {poll.data[0].id}")

EventSub WebSocket

from twitch_sdk import TwitchSDK

async def main():
    async with TwitchSDK() as sdk:
        async with sdk.create_eventsub_websocket() as ws:
            # Subscribe to chat messages
            await ws.subscribe(
                event_type="channel.chat.message",
                version="1",
                condition={
                    "broadcaster_user_id": "123456",
                    "user_id": "123456"
                }
            )

            # Process events
            async for event in ws.events():
                print(f"Event: {event}")

API Coverage

The SDK covers all Twitch Helix API endpoints:

Category Endpoints
Ads start_commercial, get_ad_schedule, snooze_next_ad
Analytics get_extension_analytics, get_game_analytics
Bits get_bits_leaderboard, get_cheermotes
Channel Points custom rewards, redemptions
Channels get/modify channel info, followers, VIPs, editors
Charity campaigns, donations
Chat messages, emotes, badges, settings, shoutouts
Clips create_clip, get_clips
EventSub subscriptions, WebSocket, conduits
Games get_games, get_top_games
Goals creator goals
Guest Star sessions, invites, slots
Hype Train events
Moderation bans, blocked terms, AutoMod, shield mode
Polls create, get, end
Predictions create, get, end
Raids start, cancel
Schedule segments, iCalendar
Search categories, channels
Streams get_streams, markers, stream key
Subscriptions broadcaster subscriptions
Teams get teams
Users get/update users, blocks, extensions
Videos get, delete
Whispers send whisper

Pydantic Validation

All requests and responses use Pydantic models for validation:

from twitch_sdk.schemas.chat import SendMessageRequest

# Validation happens automatically
params = SendMessageRequest(
    broadcaster_id="123",
    sender_id="456",
    message="Hello!"
)

# Access validated fields
print(params.broadcaster_id)

# Serialize for API
print(params.model_dump(exclude_none=True))

License

MIT License - see LICENSE for details.

Metadata

Release files for twitch-sdk 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for twitch-sdk 0.1.5
File Size Uploaded
twitch_sdk-0.1.5.tar.gz 23.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for twitch-sdk 0.1.5
File Interpreter ABI Platform
twitch_sdk-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 68.0 kB

Release files / twitch_sdk-0.1.5.tar.gz

Download URL twitch_sdk-0.1.5.tar.gz
Size 23.9 kB
Tags Source
SHA-256 checksum
How to use checksums
62c4c9760f499b7da55801fdaaf4ca418aab34b4c0946a2c9094ec93e2b77b6a
BLAKE2b-256 checksum
How to use checksums
d6cd977cca83c1180b46ff13d211f595dd7c4d76dcb8df897d3dddcf4125c099
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.1 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

Release files / twitch_sdk-0.1.5-py3-none-any.whl

Download URL twitch_sdk-0.1.5-py3-none-any.whl
Size 44.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
74f59aa25f85407f9476c3d4c24e155ce306572e27a9926e2c8f0b06cc66069f
BLAKE2b-256 checksum
How to use checksums
0076fc182a53f7d3126e27288a46de8218690330d44ecd49c37c2e76db16c323
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.1 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page