How to Get a Medium User's Following

Using JavaScript and Medium API

Knowing which users someone follows on Medium can provide valuable insights, whether you're studying social media trends, analyzing user behavior, or building tools to enhance engagement on the platform.

By leveraging the Medium API, you can fetch the list of users that a specific user is following based on their user ID.

This guide explains how to accomplish this using a concise piece of JavaScript code. The process is broken into clear steps, making it easy to follow and implement.

Prerequisites

Before getting started, ensure you have the following:

Code Explanation

Step 1: Import Required Modules

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

dotenv.config();

Here, two essential modules are imported:

Step 2: Initialize the Medium API Client

const medium = new Medium(process.env.RAPIDAPI_KEY);

An instance of the Medium class is created using your API key, retrieved from an environment variable process.env.RAPIDAPI_KEY. This key authenticates your requests to Medium's API.

Step 3: Specify the User ID

const userId = '14d5c41e0264';

The userId variable contains the ID of the Medium user whose following list you want to retrieve. Replace this ID with the actual user ID of interest.

Step 4: Fetch the Following List

medium.getUserFollowing(userId)
    .then(data => console.log('User Following:', data));

The getUserFollowing method fetches the list of users that the specified user follows. The result is logged to the console in JSON format for easy readability. The method returns a promise, and the then block handles the asynchronous response.

Running the Code

Save the code in a file named getUserFollowing.js. Open your terminal, navigate to the file’s directory, and execute the script with the following command:

node getUserFollowing.js

Output and Explanation

Upon running the script, the console will display the following output (formatted for readability):

Potential Use Cases

Summary

Fetching a Medium user’s following list is straightforward with the Medium API and JavaScript. This guide walked you through setting up the environment, writing the code, and interpreting the output. Such insights can enhance your understanding of user behavior and facilitate building innovative tools.

If you have questions or need further assistance with the Medium API, feel free to reach out to nishu@mediumapi.com.

Happy coding!

nishu@mediumapi.com