TwitchTv

new TwitchTv(options)

This module manages requests to the twitch api in a asynchonos manner
Parameters:
Name Type Description
options Object Options for the TwitchTv object
Properties
Name Type Attributes Description
clientid String <optional>
The client id of the twitch api app
redirecturi String <optional>
The redirect uri of the twitch api app
scope Array <optional>
An array of the permission scopes you might need

Members

channelid :String

The channel id of the logged in user. Until now it's the same as the user id but it might change.

channelobj :null|Object

An object with all twitch channel information. More information in the twitch api documentation.

clientid :String

The twitch api app client id that is necessary for request to the twitch api.

redirecturi :String

The redirect uri that ist set in the app settings.

scope :Array

An array with the required permission scopes.

userid :String

User id of the logged in user.

userobj :null|Object

An object with all twitch user information. More information in the twitch api documentation.

Methods

getAuthImplicitGrantFlowUrl() → {String}

Returns a complete URL for the grant flow authentication and creates a state.
Returns:
String

getAuthToken() → {String}

Returns the twitch user auth token. Used for login in twitch irc or api requests for user related information.
Returns:
String

async getChannel(channelidopt, callback)

Gets a channel by the passed channelid or by the oauth token
Parameters:
Name Type Attributes Description
channelid String | Number <optional>
The channel id of the channel you want to get. Channel names are not supported.
callback TwitchTv~requestCallback
See:

async getChannelFollowers(channelid, options, callback)

Gets users that follow the channel.
Parameters:
Name Type Description
channelid String | Number Channel id of the channel you want the followers of.
options Object Options to control direction of the result.
Properties
Name Type Attributes Default Description
limit Number <optional>
25 Maximum number of objects to return. Maximum: 100.
offset Number <optional>
0 Object offset for pagination of results.
cursor String <optional>
Tells the server where to start fetching the next set of results, in a multi-page response.
direction String <optional>
'desc' Direction of sorting. Valid values: asc (oldest first), desc (newest first).
callback TwitchTv~requestCallback
See:

async getChannelSubscribers(channelid, options, callback)

Gets subscribers to the channel. Logged in user needs to have permission to view channel subscriber or you get a 401 error.
Parameters:
Name Type Description
channelid String | Number Channel id of the channel you want the subscriber of.
options Object Options to control direction of the result.
Properties
Name Type Attributes Default Description
limit Number <optional>
25 Maximum number of objects to return. Maximum: 100.
offset Number <optional>
0 Object offset for pagination of results.
direction String <optional>
'asc' Direction of sorting. Valid values: asc (oldest first), desc (newest first).
callback TwitchTv~requestCallback
See:

async getChatBadgesByChannel(channelid, callback)

Loads channel badges. It's incomplete. If you want better badges use TwitchTv#getChatBadgeSetsByChannel.
Parameters:
Name Type Description
channelid String | Number Channel id of the channel you want the badges of.
callback TwitchTv~requestCallback
See:

async getChatBadgeSetsByChannel(channelid, callback)

A more detailed alternative to TwitchTv#getChatBadgesByChannel. There is no official documentation and is now official api endpoint. See example for an example response.
Parameters:
Name Type Description
channelid String | Number Channel id of the channel you want the badges of.
callback TwitchTv~requestCallback
Example
// Example response:
{
  "badge_sets": {
    "subscriber": {
      "versions": {
        "0": {
          "image_url_1x": "https://static-cdn.jtvnw.net/badges/v1/7821fdef-7fd6-4085-a9b0-eae8952ce299/1",
          "image_url_2x": "https://static-cdn.jtvnw.net/badges/v1/7821fdef-7fd6-4085-a9b0-eae8952ce299/2",
          "image_url_4x": "https://static-cdn.jtvnw.net/badges/v1/7821fdef-7fd6-4085-a9b0-eae8952ce299/3",
          "description": "Subscriber",
          "title": "Subscriber",
          "click_action": "subscribe_to_channel",
          "click_url": ""
        }
      }
    }
  }
}

async getChatEmoticonsBySet(emotesets, callback)

Get emote sets.
Parameters:
Name Type Description
emotesets String Comma seperated list of emote set ids.
callback TwitchTv~requestCallback
See:

async getStreamByUser(channelid, options, callback)

Loads a stream object for a channel
Parameters:
Name Type Description
channelid String | Number Channel id of the channel you want the stream for.
options Object Options to filter by stream type
Properties
Name Type Attributes Default Description
steam_type String <optional>
'live' Constrains the type of streams returned. Valid values: live, playlist, all. Playlists are offline streams of VODs (Video on Demand) that appear live.
callback TwitchTv~requestCallback
See:

async getUser(useridopt, callback)

Gets a user by the passed userid or by the oauth token
Parameters:
Name Type Attributes Description
userid String | Number <optional>
The user id of the user you want to get. Usernames are not supported. Use TwitchTv#getUserByName to get the user by name.
callback TwitchTv~requestCallback
See:

async getUserByName(username, callback)

Gets user objects by name.
Parameters:
Name Type Description
username String Username of the user you want to get. You can pass a comma seperated list (up to 100) to get multiple users at once. Display name is not supported.
callback TwitchTv~requestCallback
See:

async getUserFollows(userid, options, callback)

Get the channels a user follows.
Parameters:
Name Type Description
userid String | Number The user id of the user you want the follows of.
options Object Options to control direction and sorting of the result
Properties
Name Type Attributes Default Description
limit Number <optional>
25 Maximum number of most-recent objects to return. Maximum: 100.
offset Number <optional>
0 Object offset for pagination of results.
direction String <optional>
'desc' Sorting direction. Valid values: asc (oldes first), desc (newest first).
sortby String <optional>
'created_at' Sorting key. Valid values: created_at, last_broadcast, login.
callback TwitchTv~requestCallback
See:

isLoggedIn() → {Boolean}

Returns true if user is logged in.
Returns:
Boolean

async requestAPI(uri, query, authNeeded, postdataopt, callback)

Prepares and executes a request to the twitch api and parses the response
Parameters:
Name Type Attributes Default Description
uri String The URI to the api endpoint. Only the path is required, the host api.twitch.tv is prepended when the uri does not start with https://
query Object An object with all request parameters. Is being encoded for the uri. Must be passed but can be empty.
authNeeded Boolean Is user authorization required for this request. Oauth token is then passed on the request.
postdata Object <optional>
{} Optional post data. If there are properties in this object authNeeded is set to true and request method is set to put. Post data is serialized to a JSON string.
callback TwitchTv~requestCallback The callback that is called after response was processed. If this is not passed no request is being done!
Example
twitchtv.requestAPI(
    // uri
    '/kraken/users/44322889/follows/channels',

    // query
    {
        limit: 100,
        direction: 'desc'
    },

    // authNeeded
    false,

    // callback
    (data, err) => {
        if(err === null) {
            // Do something with the data
        }
    }
)

async searchGames(query, options, callback)

Search for available games
Parameters:
Name Type Description
query String Search query. Must be at least 3 characters long, otherwise request is not being done.
options Object Options for filtering
Properties
Name Type Attributes Default Description
live Boolean <optional>
false If true, returns only games that are live on at least one channel.
callback TwitchTv~requestCallback
See:

setAuthToken(token)

Fills the user token and saves it in the localStorage.
Parameters:
Name Type Description
token String The token that is returned by the OAuth2 auth flow

async updateChannel(channelid, options, callback)

Updates some channel data
Parameters:
Name Type Description
channelid String | Number The channel id of the channel you want to update. The logged in user must have permission for that or an http error 401 is returned.
options Object Options that should be changed. At least one property is required or no request is made (and therefore no callback called).
Properties
Name Type Attributes Description
status String <optional>
Description of the broadcaster's status, displayed as a title on the channel page.
game String <optional>
Name of game.
delay String <optional>
Channel delay, in seconds. This inserts a delay in the live feed. Only channel owner can change this.
channel_feed_enabled Boolean <optional>
If true, the channel's feed is turned on. Only channel owner can change this.
callback TwitchTv~requestCallback
See:

verifyState(state) → {Boolean}

Verifys if this returned state is one of the valid states and removes it from the local list.
Parameters:
Name Type Description
state String The state that is returned by the OAuth auth flow
Returns:
Boolean

Type Definitions

requestCallback(data, error)

Parameters:
Name Type Description
data null | Object Has the parsed api response. Can have the api error response. Please check error for errors.
error null | Error Null if no error