This is a NodeCG bundle.
Lets other bundles easily query the Twitch API on behalf of a logged in user. Requires Twitch authentication to be enabled in your NodeCG installation.
- Install to
nodecg/bundles/lfg-twitchapi - Create
nodecg/cfg/lfg-twitchapi.jsonwith a valid configuration (see below).
{
"username": "langeh"
}First, ensure that your NodeCG installation is using Twitch authentication. See the NodeCG configuration docs for details.
Then, add lfg-twitchapi as a bundleDependency in your bundle's nodecg.json
Now, add the following code to your bundle's extension:
const twitchApi = nodecg.extensions['lfg-twitchapi'];
// Gets the 25 most recent subs
// {{username}} will be automatically replaced by the username specified in lfg-twitchapi.json
twitchApi.get('/channels/{{username}}/subscriptions', {
limit: 25,
direction: 'desc'
}).then(response => {
if (response.statusCode !== 200) {
return nodecg.log.error(response.body.error, response.body.message);
}
// Go through subs in reverse, from oldest to newest
response.body.subscriptions.reverse().forEach(subscription => {
const username = subscription.user.name;
console.log('%s subscribed to channel %s', username, twitchApi.channel);
});
}).catch(err => {
nodecg.log.error(err);
});Makes a call to the Twitch API and returns a promise.
If {{username}} is present in path, it will be replaced with the value of nodecg.bundleConfig.username.
pathis the desired endpoint; ex:/channels/{{username}}/subscriptionsqs(Optional) is an object of query parameters; ex:{limit: 25, direction: 'desc'}body(Optional) is an object to send as the JSON request body
lfg-twitchapi is provided under the MIT license, which is available to read in the [LICENSE][] file. [license]: LICENSE