Skip to content

Migrating from Kraken to Helix Twitch API

Posted on:March 10, 2021 at 07:53 PM

Few months back I’ve seen that my website Splitstream does not work. The issue seems to be that twitch changed their API policy. They switched from the Kraken to Helix API.

The official documentation of Twitch suggests this way of handling authentication with the [Helix API](Authentication | Twitch Developers)

The old way with using Kraken is doing a API request with just the Client-ID .

Example:

curl -H 'Accept: application/vnd.twitchtv.v5+json' \
-H 'Client-ID: XXXXXXXX' \
-X GET 'https://api.twitch.tv/kraken/games/top'

However, with the new Helix API, the request looks like this:

curl -X GET 'https://api.twitch.tv/helix/games/top' \
-H 'Authorization: Bearer XXXXXXXX' \
-H 'Client-Id: XXXXXXX'

As we can see there is now a Authorization header which requires us to send a Bearer token, there are 3 ways of getting a token. ([Reference](Getting Tokens: OAuth | Twitch Developers))

In the end I’ve created a small node server with express, and just added one endpoint which fetches the token. It looks like this:


app.get('/getToken', async function (req, res, next){
    const dataToReturn = await getCredentials()
    var dataReturner = dataToReturn.data
    // Redis to save the token
    client.set("token", dataReturner.access_token)
})


async function getCredentials() {
    var secret = SECRET_ID
    var clientid  = CLIENT_ID
    const url = 'https://id.twitch.tv/oauth2/token?client_id=' + clientid + '&client_secret=' + secret + '&grant_type=client_credentials'
    const responseToken = await axios.post(url)
    return responseToken
}

app.get('/getTopgames', async function(req, res, next) {

    let topGames = await axios.get('https://api.twitch.tv/helix/games/top?first=5', {
      headers: {
        'Client-ID': CLIENT_ID,
        'Authorization':'Bearer ' + client.get("token")
      }
    })

    res.json({topGames})

)}

I’ve also migrated all my endpoints to this nodejs Server where my frontend calls my node server which then calls the Twitch API. Think of it like a small wrapper around the twitch API. So now we have a small nodeJs server with the Twitch API calls and it is totally isolated from the frontend which only gets returned the data.

After you set the token it should be fairly easy to use it in a endpoint.

Note: Securet the endpoint if you are using it on a external server. My go to would be just using CORS for this.