# How to use the API to get the top 20 streams

**URL:** <https://discuss.dev.twitch.com/t/how-to-use-the-api-to-get-the-top-20-streams/29422>\
**Category:** API\
**Created:** [December 9, 2020, 8:46am UTC](https://discuss.dev.twitch.com/t/how-to-use-the-api-to-get-the-top-20-streams/29422 "2020-12-09T08:46:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![oo92](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/oo92/32/11337_2.png) [@oo92](https://discuss.dev.twitch.com/u/oo92)\
**Post date:** [December 9, 2020, 8:46am UTC](https://discuss.dev.twitch.com/t/how-to-use-the-api-to-get-the-top-20-streams/29422/1 "2020-12-09T08:46:59Z")

</div>

Hi.

I am trying to write a Python script to get the top 20 streams using the API. However, I could not find a guide online. I am going off of the python-twitch-client [docs](https://python-twitch-client.readthedocs.io/en/latest/v5/streams.html) but so far I couldn’t find something helpful. I’ll admit, its my first time ever working with this API.

Precisely, this is what I want to accomplish: [https://dev.twitch.tv/docs/api/reference#get-streams](https://dev.twitch.tv/docs/api/reference#get-streams) I know that the default return is 20 streams.

At the moment, this is all the code I have:

```
from twitch import TwitchClient
client = TwitchClient(client_id='<my client id>')
```

---

<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:** [December 9, 2020, 9:40am UTC](https://discuss.dev.twitch.com/t/how-to-use-the-api-to-get-the-top-20-streams/29422/2 "2020-12-09T09:40:46Z")

</div>

Get an access token

> **[Getting OAuth Access Tokens](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/)**
>
> Getting OAuth Access Tokens

Then call

> **[Reference](https://dev.twitch.tv/docs/api/reference/)**
>
> Twitch Developer tools and services to integrate Twitch into your development or create interactive experience on twitch.tv.

Call

```
https://api.twitch.tv/helix/streams?first=20

```

Pure JS example, this fetches (and estimates the view count of) the “top games”, but you can easily swap the “flow” to get the top 20 streams instead

[https://barrycarlyon.github.io/twitch\_misc/examples/browse\_categories/](https://barrycarlyon.github.io/twitch_misc/examples/browse_categories/)

This uses implicit auth to get the needed token.

For what you describe the code would be running on a server (for caching purposes) and you’d probably use a `client_credentials` access token for server to server requests

> [@oo92](#):
>
> I am trying to write a Python script to get the top 20 streams using the API. However, I could not find a guide online. I am going off of the python-twitch-client [docs](https://python-twitch-client.readthedocs.io/en/latest/v5/streams.html) but so far I couldn’t find something helpful. I’ll admit, its my first time ever working with this API.
> 
> Precisely, this is what I want to accomplish: [Reference | Twitch Developers](https://dev.twitch.tv/docs/api/reference#get-streams) I know that the default return is 20 streams.

For this python lib under Kraken looks like you need

> > > from twitch import TwitchClient  
> > > client = TwitchClient(‘’)  
> > > streams = client.streams.get\_live\_streams()

Which would return 25 streams

But you really should be using Helix since kraken is deprecated

[https://python-twitch-client.readthedocs.io/en/latest/helix.html](https://python-twitch-client.readthedocs.io/en/latest/helix.html)

```
import twitch
client = twitch.TwitchHelix(client_id='<client_id>', client_secret='<client_secret>')
client.get_oauth()
client.get_streams()

```

My python is rusty.

---

<div class="post-metadata">

**Author:** ![oo92](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/oo92/32/11337_2.png) [@oo92](https://discuss.dev.twitch.com/u/oo92)\
**Post date:** [December 9, 2020, 11:19am UTC](https://discuss.dev.twitch.com/t/how-to-use-the-api-to-get-the-top-20-streams/29422/3 "2020-12-09T11:19:25Z")

</div>

How can I get an oauth? I couldn’t make sense of the documentation for that

---

<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:** [December 9, 2020, 11:21am UTC](https://discuss.dev.twitch.com/t/how-to-use-the-api-to-get-the-top-20-streams/29422/4 "2020-12-09T11:21:09Z")

</div>

The library you are using seems to do it for you

oAuth depends on the kind of token you want/need

Server to Server requests can use an App Access Token  
If you have a logged in user to your app you can use a user access token generated one of two ways  
User data may require a specific users user access token

So depends how you can authenticate.

> **[Getting OAuth Access Tokens](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/)**
>
> Getting OAuth Access Tokens

---

<div class="post-metadata">

**Author:** ![oo92](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/oo92/32/11337_2.png) [@oo92](https://discuss.dev.twitch.com/u/oo92)\
**Post date:** [December 9, 2020, 11:22am UTC](https://discuss.dev.twitch.com/t/how-to-use-the-api-to-get-the-top-20-streams/29422/5 "2020-12-09T11:22:37Z")

</div>

I don’t think any of that is necessary in my case. I just need to retrieve the top streams so I can use my script to manually take screens…

---

<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:** [December 9, 2020, 11:23am UTC](https://discuss.dev.twitch.com/t/how-to-use-the-api-to-get-the-top-20-streams/29422/6 "2020-12-09T11:23:19Z")

</div>

Then you probably need

> **[Getting OAuth Access Tokens](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/)**
>
> Getting OAuth Access Tokens

---

<div class="post-metadata">

**Author:** ![oo92](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/oo92/32/11337_2.png) [@oo92](https://discuss.dev.twitch.com/u/oo92)\
**Post date:** [December 9, 2020, 11:24am UTC](https://discuss.dev.twitch.com/t/how-to-use-the-api-to-get-the-top-20-streams/29422/7 "2020-12-09T11:24:32Z")

</div>

How do I include that in my code? This is what I have so far…

```auto
import twitch
client = twitch.TwitchHelix(client_id='...', oauth_token='<oauth_token>')
client.get_oauth()
client.get_streams()

```

Which is basically what you posted above…

---

<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:** [December 9, 2020, 11:25am UTC](https://discuss.dev.twitch.com/t/how-to-use-the-api-to-get-the-top-20-streams/29422/8 "2020-12-09T11:25:27Z")

</div>

I posted

```auto
import twitch
client = twitch.TwitchHelix(client_id='<client_id>', client_secret='<client_secret>')
client.get_oauth()
client.get_streams()

```

---

<div class="post-metadata">

**Author:** ![oo92](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/oo92/32/11337_2.png) [@oo92](https://discuss.dev.twitch.com/u/oo92)\
**Post date:** [December 9, 2020, 11:29am UTC](https://discuss.dev.twitch.com/t/how-to-use-the-api-to-get-the-top-20-streams/29422/9 "2020-12-09T11:29:03Z")

</div>

Sorry about my repeated dumb questions but neither code makes a difference. I am running the Python on an IDE but I don’t get an output.

---

<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:** [December 9, 2020, 1:00pm UTC](https://discuss.dev.twitch.com/t/how-to-use-the-api-to-get-the-top-20-streams/29422/10 "2020-12-09T13:00:51Z")

</div>

Then you might need help from the libraries creators rather then this forum.

As I’m not sure what the problem could be since I don’t tend to use any Twitch libs.

---

<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:** [January 8, 2021, 1:00pm UTC](https://discuss.dev.twitch.com/t/how-to-use-the-api-to-get-the-top-20-streams/29422/11 "2021-01-08T13:00:54Z")

</div>

Closed on January 8, 2021, 1:00pm UTC

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