javascriptnode.jsapitwitchtwitch-api

Twitch API returning incorrect channel


I'm currently working with the Twitch API and for some reason making a GET request to /api.twitch.tv/helix/search/channels?query=[STREAMER_NAME] seems to be returning the incorrect streamer/user. Here is an example:

/api.twitch.tv/helix/search/channels?query=loltyler1 returns the incorrect user since the broadcaster_login is wrong. "broadcaster_login": "loltyler1dotcomdiscoalpha"

This doesn't seem to just happen with this specific account. Another example was when imkaicenat was used as the query and it returned a user called imkaidenbtw. Here is an example from a function making a GET request to their api from my own code, hopefully this isn't a problem on Twitch's end and there's something here that can be fixed:

client.on("message", (msg) => {
  // split message into array of words
  const args = msg.content.split(" ");

  // If bot sends message end functions
  if (msg.author.bot) return;

  // Get info
  if (msg.content.startsWith(`${prefix}live`)) {
    // Make get request to Twitch API with streamer name
    axios
      .get(`https://api.twitch.tv/helix/search/channels?query=${args[1]}`, {
        headers: {
          "client-id": process.env.TWITCH_CLIENT_ID,
          Authorization: `Bearer ${process.env.TWITCH_ACCESS_TOKEN}`,
        },
      })
      .then((res) => {
        let response = res.data.data;
        response[0].is_live
          ? msg.reply(
              `${response[0].broadcaster_login} is live playing ${response[0].game_name}. Watch here: https://twitch.tv/${response[0].broadcaster_login}`
            )
          : msg.reply(`${response[0].broadcaster_login} is not live`);
      })
      .catch((err) => console.log(err));
  }

Not sure if it helps, but this is part of a discord bot project. Thanks for any help!


Solution

  • A few months ago I also had to deal with Twitch's API I had the same issue. If you log your res variable you should notice, that it returned a lot of streamers with similar names

    In order to get the desired one, you have to filter the data you are receiving. Also you have to use res.json() first:

    const streamer = 'name of the streamer'.toLowerCase();
    
    [...]
    
    .then(res => res.json())
    .then(res = {
       if(!res.data) return console.log('Streamer not found!');
       
       const broadcaster = res.data.filter(filter => filter.display_name.toLowerCase() === streamer)[0];
    })
    

    Note:

    If you want, you can take a look at my twitch live command but I'll point out that I'll have to recreate this one since it's not quite robust. But for your problem you can orient yourself well on it