Skip to main content

Twitch OAuth client with token management and authenticated HTTP requests

Project description

twitch-client

PyPI

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

  1. Go to Twitch Developer Console
  2. Create or select your application
  3. 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 request
  • post(endpoint, data, params) - POST request
  • patch(endpoint, data, params) - PATCH request
  • put(endpoint, data, params) - PUT request
  • delete(endpoint, params) - DELETE request

TwitchAuth

Token management:

  • get_token() - Get valid access token
  • refresh_token() - Force token refresh
  • get_token_info() - Get token metadata and scopes

Exceptions

  • TwitchClientError - Base exception
  • TwitchAuthError - Authentication errors
  • TwitchTokenRefreshError - Token refresh failed
  • TwitchAPIError - API request failed
  • TwitchRateLimitError - Rate limit exceeded

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

twitch_client-0.2.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

twitch_client-0.2.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file twitch_client-0.2.0.tar.gz.

File metadata

  • Download URL: twitch_client-0.2.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.1 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for twitch_client-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2b60f9cb4febf9732edbb0e9cb9c88c873a9cbbd7e3db547888215c7b8568ac9
MD5 c4a538496d4def9ad1de4ceb76d9aa3f
BLAKE2b-256 a6e5b0e18bbb52e6aa69968886753cea4b60523bf4a2270ca85255ba2f86390f

See more details on using hashes here.

File details

Details for the file twitch_client-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: twitch_client-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.1 CPython/3.12.3 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for twitch_client-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f2a3b79d8e37377784ea0129de113cff2ca0e1c52f6ed96416501a9fc5687b2
MD5 69c150e8220ed708c4ef1c0e43290865
BLAKE2b-256 48b08bbc0a0c3e787ef6e858d50f449503c5dafe7df407e6ae1eb3cce1d32d8e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page