twitch-client
Twitch OAuth client with token management and authenticated HTTP requests for the Twitch Helix API.
Installation
pip install twitch-client
Credentials Setup
1. Get Client ID & Secret
- Go to Twitch Developer Console
- Create or select your application
- Copy the Client ID and generate a Client Secret
2. Get Access & Refresh Tokens
Use the Twitch CLI or an OAuth flow to get tokens:
twitch token -u -s "chat:read chat:edit channel:manage:broadcast"
3. Create .env File
Create ~/.twitch-secrets/.env (default location):
mkdir -p ~/.twitch-secrets
cat > ~/.twitch-secrets/.env << 'EOF'
TWITCH_CLIENT_ID=your_client_id
TWITCH_CLIENT_SECRET=your_client_secret
TWITCH_ACCESS_TOKEN=your_access_token
TWITCH_REFRESH_TOKEN=your_refresh_token
EOF
Or set the TWITCH_ENV_FILE environment variable to use a custom path.
Basic Usage
import asyncio
from twitch_client import TwitchHTTPClient
async def main():
async with TwitchHTTPClient() as client:
# Get user info
response = await client.get("/users", params={"login": "twitch"})
print(response)
asyncio.run(main())
Manual Token Management
from twitch_client import TwitchAuth, TwitchCredentials
async def main():
# Load credentials
credentials = TwitchCredentials()
# Create auth manager
async with TwitchAuth(credentials) as auth:
# Get valid token (refreshes automatically if needed)
token = await auth.get_token()
# Get token info including scopes
info = await auth.get_token_info()
print(f"Scopes: {info.scopes}")
asyncio.run(main())
Custom Credentials Location
import os
os.environ["TWITCH_ENV_FILE"] = "~/my-secrets/.env"
from twitch_client import TwitchHTTPClient
# Will load from custom .env location
Features
- Automatic token refresh before expiration
- Pydantic-based configuration validation
- Async HTTP client with httpx
- Rate limit handling
- Comprehensive error types
API
TwitchHTTPClient
The main HTTP client for making authenticated requests:
get(endpoint, params)- GET requestpost(endpoint, data, params)- POST requestpatch(endpoint, data, params)- PATCH requestput(endpoint, data, params)- PUT requestdelete(endpoint, params)- DELETE request
TwitchAuth
Token management:
get_token()- Get valid access tokenrefresh_token()- Force token refreshget_token_info()- Get token metadata and scopes
Exceptions
TwitchClientError- Base exceptionTwitchAuthError- Authentication errorsTwitchTokenRefreshError- Token refresh failedTwitchAPIError- API request failedTwitchRateLimitError- Rate limit exceeded
Metadata
Release files for twitch-client 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| twitch_client-0.2.0.tar.gz | 5.3 kB | Without provenance | Details for twitch_client-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| twitch_client-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for twitch_client-0.2.0-py3-none-any.whl |
Total release size: 12.2 kB
Release files / twitch_client-0.2.0.tar.gz
| Download URL | twitch_client-0.2.0.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2b60f9cb4febf9732edbb0e9cb9c88c873a9cbbd7e3db547888215c7b8568ac9
|
|
BLAKE2b-256 checksum How to use checksums |
a6e5b0e18bbb52e6aa69968886753cea4b60523bf4a2270ca85255ba2f86390f
|
| 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_client-0.2.0-py3-none-any.whl
| Download URL | twitch_client-0.2.0-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2f2a3b79d8e37377784ea0129de113cff2ca0e1c52f6ed96416501a9fc5687b2
|
|
BLAKE2b-256 checksum How to use checksums |
48b08bbc0a0c3e787ef6e858d50f449503c5dafe7df407e6ae1eb3cce1d32d8e
|
| 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
|
Security for twitch-client 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.