ATTENTION: This library is still under initial development.
Public APIs may break with any release that increases y in version 0.y.z until this library reaches 1.0 (as per SemVer).
To add Twitch.js to your project, just execute:
yarn add twitch
The first thing you do is constructing a Twitch client instance. The easiest way to get one is to supply static credentials:
import TwitchClient from 'twitch';
const clientId = '123abc';
const accessToken = 'def456';
const twitchClient = TwitchClient.withCredentials(clientId, accessToken);
You can also have the client refresh the tokens automatically if necessary by supplying an additional parameter containing the necessary data:
import TwitchClient, {AccessToken} from 'twitch';
const clientId = '123abc';
const accessToken = 'def456';
const clientSecret = 'foobar';
const refreshToken = '999999';
const twitchClient = TwitchClient.withCredentials(clientId, accessToken, {clientSecret, refreshToken, onRefresh: (token: AccessToken) => {
// do things with the new token data, e.g. save them in your database
}});
The following sections assume that you have created a twitchClient
already.
The API methods are organized into the different API subresources, like /users
and /streams
. All the API methods are async
and thus can be awaited. Have a quick example:
async function isStreamLive(userName: string) {
try {
const user = await twitchClient.users.getUserByName(userName);
await twitchClient.streams.getStreamByChannel(user.id); // will reject the promise if the stream is not live
return true;
} catch (e) {
return false;
}
}
We make extensive use of convenience methods that fetch related resources, so this can also be written a bit easier:
async function isStreamLive(userName: string) {
try {
const user = await twitchClient.users.getUserByName(userName);
await user.getStream(); // will reject the promise if the stream is not live
return true;
} catch (e) {
return false;
}
}
TODO
TODO
You can join the Discord Server for support.