How to Get the Top 10 Articles from a Medium User

(Using Medium API Python Package)

In this guide, we'll walk through a Python script that retrieves the top 10 articles written by a user on Medium using the medium-api library. This is useful if you want to display or analyze the most popular content from a specific Medium user.

Prerequisites

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

pip install medium-api

Code Breakdown

Here's a step-by-step explanation of the Python code that achieves this task:

Import Libraries

The script begins by importing the necessary libraries. os is used to access environment variables, and Medium from medium_api is used to interact with the Medium API.

Get API Key from Environment Variables

The API key required to access the Medium API is retrieved from environment variables. Make sure to set this variable in your environment before running the script. If you don't have the key yet, you need to get it from Medium’s API documentation or management console.

Create a `Medium` Object

Here, a Medium object is created using the API key. This object allows you to interact with Medium's API.

Create a "User" Object

Replace "nishu-jain" with the username of the Medium user whose top articles you want to fetch. This line creates a User object for that specific Medium user.

Fetch and Print Top Articles

The fetch_top_articles() method retrieves the top 10 articles written by the user. The script then loops through these articles and prints out their titles.

Output

This script provides a straightforward way to access and display the top 10 articles from a Medium user's profile. Ensure that you have set up your environment with the necessary API key and replaced the username with the correct Medium handle. By following these steps, you can easily retrieve and display popular articles from Medium.