twitch-helix
Little helper class for the new Twitch API described in current Twitch API docs.
Transpiled and minified with Babel.
Feel free to contribute by creating issues and pull requests.
Installation
Yarn (recommended)
yarn add twitch-helix
npm
npm install --save twitch-helix
Library Usage
Example
Import the default class from this package and feed its constructor with a client ID and a client secret. You can generate those in your Twitch Developers Dashboard.
Try it out on RunKit!
import TwitchHelix from "twitch-helix"
// Or: const TwitchHelix = require("twitch-helix")
const twitchApi = new TwitchHelix({
clientId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
clientSecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
})
twitchApi.getTwitchUserByName("nightbot").then(twitchUser => {
console.log(twitchUser.display_name) // Prints "Nightbot"
})
Construction
TwitchHelix is a class and you need to create an instance with:
const twitchApi = new TwitchHelix(options)
The options parameter is an object and can have following fields:
Field | Info | Default value |
---|---|---|
clientId |
Client ID of your Twitch app | 🚫 (required) |
clientSecret |
Client secret of your Twitch app | 🚫 (required) |
prematureExpirationTime |
Time in ms for the access token to expire before it is meant to (Not implemented yet) | 10000 |
autoAuthorize |
Will call automatically call authorize() when needed | true |
smartRetry |
Will retry Twitch API requests up to 10 times if the server response is invalid | true |
Implemented queries
Some of the common queries are wrapped into neat functions. Those are:
Promise | Name | Parameters | Return Value | Further info |
---|---|---|---|---|
Yes | getTwitchUserById |
string id |
Twitch user info object | |
Yes | getTwitchUserByName |
string username |
Twitch user info object | |
Yes | getTwitchUsersByName |
Array usernames |
Array of Twitch user info objects | |
Yes | getStreamInfoById |
string id |
Twitch stream object if user is currently streaming or null otherwise |
|
Yes | getStreamInfoByUsername |
string username |
Twitch stream object if user is currently streaming or null otherwise |
|
Yes | getFollowDate |
string streamerId , string followerId |
Date if follower follows streamer or null otherwise |
Custom queries
You may need custom queries for retrieving data that is not wrapped into a function yet. Feel free to do so. Some API endpoints are still not implemented by Twitch in Helix API, so you can also use Kraken v5 API.
const helixQueryData = await twitchApi.sendHelixRequest("users?login=nightbot&login=moobot")
const krakenQueryData = await twitchApi.sendApiRequest("users?login=nightbot,moobot", {api: "kraken"})
Events
You can listen to some events.
twitchApi.on(eventName, eventHandler)
Event name | Parameters | Description |
---|---|---|
log-info |
message |
Emitted on INFO log messages |
log-warn |
message |
Emitted on WARN log messages |
log-error |
message |
Emitted on ERROR log messages |
Command Line Usage
Here is an example:
node_modules/.bin/twitch-helix --client-id xxx --client-secret xxx "users?login=nightbot"
Use the --kraken
flag to query data from a Kraken endpoint instead of a Helix endpoint.