Examples
Authorization Code Flow
Guide users through the standard Twitch OAuth 2.0 authorization code flow — print a consent URL, exchange the code for tokens, and refresh them later.
Environment
TWITCH_CLIENT_ID, TWITCH_CLIENT_SECRET
Get started
- Install dependencies in the repository:
bun install. - Create a Twitch application (or reuse an existing one) and copy its
Client IDandClient Secret. - Expose the credentials to the environment, for example by adding them to a
.envfile or exporting them in your shell. - Make sure the redirect URI in the example (
http://localhost:3000/auth/callback) is registered in the Twitch Developer Console, or update the value in the script to match your app configuration. - Run the guide with
bun run examples/AUTHORIZATION_CODE_FLOW.tsand follow the console instructions to authorize your account and exchange the returned code.
What you can do
- Generate user-friendly consent URLs that pre-populate scopes for your integration.
- Exchange authorization codes for access and refresh tokens with detailed success logging.
- Refresh user access tokens as they expire to keep Helix and chat operations running.
- Embed the provided callback handler snippet into an Express or Fastify application.
Next steps
- Persist the returned refresh token securely in your database or secret store.
- Reuse the
TwitchAuthinstance with the Helix or chat clients to make authenticated calls. - Add additional scopes such as
channel:manage:broadcastbefore generating the authorization URL.