Medium provides a treasure trove of articles and user-generated content. Accessing specific user data, such as their unique ID, can be incredibly useful for developers building applications or tools that integrate with Medium.
This guide demonstrates how to fetch a Medium user’s ID using the medium-api-js
package. By the end, you’ll be able to retrieve the user ID for any Medium username and understand how this information can be used in practical applications.
Before diving into the implementation, ensure you have the following:
To install medium-api-js
, run the following command in your terminal:
npm install medium-api-js dotenv
The dotenv
package is also required to manage your API key securely.
Let’s break the code into smaller sections for a better understanding.
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
Here:
medium-api-js
package to interact with Medium's API..env
file.const medium = new Medium(process.env.RAPIDAPI_KEY);
The Medium
class is instantiated with the RapidAPI key, which is fetched securely from the environment variable RAPIDAPI_KEY
. Ensure your .env
file contains the key:
RAPIDAPI_KEY=your_rapidapi_key_here
medium.getUserId('nishu-jain')
.then(data => console.log('User:', data));
'nishu-jain'
) and sends a request to Medium's API.When the above code runs successfully, the output in the terminal will look like this:
This output shows the unique ID '1985b61817c3'
associated with the user 'nishu-jain'
.
To execute the code, follow these steps:
getUserId.js
..env
file contains the correct RapidAPI key.node getUserId.js
In this article, you learned how to retrieve a Medium user's ID using the medium-api-js
package. This unique ID serves as a key for accessing additional user-specific data through the Medium API.
Whether you’re building an analytics tool, integrating Medium data into your app, or exploring user-generated content, this guide provides a solid foundation.
For questions or further assistance with the Medium API, contact nishu@mediumapi.com.