How to Get a Medium User's ID

(Using the Medium API JS Package)

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.

Prerequisites

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.

Code Explanation

Let’s break the code into smaller sections for a better understanding.

1. Importing Required Modules

import Medium from 'medium-api-js';
import dotenv from 'dotenv';

dotenv.config();

Here:

2. Initializing the Medium API Client

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

3. Fetching the User ID

medium.getUserId('nishu-jain')
    .then(data => console.log('User:', data));

4. Example Output

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'.

Running the Code

To execute the code, follow these steps:

  1. Save the code in a file named getUserId.js.
  2. Ensure your .env file contains the correct RapidAPI key.
  3. Run the script using Node.js:
node getUserId.js

Potential Use Cases

Summary

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.

nishu@mediumapi.com