How to get username from twitch API

So I’m fairly new to working with tmi.js and am also fairly new at working with API’s, so I thought this would be a good way to learn. What I’m trying to do is create a function that puts in the chat when someone follows. I have managed to grab the newest follower from the API with this link. https://api.twitch.tv/kraken/channels/REDACTED/follows?api_version=5&client_id=redacted&limit=1, and I confirmed it’s correct because when I put it into chrome it comes back with

{
"_total": 19,
"_cursor": "eyJ0cCI6InVzZXI6MTkwNTkyOTAyOmZvbGxvd3MiLCJ0cyI6InVzZXI6MTI5ODUzODExIiwiaXAiOiJ1c2VyOjEyOTg1MzgxMTpmb2xsb3dlZF9ieSIsImlzIjoiMTYwNjg3Njc5ODA2OTM4NDkzNyJ9",
"follows": [
    {
        "created_at": "2020-12-02T02:39:58Z",
        "notifications": false,
        "user": {
            "display_name": REDACTED,
            "_id": "REDACTED",
            "name": REDACTED,
            "type": "user",
            "bio": "❥ am sleepy *yawn*",
            "created_at": "2018-01-13T22:24:37.745199Z",
            "updated_at": "2020-12-06T02:15:17.581551Z",
            "logo": REDACTED
        }
    }
]
}

However interestingly enough when I log it in the console it comes back very very very different. Nevertheless what I am trying to accomplish is I need to grab the name object, but for the life of me I can’t figure it out, hopefully you guys can lend me a hand, also before I forget this is what my current code looks like

function follow(){

https.get(“https://api.twitch.tv/kraken/channels/REDACTED/follows?api_version=5&client_id=REDACTED&limit=1”, (res) => {

var display_name = res.display_name;


console.log(display_name)

})

}
follow();

The reason the response in the console differs from just going to the URL, is that when you go to a URL the body of the response is rendered on the page, where as when you actually receive a request there’s much more data than just the body, and in this case res is the whole response object, so what you’re likely after is res.body, but that may vary depending on which HTTP request library you’re using.

A few things worth noting though, first, v5 is a deprecated API, so don’t rely on that URL being around forever as it will be removed in the near future.

Secondly, the API uses caching, so you shouldn’t poll the same endpoint more than once a minute, which means there’s a potential for multiple new follows in that space of time but if you’re only getting the newest one at the time of your request all of the others may be missed. There is also potential for abuse that you should be aware of, as without any sort of caching to check if a user was a prior follower there would be nothing stopping them unfollowing and refollowing all the time to make your bot spam.

And finally, many users wont like being shouted out in chat when they follow a streamer, There is a significant portion of the Twitch community that just like to lurk quietly in chat and wont appreciate having attention forced on them. It’s still not a bad way to learn how to program and make use of the APIs and such, I just wouldn’t recommend actually using it on a channel though.

When I log res.body it just comes back undefined. Do you have any docs or know off the top of your head other ones to try to see if I can get the block of text I’m looking for?

You’ll need to Google whichever HTTP request library you’re using and read the documentation for that as I use Got server-side, and fetch client-side, so I’m not familiar with other request libraries.

Doesn’t Betapig want to use that same query they used but just drill down to in the res object?

Something like

var display_name = res.follows[0].user.display_name

I could be wrong but I don’t think so.

That is why we are suggested they refer to the documentation for the HTTP library they are using for the correct way to interrogate the response body.

Can we not just tell from the structure of the data they gave us how to access the display_name in the res object? I’m also new but I hope I helped by trying to explain the drilling down in the object.

The user has not shown what res contains

The showed what they got from chrome, not whatever https.get is returning.

And their presented code doesn’t declare what https is

I just figured they just saw the display_name in the link they got the object back in originally, then tried to use that name to assign to that variable without accessing it deeper in the object they got from the https.get call.

Their issue is that they don’t know how to get the display_name. Their first example shows them accessing the URL in a browser, but that’s just the response body that the browser renders and only helps them know where the display_name is within the structure of the response body, it doesn’t in any way help them figure out where that body is within the response object.

Depending on the HTTP library used, the response object may just be the body (OP said the response differs from what they see in a browser, so that’s unlikely here), it may be nested in a res.body (which is coming back as undefined so in this case it’s not that), some libraries may return a Buffer that needs to be joined/parsed first to give a body, etc…

We can’t help them because we don’t know the structure of the response they are getting from their HTTP library as we don’t know what their HTTP library of choice is. We can tell them where in the body the display_name, but if they don’t know how to get the body that wont help them.

Thanks that cleared that up for me. If they posted what they got in the console that would be more telling of how to access the data they are after then yes?

Yes, knowing what their response object was would help, although depending on the library this could be a lot of data (and potentially expose private data). It would be simpler to just to know what their HTTP library is so that they can be directed to the docs for it.

Got it.

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