Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

twitch-api

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

twitch-api

Simple module for using twitch.tv's REST API in node.js

  • 0.5.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
decreased by-54.17%
Maintainers
1
Weekly downloads
 
Created
Source

twitch-api

Module for easily using all twitch.tv API v3 endpoints in nodejs

Installation

npm install twitch-api

Usage

Follow the Authorization Code Flow that you can find in the official twitch.tv API v3 documentation:

  1. Send the user you'd like to authenticate to twitch.tv's authentication URL (you can get this URL using the convenience method getAuthorizationUrl once the module is initiallized)
  2. If the user authorizes your application, she will be redirected to https://[your registered redirect URI]/?code=[CODE]. There you can get the code you need to get the user's access token.

Step 1: Initialization

var TwitchApi = require('twitch-api');
var twitch = new TwitchApi({
    clientId: 'your client id',
    clientSecret: 'your client secret',
    redirectUri: 'same redirectUri that you have configured on your app',
    scopes: [array of scopes you want access to]
  });

Step 2: Get the user's access token

twitch.getAccessToken(code, function(err, body){
    if (err){
      console.log(err);
    } else {
      /*
      * body = {
      *   access_token: 'your authenticated user access token',
      *   scopes: [array of granted scopes]
      * }
      */
    }
});

Once you have your user's access token, you can use it to query any authenticated resource the user has (and has granted you) access to.

Methods

requestCallback (Callback)

The callback that will handle the response

Params
NameTypeOptionalDescription
errObjectFalseIn request produces an error, it will be stored in this parameter. null if the request was successful
bodyObjectFalseThe body of the response if the request was successful

getAuthorizationUrl (Function)

Returns the full URL to which you must send your user in order to authorize your application

var getauthorizationurl = getAuthorizationUrl();
Returns
NameTypeDescription
returnStringThe the full URL to which you must send your user for authorization

getAccessToken (Function)

Requests Twitch.tv for an accessCode for your authorized user

getAccessToken(code, callback);
Params
NameTypeOptionalDescription
codeStringFalseThe code that twitch.tv's API sent in the redirection URI parameters when the user authorized your application
callbackrequestCallbackFalseThe callback that will manage the response

refreshAccessToken (Function)

Requests Twitch.tv for an accessCode for using your refresh token

refreshAccessToken(refreshToken, callback);
Params
NameTypeOptionalDescription
refreshTokenStringFalseThe code that twitch.tv's API sent in the getAccessToken response
callbackrequestCallbackFalseThe callback that will manage the response

getBlocks (Function)

Get user's block list

API endpoint: GET /users/:user/blocks

getBlocks(user, accessToken, [parameters], callback);
Params
NameTypeOptionalDescription
userStringFalseThe user name of wich you want to get the block list, authenticated by accesToken
accessTokenStringFalseThe token representing the authenticated user
parametersObjectTrueThe parameters of the API endpoint
callbackrequestCallbackFalseThe callback that will manage the response

addBlock (Function)

Add target to user's block list

API endpoint: PUT /users/:user/blocks/:target

addBlock(user, accessToken, target, callback);
Params
NameTypeOptionalDescription
userStringFalseThe user name of the user
accessTokenStringFalseThe token representing the authenticated user
targetStringFalsethe user name your user wants to block
callbackrequestCallbackFalseThe callback that will manage the response

removeBlock (Function)

Delete target from user's block list

API endpoint: DELETE /users/:user/blocks/:target

removeBlock(user, accessToken, target, callback);
Params
NameTypeOptionalDescription
userStringFalseThe user name of the user
accessTokenStringFalseThe token representing the authenticated user
targetStringFalsethe user name your user wants to unblock
callbackrequestCallbackFalseThe callback that will manage the response

getChannel (Function)

Returns a channel object.

API endpoint: GET /channels/:channel/

getChannel(channel, callback);
Params
NameTypeOptionalDescription
channelStringFalseThe channel name
callbackrequestCallbackFalseThe callback that will manage the response

getAuthenticatedUserChannel (Function)

Returns a channel object of authenticated user. Channel object includes stream key.

API endpoint: GET /channel

getAuthenticatedUserChannel(accessToken, callback);
Params
NameTypeOptionalDescription
accessTokenStringFalseThe token representing the authenticated user
callbackrequestCallbackFalseThe callback that will manage the response

getChannelEditors (Function)

Returns a list of user objects who are editors of channel. The user should be the owner (maybe editor?) of the channel

API endpoint: GET /channels/:channel/editors

getChannelEditors(channel, accessToken, callback);
Params
NameTypeOptionalDescription
channelStringFalseThe channel name
accessTokenStringFalseThe token representing the authenticated user
callbackrequestCallbackFalseThe callback that will manage the response

updateChannel (Function)

Update channel's status or game.

API endpoint: PUT /channels/:channel/

updateChannel(channel, accessToken, channelOptions, callback);
Params
NameTypeOptionalDescription
channelStringFalseThe channel name
accessTokenStringFalseThe token representing the authenticated user
channelOptionsObjectFalseThe options you want to change in JSON format
channelOptions.channelJSONFalseThe real options are wrapped here
channelOptions.channel.statusStringFalseThe new status of the channel
channelOptions.channel.gameStringFalseThe new game of the channel
channelOptions.channel.delayNumberFalseThe delay of the channel
callbackrequestCallbackFalseThe callback that will manage the response

resetStreamKey (Function)

Resets channel's stream key.

API endpoint: DELETE /channels/:channel/stream_key

resetStreamKey(channel, accessToken, callback);
Params
NameTypeOptionalDescription
channelStringFalseThe channel name
accessTokenStringFalseThe token representing the authenticated user
callbackrequestCallbackFalseThe callback that will manage the response

startCommercial (Function)

Start commercial on channel.

API endpoint: POST /channels/:channel/commercial

startCommercial(channel, accessToken, [parameters], callback);
Params
NameTypeOptionalDescription
channelStringFalseThe channel name
accessTokenStringFalseThe token representing the authenticated user
parametersObjectTrueThe parameters of the request
parameters.lengthNumberTrueThe length of the commercial break in seconds. One of 30, 60, 90, 120, 150 or 180. Defaults to 30.
callbackrequestCallbackFalseThe callback that will manage the response

getChannelTeams (Function)

Returns a list of team objects channel belongs to.

API endpoint: GET /channels/:channel/teams

getChannelTeams(channel, callback);
Params
NameTypeOptionalDescription
channelStringFalseThe channel name
callbackrequestCallbackFalseThe callback that will manage the response

getChannelChat (Function)

Returns a links object to all other chat endpoints.

API endpoint: GET /chat/:channel

getChannelChat(channel, callback);
Params
NameTypeOptionalDescription
channelStringFalseThe channel name
callbackrequestCallbackFalseThe callback that will manage the response

getEmoticons (Function)

Returns a list of all emoticon objects for Twitch.

API endpoint: GET /chat/emoticons

getEmoticons(callback);
Params
NameTypeOptionalDescription
callbackrequestCallbackFalseThe callback that will manage the response

getChannelBadges (Function)

Returns a list of chat badges that can be used in the channel's chat.

API endpoint: GET /chat/:channel/badges

getChannelBadges(channel, callback);
Params
NameTypeOptionalDescription
channelStringFalseThe channel name
callbackrequestCallbackFalseThe callback that will manage the response

getChannelFollows (Function)

Returns a list of follow objects.

API endpoint: GET /channels/:channel/follows

getChannelFollows(channel, [parameters], callback);
Params
NameTypeOptionalDescription
channelStringFalseThe channel name
parametersObjectTrueThe parameters of the request
parameters.limitNumberTrueMaximum number of follow objects. Maximum is 100, defaults to 25
parameters.offsetNumberTrueFollow object offset for pagination. Defaults to 0
parameters.directionStringTrueCreation date sorting direction. Defaults to desc. Valid values are asc and desc.
callbackrequestCallbackFalseThe callback that will manage the response

getUserFollowedChannels (Function)

Returns a list of follow objects

API endpoint: GET /users/:user/follows/channels

getUserFollowedChannels(user, [parameters], callback);
Params
NameTypeOptionalDescription
userStringFalseThe user name
parametersObjectTrueThe parameters of the request
parameters.limitNumberTrueMaximum number of follow objects. Maximum is 100, defaults to 25
parameters.offsetNumberTrueFollow object offset for pagination. Defaults to 0
parameters.directionStringTrueCreation date sorting direction. Defaults to desc. Valid values are asc and desc.
parameters.sortbyStringTrueSort key. Defaults to created_at. Valid values are created_at, last_broadcast, and login. Defaults to desc. Valid values are asc and desc.
callbackrequestCallbackFalseThe callback that will manage the response

getUserFollowsChannel (Function)

Returns a follow object if user is following channel, 404 otherwise.

API endpoint: GET /users/:user/follows/channels/:target

getUserFollowsChannel(user, channel, callback);
Params
NameTypeOptionalDescription
userStringFalseThe user name
channelStringFalseThe channel name
callbackrequestCallbackFalseThe callback that will manage the response

userFollowChannel (Function)

Adds user to channel's followers.

API endpoint: PUT /users/:user/follows/channels/:target

userFollowChannel(user, channel, accessToken, [parameters], callback);
Params
NameTypeOptionalDescription
userStringFalseThe user name
channelStringFalseThe channel name
accessTokenStringFalseThe token representing the authenticated user
parametersObjectTrueThe parameters of the request
parameters.notificationsbooleanTrueWhether user should receive notifications when channel goes live. Defaults to false.
callbackrequestCallbackFalseThe callback that will manage the response

userUnfollowChannel (Function)

Removes user from channel's followers.

API endpoint: DELETE /users/:user/follows/channels/:target

userUnfollowChannel(user, channel, accessToken, callback);
Params
NameTypeOptionalDescription
userStringFalseThe user name
channelStringFalseThe channel name
accessTokenStringFalseThe token representing the authenticated user
callbackrequestCallbackFalseThe callback that will manage the response

getTopGames (Function)

Returns a list of games objects sorted by number of current viewers on Twitch, most popular first.

API endpoint: GET /games/top

getTopGames([parameters], callback);
Params
NameTypeOptionalDescription
parametersObjectTrueThe parameters of the request
parameters.limitNumberTrueMaximum number of games. Maximum is 100, defaults to 25
parameters.offsetNumberTrueFollow object offset for pagination. Defaults to 0
callbackrequestCallbackFalseThe callback that will manage the response

getIngests (Function)

Returns a list of ingest objects.

API endpoint: GET /ingests/

getIngests(callback);
Params
NameTypeOptionalDescription
callbackrequestCallbackFalseThe callback that will manage the response

getRoot (Function)

Basic information about the API and authentication status. If you are accessToken is provided, the response includes the status of your token and links to other related resources.

API endpoint: GET /

getRoot([accessToken], callback);
Params
NameTypeOptionalDescription
accessTokenStringTrueThe token representing the authenticated user
callbackrequestCallbackFalseThe callback that will manage the response

searchChannels (Function)

Returns a list of channel objects matching the search query.

API endpoint: GET /search/channels

searchChannels(parameters, callback);
Params
NameTypeOptionalDescription
parametersObjectFalseThe parameters of the request
parameters.queryStringFalseSearch query. The field can also be parameters.q
parameters.limitNumberTrueMaximum number of channel objects. Maximum is 100, defaults to 25
parameters.offsetNumberTrueFollow object offset for pagination. Defaults to 0
callbackrequestCallbackFalseThe callback that will manage the response

searchStreams (Function)

Returns a list of stream objects matching the search query.

API endpoint: GET /search/streams

searchStreams(parameters, callback);
Params
NameTypeOptionalDescription
parametersObjectFalseThe parameters of the request
parameters.queryStringFalseSearch query. The field can also be parameters.q
parameters.limitNumberTrueMaximum number of stream objects. Maximum is 100, defaults to 25
parameters.offsetNumberTrueFollow object offset for pagination. Defaults to 0
parameters.hlsbooleanTrueIf set to true, only returns streams using HLS. If set to false, only returns streams that are non-HLS.
callbackrequestCallbackFalseThe callback that will manage the response

searchGames (Function)

Returns a list of game objects matching the search query.

API endpoint: GET /search/games

searchGames(parameters, callback);
Params
NameTypeOptionalDescription
parametersObjectFalseThe parameters of the request
parameters.queryStringFalseSearch query. The field can also be parameters.q
parameters.typeStringFalseOnly accepts suggest: Suggests a list of games similar to query
parameters.livebooleanTrueIf true, only returns games that are live on at least one channel.
callbackrequestCallbackFalseThe callback that will manage the response

getChannelStream (Function)

Returns a stream object if live.

API endpoint: GET /streams/:channel/

getChannelStream(channel, callback);
Params
NameTypeOptionalDescription
channelStringFalseThe channel name
callbackrequestCallbackFalseThe callback that will manage the response

getStreams (Function)

Returns a list of stream objects that are queried by a number of parameters sorted by number of viewers descending.

API endpoint: GET /streams

getStreams([parameters], callback);
Params
NameTypeOptionalDescription
parametersObjectTrueThe parameters of the request
parameters.gameStringTrueStreams categorized under game.
parameters.channelStringTrueStreams from a comma separated list of channels.
parameters.limitNumberTrueMaximum number of streams. Maximum is 100, defaults to 25
parameters.offsetNumberTrueFollow object offset for pagination. Defaults to 0
parameters.client_idStringTrueOnly shows streams from applications of client_id.
callbackrequestCallbackFalseThe callback that will manage the response

getFeaturedStreams (Function)

Returns a list of featured (promoted) stream objects.

API endpoint: GET /streams/featured

getFeaturedStreams([parameters], callback);
Params
NameTypeOptionalDescription
parametersObjectTrueThe parameters of the request
parameters.limitNumberTrueMaximum number of streams. Maximum is 100, defaults to 25
parameters.offsetNumberTrueFollow object offset for pagination. Defaults to 0
callbackrequestCallbackFalseThe callback that will manage the response

getStreamsSummary (Function)

Returns a summary of current streams.

API endpoint: GET /streams/summary

getStreamsSummary([parameters], callback);
Params
NameTypeOptionalDescription
parametersObjectTrueThe parameters of the request
parameters.gameStringTrueStreams categorized under game.
callbackrequestCallbackFalseThe callback that will manage the response

getChannelSubscriptions (Function)

Returns a list of subscription objects sorted by subscription relationship creation date which contain users subscribed to channel.

API endpoint: GET /channels/:channel/subscriptions

getChannelSubscriptions(channel, accessToken, [parameters]);
Params
NameTypeOptionalDescription
channelStringFalseThe channel name
accessTokenStringFalseThe token representing the authenticated user
parametersObjectTrueThe parameters of the request
parameters.limitNumberTrueMaximum number of subscription objects. Maximum is 100, defaults to 25
parameters.offsetNumberTrueFollow object offset for pagination. Defaults to 0
parameters.directionStringTrueCreation date sorting direction. Defaults to asc. Valid values are asc and desc.

getUserSubscriptionToChannel (Function)

Returns a subscription object which includes the user if that user is subscribed. Requires authentication for channel. The authenticated user must be the owner of the channel

API endpoint: GET /channels/:channel/subscriptions/:user

getUserSubscriptionToChannel(user, channel, accessToken, callback);
Params
NameTypeOptionalDescription
userStringFalseThe user name
channelStringFalseThe channel name
accessTokenStringFalseThe token representing the authenticated user
callbackrequestCallbackFalseThe callback that will manage the response

getChannelSubscriptionOfUser (Function)

Returns a channel object that user subscribes to. user must be authenticated by accessToken.

API endpoint: GET /users/:user/subscriptions/:channel

getChannelSubscriptionOfUser(user, channel, accessToken, callback);
Params
NameTypeOptionalDescription
userStringFalseThe user name
channelStringFalseThe channel name
accessTokenStringFalseThe token representing the authenticated user
callbackrequestCallbackFalseThe callback that will manage the response

getTeams (Function)

Returns a list of active teams.

API endpoint: GET /teams/

getTeams([parameters], callback);
Params
NameTypeOptionalDescription
parametersObjectTrueThe parameters of the request
parameters.limitNumberTrueMaximum number of teams. Maximum is 100, defaults to 25
parameters.offsetNumberTrueFollow object offset for pagination. Defaults to 0
callbackrequestCallbackFalseThe callback that will manage the response

getTeam (Function)

Returns a team object for team.

API endpoint: GET /teams/:team/

getTeam(team, callback);
Params
NameTypeOptionalDescription
teamStringFalseThe team name
callbackrequestCallbackFalseThe callback that will manage the response

getUser (Function)

Returns a user object.

API endpoint: GET /users/:user

getUser(user, callback);
Params
NameTypeOptionalDescription
userStringFalseThe user name of the user
callbackrequestCallbackFalseThe callback that will manage the response

getAuthenticatedUser (Function)

Returns a user object that represents the user authenticated by accessToken . API endpoint: GET /user

getAuthenticatedUser(accessToken, callback);
Params
NameTypeOptionalDescription
accessTokenStringFalseThe token representing the authenticated user
callbackrequestCallbackFalseThe callback that will manage the response

getAuthenticatedUserFollowedStreams (Function)

Returns a list of stream objects that the authenticated user is following.

API endpoint: GET /streams/followed

getAuthenticatedUserFollowedStreams(accessToken, [parameters], callback);
Params
NameTypeOptionalDescription
accessTokenStringFalseThe token representing the authenticated user
parametersObjectTrueThe parameters of the request
parameters.limitNumberTrueMaximum number of streams. Maximum is 100, defaults to 25
parameters.offsetNumberTrueFollow object offset for pagination. Defaults to 0
parameters.stream_typeStringTrueOnly shows streams from a certain type. Permitted values: all, playlist, live
callbackrequestCallbackFalseThe callback that will manage the response

getAuthenticatedUserFollowedVideos (Function)

Returns a list of video objects from channels that the authenticated user is following.

API endpoint: GET /videos/followed

getAuthenticatedUserFollowedVideos(accessToken, [parameters], callback);
Params
NameTypeOptionalDescription
accessTokenStringFalseThe token representing the authenticated user
parametersObjectTrueThe parameters of the request
parameters.limitNumberTrueMaximum number of videos. Maximum is 100, defaults to 25
parameters.offsetNumberTrueFollow object offset for pagination. Defaults to 0
callbackrequestCallbackFalseThe callback that will manage the response

getVideo (Function)

Returns a video object.

API endpoint: GET /videos/:id

getVideo(videoId, callback);
Params
NameTypeOptionalDescription
videoIdStringFalseThe id of the video
callbackrequestCallbackFalseThe callback that will manage the response

getTopVideos (Function)

Returns a list of videos created in a given time period sorted by number of views, most popular first.

API endpoint: GET /videos/top

getTopVideos([parameters], callback);
Params
NameTypeOptionalDescription
parametersObjectTrueThe parameters of the request
parameters.limitNumberTrueMaximum number of videos. Maximum is 100, defaults to 25
parameters.offsetNumberTrueFollow object offset for pagination. Defaults to 0
parameters.gameStringTrueReturns only videos from game.
parameters.periodStringTrueReturns only videos created in time period. Valid values are week, month, or all. Defaults to week.
callbackrequestCallbackFalseThe callback that will manage the response

getChannelVideos (Function)

Returns a list of videos ordered by time of creation, starting with the most recent from channel.

API endpoint: GET /channels/:channel/videos

getChannelVideos(channel, [parameters], callback);
Params
NameTypeOptionalDescription
channelStringFalseThe channel name
parametersObjectTrueThe parameters of the request
parameters.limitNumberTrueMaximum number of videos. Maximum is 100, defaults to 25
parameters.offsetNumberTrueFollow object offset for pagination. Defaults to 0
parameters.broadcastsbooleanTrueReturns only broadcasts when true, only highlights when false. Defaults to false.
parameters.hlsbooleanTrueIf set to true, only returns streams using HLS. If set to false, only returns streams that are non-HLS.
callbackrequestCallbackFalseThe callback that will manage the response

Keywords

FAQs

Package last updated on 04 Nov 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc