Created
March 3, 2022 09:54
-
-
Save toshvelaga/1b094428a7a6a0c968ffe722886a681d to your computer and use it in GitHub Desktop.
server side node function to get view count from twitch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'), | |
router = express.Router(), | |
{ default: axios } = require('axios') | |
require('dotenv').config() | |
router.post('/api/twitch/view-count', async (req, res) => { | |
// twitch forum: https://discuss.dev.twitch.tv/t/viewer-counter-help/24726/2 | |
// https://discuss.dev.twitch.tv/t/getting-stream-viewer-count-webhook-notifications/20645/5 | |
const twitchUsername = req.body.twitchUsername | |
const twitchAccessToken = req.body.twitchAccessToken | |
let viewCount = await axios | |
.get(`https://api.twitch.tv/helix/streams?user_login=${twitchUsername}`, { | |
headers: { | |
Authorization: `Bearer ${twitchAccessToken}`, | |
'Client-Id': process.env.TWITCH_CLIENT_ID, | |
'Content-Type': 'application/json', | |
}, | |
}) | |
.then((res) => { | |
console.log(res.data.data[0].viewer_count) | |
return res.data.data[0].viewer_count | |
}) | |
.catch((err) => { | |
console.log(err) | |
}) | |
return res.status(201).send({ number: viewCount }) | |
}) | |
module.exports = router |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment