Curl Command in Javascript

Hey,

I try to get some Informations about a Stream and with Curl it works:

"curl -X GET ‘https://api.twitch.tv/helix/streams?user_login={streamer}
-H ‘Authorization: Bearer {token}’
-H ‘Client-Id: {ID}’ "

But I need that in Javascript, but I don’t know how to do it there.
So please tell me how to use the API in Javascript.

It’s just a HTTP request, so you can use any HTTP library in JS. Such as fetch if you’re doing this in the browser, or if you’re doing it in NodeJS you could use got, or any of the other dozens of HTTP request libraries.

Hey Thanks for your reply

Is it possible too use the API in the Browser? But how do I authenticate then?
Could you maybe give me an example?
I tryed a lot already but nothing worked for me

You either use the oAuth flow to get the users access token. Either via Implicit or regular oAuth

Or you make your front end call your backend and then the backend uses an App Access Token to call Twitch (for public data).

This is an example of using pure Client Side JavaScript to authenticate and call the API

Demo: https://barrycarlyon.github.io/twitch_misc/authentication/implicit_auth/
Code: https://github.com/BarryCarlyon/twitch_misc/tree/master/authentication/implicit_auth

Sorry i dont understand.

i just want data from a stream via the API in the browser. I already have my access token

Then in order to not leak your access token to the world. (An access token is basically a password)

Your front end will need to call your backend and the backend makes the request to Twitch API

And how will the backend make the request? With Curl?

@Dist said " It’s just a HTTP request, so you can use any HTTP library in JS." So its possible with JS? But how? How can I authenticate with JS?

Depends what language your backend is, but essentially yes, most languages just wrap cURL

You on about how to construct a API request to provide your access token or how to get an oAuth token to use?

I already linked you a fetch example

Here is a nodeJS “regular oAuth” example https://github.com/BarryCarlyon/twitch_misc/tree/master/authentication/user_access_generator/nodejs

Which covers how to get a token and then use that token, (line 147 in server.js is the got example on getting the authenticated users profile)