Skip to main content

Twitch

"Twitch is an interactive livestreaming service for content spanning gaming, entertainment, sports, music, and more."

Configuration​

Getting Twitch OAuth credentials​

Start by accessing or creating a Twitch account.

If you do not already have a Client Id and Client Secret, you will first need to create them. Start by registering an app.

Note: You will need to set up 2 factor auth in order to create Twitch OAuth credentials.

Set your redirect url to the following:

http://localhost

Once you have registered an application in the Twitch Developer console, click on the "Applications" tab, then "Manage" and note your Client Id and Client Secret for the next step.

NOTE: The Client Secret will no longer be visible after it is shown for the first time so be sure to temporarily copy it for the next step.

Configuring Twitch OAuth Credentials in crul​

Navigate to the credentials page in crul (top right corner menu -> credentials).

Click the "create" button.

Select "OAuth" from the type dropdown, then select "Twitch" from the provider dropdown.

Copy the Client Id into the Client Id credentials form entry. Then copy the Client Secret into the Client Secret credentials form entry.

Test the credential by running the following query. You should see a populated token.access_token column in the results.

oauth --credential "twitch"

NOTE: The Twitch API requires that each request includes the Client Id in the request headers, you can either hardcode this value using the headers flag:

oauth --credential "twitch"
|| api get https://api.twitch.tv/helix/streams
--token.access_token "$token.access_token$"
--headers '{"Client-Id": "asdfghjklqwertyuiopzxcvbnm"}'

Or store as a custom key/value credential and access using a token:

oauth --credential "twitch"
|| api get https://api.twitch.tv/helix/streams
--token.access_token "$token.access_token$"
--headers '{"Client-Id": "$CREDENTIALS.{credential name}$"'

Example queries​

NOTE: For the following example queries, it is assumed that you have configured a custom key/value credential with a name/key of twitch_client_id and a value of your Twitch Client Id.

These examples use the /helix/streams endpoint further described in the Twitch API reference.

Example 1:​

Gets the top 100 streams by viewer count.

oauth --credential "twitch"
|| api get https://api.twitch.tv/helix/streams?first=100
--token.access_token "$access_token$"
--headers '{"Client-Id": "$CREDENTIALS.twitch_client_id$"}'

Example 2:​

Gets the first 5 pages of streams (top 500 streams).

oauth --credential "twitch"
|| api get https://api.twitch.tv/helix/streams?first=100
--token.access_token "$access_token$"
--headers '{"Client-Id": "$CREDENTIALS.twitch_client_id$"}'
--pagination.max 5
--pagination.next "pagination.cursor"
--pagination.url "https://api.twitch.tv/helix/streams?first=100&after=$pagination.next$"

Example 3:​

List the first 100 Music streams (game_id=26936), then join each stream with the broadcaster user profile information.

oauth --credential "twitch"
|| api get https://api.twitch.tv/helix/streams?first=100&game_id=26936
--token.access_token "$token.access_token$"
--headers '{"Client-Id": "$CREDENTIALS.twitch_client_id$"}'
|| normalize data --labelStage streams
|| mergecolumn user_ids "&id=" user_id --maxRows 100
|| api get https://api.twitch.tv/helix/users?id=$user_ids$
--token.access_token "$token.access_token$"
--headers '{"Client-Id": "$CREDENTIALS.twitch_client_id$"}'
|| normalize data
|| rename id user_id
|| join id streams