How to Get a Medium User's ID

(Using Medium API Python Package)

In this guide, we'll walk through the steps to retrieve a Medium user's ID using the Medium API in Python. With just a few lines of code, you can obtain the user ID by referencing their username. Let's break down the process.

Prerequisite

Before you start, make sure you have the medium-api Python package installed. You can install it via pip:

code pip install medium-api

Now, let's move forward and break down the code.

Step 1: Import Required Libraries

We start by importing the necessary libraries. Here, we'll import the os module to access environment variables and the Medium class from the medium_api package.

Step 2: Get Your Medium API Key

In this part of the code, we retrieve the RAPIDAPI_KEY from your environment variables. Make sure your API key is stored in the environment variable RAPIDAPI_KEY.

You can set this environment variable in your terminal or development environment using:

code export RAPIDAPI_KEY='your_medium_api_key'

Step 3: Create a Medium Object

Next, we initialize a Medium object by passing the API key we just retrieved. This object will help us interact with Medium's API.

Step 4: Get the User Object and User ID

Finally, we use the medium.user() method to fetch the user object by specifying the Medium username. In this example, the username is "nishu-jain". Once we have the user object, we can print the user_id associated with the username.


Output

By executing this script, you will see the user ID printed in your console for the given username. You've successfully retrieved a user's ID on Medium using the Medium API in Python!