node.jstwitchtwitch-api

twitch.js fetch error: ClientID is missing


I'm trying to use twitch API (helix) through Twitch.js. I can't seem to find a way for using the API. Tried it first with only the token but didn't work, tried with Twitch Applications' clientID and token but didn't work as well. I couldn't find anything about the parameters that the const api needs for it to work correctly other than this page (https://twitch-js.netlify.app/classes/api.html) which seems outdated cause i apparently need both the clientID and the oAuth This is my code:

const { Chat, ChatEvents, Api } = require("twitch-js");

const username = xxxxxx;
const token = xxxxxxxx;
const channel = xxxxxx;
const clientID = xxxxxx;

const run = async () => {
    const chat = new Chat({
      username,
      token
    });

    const api = new Api({
        clientID,
        token
    });
  
    await chat.connect();
    await chat.join(channel);
  
    //API test ↓

    api.get('bits/leaderboard', { search: { user_id: 'xxxxx' } }).then(response =>{
        console.log(response);
    })

run();

and this is my error:

[1631712611142] ERROR (TwitchJS/Api/10720 on PC): GET https://api.twitch.tv/helix/bits/leaderboard (334ms)
(node:10720) UnhandledPromiseRejectionWarning: FetchError: [TwitchJS] Client ID is missing
    at D:\Documents\Code\anti bot\node_modules\twitch-js\lib\utils\fetch\fetch.js:90:31
    at step (D:\Documents\Code\anti bot\node_modules\twitch-js\lib\utils\fetch\fetch.js:44:23)
    at Object.next (D:\Documents\Code\anti bot\node_modules\twitch-js\lib\utils\fetch\fetch.js:25:53)
    at fulfilled (D:\Documents\Code\anti bot\node_modules\twitch-js\lib\utils\fetch\fetch.js:16:58)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

Solution

  • Because you have a typo.

    clientID > clientId

    And you only need either the token or the clientID

    // With a token ...
    const token = 'cfabdegwdoklmawdzdo98xt2fo512y'
    const { api } = new TwitchJs({ token })
    
    // ... or with a client ID ...
    const clientId = 'uo6dggojyb8d6soh92zknwmi5ej1q2'
    const { api } = new TwitchJs({ clientId })
    

    Check the twitch-js github for more example