Skip to content

Instantly share code, notes, and snippets.

@toshvelaga
Created March 3, 2022 09:54
Show Gist options
  • Save toshvelaga/1b094428a7a6a0c968ffe722886a681d to your computer and use it in GitHub Desktop.
Save toshvelaga/1b094428a7a6a0c968ffe722886a681d to your computer and use it in GitHub Desktop.
server side node function to get view count from twitch
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