TWITCH-API
The twitch API v5 made easy
Twitch Class
Starting
The class object takes two parameters in the object, the client ID and the client secret. Create the a new object with the correct parameters to use the class.
The package dotenv is recommended for keeping your client information secret.
const Twitch = ;const twitch = id: "YOUR ID HERE" secret: "YOUR SECRET HERE";
Methods
METHOD | DESCRIPTION |
---|---|
.getUser(user) |
Returns information about a user |
.getFeaturedStreams(options*) |
Returns twitch's featured streams |
.getTopStreams(options*) |
Returns the current top streams |
.getTopGames(options*) |
Returns the top games |
.getUsersByGame(game) |
Returns users by game |
.getStreamUrl(user) |
Returns the RTMP stream URL |
.searchChannels(query, limit*, offset*) |
Returns a list of channels |
.searchStreams(query, limit*, offset*) |
Returns a list of streams |
.searchGames(query, live*) |
Returns a list of games |
*Parameter is optional or has default values.
Using
The twitch api module uses promises to resolve/reject data.
const Twitch = ;const twitch = id: "YOUR ID HERE" secret: "YOUR SECRET HERE"; twitch ; // making requests with optional parametersconst optionalParams = game: 'StarCraft: Brood War' language: 'es';twitch ; twitch ; // non es6twitch ; // using async/await { let data = await twitch; console;} // with error handling { try let data = await twitch; console; catcherr throw err; }
Example
Here is an example of routing the requests through a Hapi server.
;const Hapi = ;const Twitch = ; const server = {} const twitch = id: processenvTWITCH_ID secret: processenvTWITCH_SECRET; server; server; serverstart { if err throw err; console;};