(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.
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.
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.
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'
Next, we initialize a Medium
object by passing the API key we just retrieved. This object will help us interact with Medium's API.
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.
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!