# Connecting to the API and getting data from Channel Points Rewards

**URL:** <https://discuss.dev.twitch.com/t/connecting-to-the-api-and-getting-data-from-channel-points-rewards/30288>\
**Category:** API\
**Created:** [February 2, 2021, 10:50pm UTC](https://discuss.dev.twitch.com/t/connecting-to-the-api-and-getting-data-from-channel-points-rewards/30288 "2021-02-02T22:50:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Black\_Werwulf](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@Black\_Werwulf](https://discuss.dev.twitch.com/u/Black_Werwulf)\
**Post date:** [February 2, 2021, 10:50pm UTC](https://discuss.dev.twitch.com/t/connecting-to-the-api-and-getting-data-from-channel-points-rewards/30288/1 "2021-02-02T22:50:09Z")

</div>

Hey 🙂

When a viewer is redeeming a custom channel point reward, I would like to get the information of “Who redeemed which CP Reward?”. It seems to be that I get this info from the [Channel Points Custom Reward Redemption Add Notification](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelchannel_points_custom_reward_redemptionadd). But I am already having troubles with getting connected to the twitch API. I am working with nodeJS and for example when I try to connect with the streamlabs API, all I need to do is:

~ register app on streamlabs  
~ take the Socket API Token and create a windows environment variable  
~ install socket.io-client  
~ and then just write this 3 lines:

```
const io = require('socket.io-client');
const streamlabs = io(`https://sockets.streamlabs.com?token=${process.env.STREAMLABS_SOCKET_API}`, {transports: ['websocket']});
streamlabs.on('connect', () => console.log('Connected with Streamlabs.'));

```

After those steps, I can get information from things like Test Subs. But this easy way is not mentioned in the streamlabs dev docs. And looking into the twitch dev docs makes it again more complicated than easy (I could rant SO MUCH about how user/beginner unfriendly these docs are written, but that would be too much text and not help here 😆).  
So I have this feeling there MUST be also an almost equal simple way to connect to the twitch API like with the streamlabs API. But how?  
Steps I have already taken is to register an app on twitch, so I already have a Client ID and a Client Secret. And as mentioned above, nodeJS and socket.io-client are installed.

What are the next steps? 🙂

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [February 2, 2021, 10:55pm UTC](https://discuss.dev.twitch.com/t/connecting-to-the-api-and-getting-data-from-channel-points-rewards/30288/2 "2021-02-02T22:55:12Z")

</div>

> [@Black\_Werwulf](#):
>
> It seems to be that I get this info from the [Channel Points Custom Reward Redemption Add Notification](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#channelchannel_points_custom_reward_redemptionadd).

EventSub is (currently) a webhook _only_ mechanism

You need a server (such as running express for example, since we are talking node).  
That is capable of recieving HTTP post requests

A regular eventsub example

[https://github.com/BarryCarlyon/twitch\_misc/tree/master/eventsub/handlers/nodejs](https://github.com/BarryCarlyon/twitch_misc/tree/master/eventsub/handlers/nodejs)

A more channel points specific event sub example

> <https://github.com/BarryCarlyon/twitch_misc/tree/main/channel_points>
>
> //github.com/BarryCarlyon/twitch\_misc/tree/main/channel\_points

For a socket solution, you want to be talking to the channel-points topic on pubsub

> **[PubSub](https://dev.twitch.tv/docs/pubsub/)**
>
> PubSub Guide

> [@Black\_Werwulf](#):
>
> And looking into the twitch dev docs makes it again more complicated than easy (I could rant SO MUCH about how user/beginner unfriendly these docs are written, but that would be too much text and not help here 😆).

I have no issue, the docs list the endpoints and the response to those endpoints.

Everything else I throw tests/requests at and see what comes back.

What specifically is complicated in the docs? Feedback is always being gathered to improve the documentation.

---

<div class="post-metadata">

**Author:** ![Black\_Werwulf](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@Black\_Werwulf](https://discuss.dev.twitch.com/u/Black_Werwulf)\
**Post date:** [February 3, 2021, 12:51am UTC](https://discuss.dev.twitch.com/t/connecting-to-the-api-and-getting-data-from-channel-points-rewards/30288/3 "2021-02-03T00:51:11Z")

</div>

> You need a server (such as running express for example, since we are talking node).  
> That is capable of recieving HTTP post requests

Ok, express is installed now.

> A more channel points specific event sub example

So this is what I have to use to test the finished code for channel points rewards?

> What specifically is complicated in the docs?

Well the first thing is, that the code examples are just written in one langage, which seems to be called cURL. As soon as a reader doesn’t know this language (like me), it gets confusing. Streamlabs has a refernce website with [5 different languages](https://dev.streamlabs.com/reference). That does not mean, that a js-idiot like me immediatly understands it, but atleast the chances are higher that I can find it out by looking at my language and also the others. E.g. when I looked into the twitch dev docs for my problem, first I looked into [Twitch API](https://dev.twitch.tv/docs/api). But the first code that I see there is this:

```
curl --location --request
GET 'https://api.twitch.tv/helix/search/channels?query=a_seagull' \
--header 'client-id: wbmytr93xzw8zbg0p1izqyzzc5mbiz' \
--header 'Authorization: Bearer 2gbdx6oar67tqtcmt49t3wpcgycthx'

```

That’s not helpful.

The second thing is, that the docs don’t give enough information. So for example: As mentioned at the start post, I would like to connect to the twitch API and get the CP reward infos. You are so nice and help me by saying _“Better you look into PubSub”_. So I look into PubSub and the info it gives me is **“A PubSub is [explaination].”** and **“The content of a PubSub is [list of content]”**. But there is no **“And THIS is how you get the content.”**. All I see is **“Something, something `wss://pubsub-edge.twitch.tv`, ping, pong, listen, reconnect”** and **“Not that important, but good to know”** -info like the API Limits. And this way of giving information basicly goes through the whole dev docs. There are no examples of **“Here is the summarizing example code for this topic. In chase you want to copy-paste it, there are [EXAMPLE] fields. E.g. one info you need is your Client ID. So look for [EXAMPLE Client ID], delete it including the [], and write you Client ID.”**

So all in all the dev docs are written in a way, that feels like you have to be the programmer who programmed all of twitch and with that understand 100% of EVERY single word in it. I mean, of course this site is made for people who know what it is about. And I am just a stupid beginner. But how much it is just assuming that you should know and also remember is really user unfriendly. Just look at my short explaination of how I connect to streamlabs: 4 steps and just 3 lines of code. That’s all. Why is it so hard to put such examples into the twitch dev docs? The dev docs just makes me feel dumb by reading it, annoyed by being forced to ask for help again, and angry that I have to waste somebody else time by answering my stupid question.

Ok, enough of this little rant. Sorry for the text block, it’s just all so exhausting with how hard it is, especially when it could be way easier.  
It’s late here now and I go to bed. See you tomorrow. Good night and thanks for helping me (or more like “trying to help” because I am an idiot).

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [February 3, 2021, 1:09am UTC](https://discuss.dev.twitch.com/t/connecting-to-the-api-and-getting-data-from-channel-points-rewards/30288/4 "2021-02-03T01:09:39Z")

</div>

> [@Black\_Werwulf](#):
>
> E.g. when I looked into the twitch dev docs for my problem, first I looked into [Twitch API](https://dev.twitch.tv/docs/api). But the first code that I see there is this:
> 
> ```auto
> curl --location --request
> GET 'https://api.twitch.tv/helix/search/channels?query=a_seagull' \
> --header 'client-id: wbmytr93xzw8zbg0p1izqyzzc5mbiz' \
> --header 'Authorization: Bearer 2gbdx6oar67tqtcmt49t3wpcgycthx'
> 
> ```

This tells you the

- HTTP Method, it’s GET
- the URL to call `https://api.twitch.tv/helix/search/channels?query=a_seagull`
- you need two headers, a client-ID header and and Authorization

(`--location --request` and superflous, and some of the newer docs, the doc writer has been using the “longer” form of the cURL flags/arguments)

This is then translatable into any langauge, Streamlabs does 5, well what about the others, such as PHP or brainfuck or lua for example?

> [@Black\_Werwulf](#):
>
> That does not mean, that a js-idiot like me immediatly understands it, but atleast the chances are higher that I can find it out by looking at my language and also the others

Aside from being opinated on the languages, SL also is opnatiated on the libraries to use, like nodeJS they suggest you use `node-fetch`

Which means you learn to use `node-fetch` instead. As then we’ll get questions like “I’m trying to do this in axios/got/other module/no module at all”. So cURL is given as a cURL call can be translated to match whatever language you are working in and whatever module/library you are working with.

Which is why cURL is the first example in the Streamlabs docs, as cURL is universal

> [@Black\_Werwulf](#):
>
> And this way of giving information basicly goes through the whole dev docs. There are no examples of **“Here is the summarizing example code for this topic. In chase you want to copy-paste it, there are [EXAMPLE] fields. E.g. one info you need is your Client ID. So look for [EXAMPLE Client ID], delete it including the [], and write you Client ID.”**

Given that all of Twitch’s offerings (except kraken) requires an oAuth token to do anything, you can’t just copy paste without getting a token first, so you can’t really do “simple” copy/pasta examples

> [@Black\_Werwulf](#):
>
> Well the first thing is, that the code examples are just written in one langage, which seems to be called cURL. As soon as a reader doesn’t know this language (like me), it gets confusing.

cURL is “standard” and language agnostic.

Most lanaguaages _generally_ just implement cURL under the hood and provide helper libraries to convert to cURL. So the Twitch docs are unopinated on langauge.

> [@Black\_Werwulf](#):
>
> Just look at my short explaination of how I connect to streamlabs: 4 steps and just 3 lines of code.

Not really thats, “heres how to use nodeJS library of socket IO to connect” it’s an “opinated” piece of documetnation that says “you should be using nodeJS”.

It’s four steps of “heres [socket.io](http://socket.io) just use that”, (granted they are using a [socket.io](http://socket.io) server). So the ways to connect to [socket.io](http://socket.io) are limited, to the langauges that [socket.io](http://socket.io) has been ported to. Twitch uses websockets (which [socket.io](http://socket.io) uses underneath), and websockets can be made to work much more easily in any langauge, and thus the docs don’t give an opinion on language or modules to use.

Twitch’s docs tell you the endpoints you can call and the responses to those endpoints, so it doesn’t say `use socket.io` as thats an opinion on what you should use, as apposed to “heres the calls, use what you are used to using”

> [@Black\_Werwulf](#):
>
> angry that I have to waste somebody else time by answering my stupid question.

The only stupid question is the one you don’t ask.

> [@Black\_Werwulf](#):
>
> Well the first thing is, that the code examples are just written in one langage, which seems to be called cURL.

Of course this is all irrelevant if instead of reading the docs, you use someone elses Twitch library (for nodeJS if you stay in node), as then you follow their docs for their library instead.

> [@Black\_Werwulf](#):
>
> Ok, express is installed now.

Feel free to use any module that can recieve HTTP Posts requests, I use express myself, so use what you are comfortable with, and you don’t have to use NodeJS.

> [@Black\_Werwulf](#):
>
> So this is what I have to use to test the finished code for channel points rewards?

For channel points first you need an access token (with the relevant scopes attached) to a channel that has channel points enabled, which is affiliate+

---

<div class="post-metadata">

**Author:** ![Black\_Werwulf](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@Black\_Werwulf](https://discuss.dev.twitch.com/u/Black_Werwulf)\
**Post date:** [February 3, 2021, 5:39pm UTC](https://discuss.dev.twitch.com/t/connecting-to-the-api-and-getting-data-from-channel-points-rewards/30288/5 "2021-02-03T17:39:41Z")

</div>

> Feel free to use any module that can recieve HTTP Posts requests, I use express myself, so use what you are comfortable with, and you don’t have to use NodeJS.

I found a solution that uses the https module, because it is already in node.

> For channel points first you need an access token (with the relevant scopes attached) to a channel that has channel points enabled, which is affiliate+

The mentioned solution looks like this…

> ****
>
> ```
> const https = require('https');
> const options = {
> host: 'id.twitch.tv',
> path: '/oauth2/token?client_id=[myClientID]&client_secret=[myClientSecret]&grant_type=client_credentials&scope=channel:read:redemptions',
> method: 'POST',
> };
> 
> callback = function(response) {
> let str = '';
> response.on('data', function (chunk) {
> str += chunk;
> });
> response.on('end', function () {
> console.log(str);
> });
> };
> https.request(options, callback).end();
> 
> ```

…which gives me this…

```
{"access_token":"[myAccessToken]","expires_in":5301018,"scope":["channel:read:redemptions"],"token_type":"bearer"}

```

So I have an access token now, but:  
1.1 My channel has NOT channel points enabled. Can I still make tests?  
1.2 If yes, I should use your [twitch misc aboutchannel points](https://github.com/BarryCarlyon/twitch_misc/tree/master/channel_points) then?  
2.1 The PubSub says, I need a constant connection to the topic with **`LISTEN`** , **`PING`** and **`wss://pubsub-edge.twitch.tv`**. Do I have to do that with [this](https://www.w3schools.com/nodejs/met_server_listen.asp)?  
2.2 If yes, how would it look like combined with twitch? Something like this?

> ****
>
> ```
> const https = require('https');
> http.createServer(function (req, res) {
> res.writeHead(200, {'Content-Type': 'text/plain'});
> res.write('Connected with topic.');
> res.end();
> }).listen('wss://pubsub-edge.twitch.tv');
> 
> ```

2.3 If no, what would be the right code?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [February 3, 2021, 5:45pm UTC](https://discuss.dev.twitch.com/t/connecting-to-the-api-and-getting-data-from-channel-points-rewards/30288/6 "2021-02-03T17:45:27Z")

</div>

> [@Black\_Werwulf](#):
>
> 1.1 My channel has NOT channel points enabled. Can I still make tests?

No, as the API will reject reward creation

> [@Black\_Werwulf](#):
>
> 1.2 If yes, I should use your [twitch misc aboutchannel points](https://github.com/BarryCarlyon/twitch_misc/tree/master/channel_points) then?

That is a rough clone of some production code I use for 2/3 channels (2 live one in dev)

> [@Black\_Werwulf](#):
>
> 2.1 The PubSub says, I need a constant connection to the topic with **`LISTEN`** , **`PING`** and **`wss://pubsub-edge.twitch.tv`**. Do I have to do that with [this](https://www.w3schools.com/nodejs/met_server_listen.asp)?

No, the example you link to is for recieving HTTP, not for connecting to PubSub.

I have a PubSub example here [https://github.com/BarryCarlyon/twitch\_misc/tree/master/pubsub](https://github.com/BarryCarlyon/twitch_misc/tree/master/pubsub)

> [@Black\_Werwulf](#):
>
> 2.2 If yes, how would it look like combined with twitch? Something like this?

No this shows a misunderstanding of what PubSub is. Pubsub is a socket connection where you conenct to a server maintain. Not a server where someone else send you data

> [@Black\_Werwulf](#):
>
> 2.3 If no, what would be the right code?

It is not dissimilar from the Streamlabs `socket.io` example, but you have to create the connection managment logic/maintaince. See the pubsub nodejs example I linked.

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [March 5, 2021, 5:45pm UTC](https://discuss.dev.twitch.com/t/connecting-to-the-api-and-getting-data-from-channel-points-rewards/30288/7 "2021-03-05T17:45:31Z")

</div>

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