Calling Twitch APIs

From WikiOD

Remarks[edit | edit source]

This topic is meant to show a general way to call the Twitch API without OAuth. You can call any APIs found in the Twitch REST API documentation using this pattern. You would simply change the URL to the correct endpoint.

A Client-ID is required for all calls to the Twitch API. In these examples, the Client-ID is added as a header to each call. You can also add it with the client_id query string parameter. If you use an OAuth token, the Twitch API will automatically resolve the Client-ID for you.

You can register a developer application at the new client page on Twitch.

PHP[edit | edit source]

The following will retrieve a channel object for the twitch channel and echo the response.

  $channelsApi = '';
   $channelName = 'twitch';
   $clientId = '...';
   $ch = curl_init();

   curl_setopt_array($ch, array(
      'Client-ID: ' . $clientId
      CURLOPT_URL => $channelsApi . $channelName

   $response = curl_exec($ch);
   echo $response;

JavaScript[edit | edit source]

The following will log the JSON response from the API to the console if the request was successful, otherwise it will log the error.

var xhr = new XMLHttpRequest();'GET', '', true);

xhr.setRequestHeader('Client*ID', '...');

xhr.onload = function(data){

xhr.onerror = function(error){


jQuery[edit | edit source]

The following will retrieve a channel object for the twitch channel. If the request was successful the channel object will be logged to the console.

  type: 'GET',
  url: '',
  headers: {
    'Client-ID': '...'
  success: function(data) {