Can i get some help here? What's wrong with this API call?

So I’m trying to collect the total amount of subs the logged ind user have.
But I’m getting these error’s and I’m not sure why.

[Twitch] Response Data: {error: "Unprocessable Entity", status: 422, message: "undefined does not have a subscription program"}
[Twitch] API Error: Unprocessable Entity; undefined does not have a subscription program

Here’s what I’m doing:

Javascript

var streamerName;
...
var subCountURL;

$(document).ready(function() { 

Twitch.init({
    clientId: Client_ID
}, function(error, status) {
    // the sdk is now loaded

        Twitch.api({
            method: 'channel'
        }, function(error, channel) {
            streamerName = channel.name;
            ...
            subCountURL = "channels/" + streamerName + "/subscriptions";

            updateUI();
            
        });

        Twitch.api({
            method: "channels/" + streamerName + "/subscriptions"   // CALLING THE SUBS HERE - THIS DOESNT WORK
        }, function(error, channels) {
            totalSubs = channels._total;

            updateUI();
        });

Is the logged in user a Twitch partner or affiliate? Because if not then they don’t have a subscription program just as the error message says.

I’m logging in and testing it, and im affiliated, so it should not come up with this problem.
Although, if i make the URL one string with my twitch username in it directly, then it works.
Like this:

"channels/" + streamerName + "/subscriptions" - DONT WORK
"channels/MY-TWITCH-USERNAME/subscriptions" - THIS WORKS, AND I GET THE INFO

That’s likely because streamerName has no value. You declare the variable but then use it in a function before you ever give it a value. You need to wait for your first api call to finish (which you then have assigning it a value) before calling the 2nd function.

1 Like

Twitch.api is asynchronous, the 2nd Twitch.api is called before the callback to your first one, which sets the value for streamerName

1 Like

Thanks for the help! Didn’t knew that. It’s working now :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.