Twitch

Methods for the New Twitch API.

TIP

You first need to create a Twitch application

Register API keys

import { registerTwitchApiKeys } from '@kocal/web-extension-library';

// If you are building an extension for a lot of users (~4000 or more),
// it's advised to create multiple Twitch application to prevent API calls limitations
registerTwitchApiKeys(['<your api key>', '<your second api key if needed>']);

Picking an API key

Returns randomly an API key. It throws an error if you forgot to call registerTwitchApiKeys.

WARNING

Normally you don't have to call this method by yourself, this is an internal method used by getTwitchGame or getTwitchLiveStreams.

import { pickTwitchApiKey } from '@kocal/web-extension-library';

pickTwitchApiKey();

Getting information on a Game

import { getTwitchGame } from '@kocal/web-extension-library';

getTwitchGame('21779')
  // If everything is fine
  .then(game => {
    // {
    //   id: '21779',
    //   name: 'League of Legends',
    //   box_art_url: 'https://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-{width}x{height}.jpg',
    // }
  })
  // If game does not exist
  .catch(err => {
    // 'Twitch Game "21779" does not exist.'
  });

Getting live streams

You must pass an array of user id. User id can be retrieved with Get Users (Twitch API) and by specifying login query parameter.

For each online streams, a call to getTwitchGame is automatically made in order to make your life easier.

import { getTwitchLiveStreams } from '@kocal/web-extension-library';

// https://twitch.tv/solary
const solaryUserId = 174955366;
// https://twitch.tv/solaryfortnite
const solaryFortniteUserId = 198506129;

getTwitchLiveStreams([solaryUserId, solaryFortniteUserId])
  .then(({ onlineStreams, offlineStreams }) => {
    console.log(offlineStreams); // [198506129], Solary Fortnite is offline
    console.log(onlineStreams);
    // [
    //   {
    //     id: '30435956192',
    //     user_id: '174955366',
    //     game_id: '21779',
    //     community_ids: [],
    //     type: 'live',
    //     title: 'TIOO NUCLEAR SMURFING',
    //     viewer_count: 2169,
    //     started_at: '2018-09-20T07:01:47Z',
    //     language: 'fr',
    //     thumbnail_url: 'https://static-cdn.jtvnw.net/previews-ttv/live_user_solary-{width}x{height}.jpg',
    //     game: {
    //       id: '21779',
    //       name: 'League of Legends',
    //       box_art_url: 'https://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-{width}x{height}.jpg',
    //     },
    //   },
    // ]
  })