How to Get Top Medium Writers for the given Tag

(Using Medium API Python Package)

In this guide, we'll walk you through how to use the Medium API to get a list of top writers for a specific topic. By the end, you'll be able to retrieve and display the top writers for a given tag on Medium using Python.

Prerequisite

Before you start, ensure you have the code medium-api library installed. You can install it using pip:

pip install medium-api

Code Breakdown

Import Libraries

First, we import the necessary libraries. code os is used to access environment variables, and code Medium is the class from the code medium-api library that allows us to interact with Medium's API.

Retrieve API Key

We retrieve the API key from the environment variables. Make sure to set this key in your environment for the code to work properly.

Create a Medium Object

Here, we create an instance of the code Medium class, passing the API key as an argument. This object will be used to interact with the Medium API.

Create a "TopWriters" Object

We use the code top_writers method of the code Medium object to fetch the top writers for the topic "artificial-intelligence". The code count parameter specifies how many top writers we want to retrieve.

Fetch and Print Top Writers

Finally, we call the code fetch_users method to get the list of top writers. We then loop through each writer and print their full name and username.

Output

Here is an example of the output you might see when running the script:

This code snippet demonstrates how to use the Medium API to get a list of top writers for a specified topic. By following these steps, you can adapt the code to fetch top writers for different topics by changing the code topic_slug parameter. Happy coding!