If you want to explore which Medium publications a specific user is following, this guide walks you through a straightforward method to fetch this data using the "medium-api-js" library.
By the end of this article, you will know how to set up, run, and understand a script that retrieves this information. This can be particularly useful for analyzing user interests or curating content recommendations.
Before diving into the code, ensure you have the following:
medium-api-js
package.First, install the required packages:
npm install medium-api-js dotenv
Then, create a .env
file in your project directory and add your RapidAPI key:
RAPIDAPI_KEY=your_rapidapi_key_here
Let’s break down the script into smaller sections for clarity.
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
Here:
Medium
is imported from the medium-api-js
package, which provides methods to interact with the Medium API.dotenv
is used to load environment variables (like the RapidAPI key) from a .env
file.dotenv.config()
initializes the environment variable configuration.const medium = new Medium(process.env.RAPIDAPI_KEY);
This line creates an instance of the Medium
class, passing in the RAPIDAPI_KEY
from the environment variables to authenticate API requests.
const userId = '1985b61817c3';
The userId
variable stores the Medium User ID for which we want to fetch publication following data. Replace '1985b61817c3'
with the actual User ID of the target user.
medium.getUserPublicationFollowing(userId)
.then(data => console.log('User Publication Following:', data));
Here:
getUserPublicationFollowing(userId)
is a method provided by the medium-api-js
package to fetch the list of publications that a user is following.Promise
that resolves to the data, which is then logged to the console.Save the script to a file named getUserPublicationFollowing.js
. Open your terminal and run the script:
node getUserPublicationFollowing.js
When the script runs successfully, it will fetch and display the publications the user is following. For example:
This output includes:
Fetching the list of publications a Medium user follows is simple and insightful with the medium-api-js
library. This functionality can help developers and marketers alike to better understand user preferences and create engaging content strategies.
If you have any questions or need assistance with the Medium API, feel free to reach out to me at nishu@mediumapi.com.