Search code examples
pythonjsonapitwitch

How to use the API to get the top 20 streams


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 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 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>')

Solution

  • Welcome to working with APIs - it is definitely a blast.

    In this scenario, assuming you've successfully created your application and retrieved your client id as outlined in the instructions, to get the latest streams you would use get_live_streams

    from twitch import TwitchClient
    
    client = TwitchClient(client_id='<client-id>')
    streams = client.streams.get_live_streams()
    
    print(streams)
    

    The API provides other functions you can use to retrieve other data such as get_featured, get_stream_by_user, and more. You can view all those functions in the documentation.