zairakai / laravel-twitch
Complete Twitch API integration package with OAuth, EventSub, badges system and event-driven architecture
v1.0.0
2026-03-11 19:56 UTC
Requires
- php: ^8.3
- guzzlehttp/guzzle: ^7.8
- laravel/framework: ^11.0 || ^12.0
- spatie/laravel-data: ^4.0
Requires (Dev)
- driftingly/rector-laravel: ^2.1
- ergebnis/composer-normalize: ^2.49
- larastan/larastan: ^3.9
- laravel/pint: ^1.27
- mockery/mockery: ^1.6
- nunomaduro/phpinsights: ^2.13
- orchestra/testbench: ^9.0 || ^10.0
- phpmetrics/phpmetrics: ^2.9
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.0
- rector/rector: ^2.3
- zairakai/laravel-dev-tools: ^1.0
Suggests
- ergebnis/composer-normalize: Automated composer.json normalization
README
Complete Twitch API integration for Laravel: OAuth, Helix API, EventSub webhooks, and a badges system.
Features
- Helix API — games, streams, users, clips, channels, search, and more via
Twitchfacade - OAuth 2.0 — authorization code flow, token refresh, and PKCE support via
TwitchOAuthfacade - EventSub — subscribe to and handle Twitch webhook events with signature verification
- Badges system — fetch, cache, and display global and channel emote/badge sets
- Event-driven — Laravel events dispatched for every received EventSub notification
- Token management — automatic token refresh and storage
Install
composer require zairakai/laravel-twitch
Publish the config:
php artisan vendor:publish --tag=config
Add your credentials to .env:
TWITCH_CLIENT_ID=your_client_id
TWITCH_CLIENT_SECRET=your_client_secret
TWITCH_REDIRECT_URI=https://your-app.com/auth/twitch/callback
TWITCH_WEBHOOK_SECRET=your_webhook_secret
Usage
Helix API
use Zairakai\LaravelTwitch\Facades\Twitch;
// Get top games
$games = Twitch::getTopGames();
// Get streams by user ID(s)
$streams = Twitch::getStreams(userIds: ['12345', '67890']);
// Get users by login(s) or ID(s)
$users = Twitch::getUsers(logins: 'username');
// Get the currently authenticated user (after setAccessToken)
$me = Twitch::getAuthenticatedUser();
// Search channels
$results = Twitch::searchChannels('gaming');
OAuth
use Zairakai\LaravelTwitch\Facades\TwitchOAuth;
// Redirect to Twitch authorization
$authUrl = TwitchOAuth::getAuthorizationUrl(['user:read:email', 'channel:read:subscriptions']);
return redirect($authUrl);
// Exchange code for token (in callback controller)
$token = TwitchOAuth::getAccessToken(request('code'));
// Refresh token
$newToken = TwitchOAuth::refreshToken($refreshToken);
EventSub
// Subscribe to events (webhook transport)
Twitch::createEventSubSubscription(
type: 'stream.online',
condition: ['broadcaster_user_id' => $broadcasterId],
callbackUrl: route('twitch.webhook'),
secret: config('twitch.eventsub.webhook_secret'),
);
// Handle webhooks — in routes/web.php
Route::post('/twitch/webhook', [TwitchAuthController::class, 'webhook']);
// Listen to dispatched Laravel events (string-based)
Event::listen('twitch.stream.online', function (array $eventData) {
// handle the stream.online payload
});
Event::listen('twitch.channel.follow', function (array $eventData) {
// handle the channel.follow payload
});
Configuration
Key options in config/twitch.php:
| Key | Description |
|---|---|
client_id | Twitch application client ID |
client_secret | Twitch application client secret |
redirect_uri | OAuth callback URL |
webhook_secret | Secret for EventSub signature verification |
cache_ttl | TTL in seconds for API response caching |
Development
make quality # pint + phpstan + rector + insights + markdownlint + shellcheck
make quality-fast # pint + phpstan + markdownlint
make test # phpunit / pest
Getting Help
Made with ❤️ by Zairakai