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.
Before getting started, ensure you have the following:
dotenv
package.medium-api-js
and dotenv
packages by running the following command:npm install medium-api-js dotenv
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
Here, two essential modules are imported:
medium-api-js
: A wrapper for interacting with Medium's API.dotenv
: Used to load environment variables from a .env
file, keeping sensitive information secure.dotenv.config()
: Ensures the environment variables are available throughout the script.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.
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.
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.
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
Upon running the script, the console will display the following output (formatted for readability):
id
: Confirms the user ID for which the following list was fetched.following
: Contains an array of user IDs that the specified user is following.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!