Medium has become a platform for millions of readers and writers, where personalization and relevant content play a pivotal role in keeping users engaged.
Suppose you want to analyze or tailor content based on user interests. In that case, you can fetch the tags they follow using their Medium user ID, enabling you to deliver curated experiences. Here's a step-by-step guide to achieve this using the "medium-api-js" package.
By the end of this guide, you'll know how to fetch the list of tags a Medium user is interested in and understand how to interpret the data.
Before diving into the code, ensure you have the following:
medium-api-js
package and dotenv
for environment variable management:npm install medium-api-js dotenv
.env
file in your project root directory and add your RapidAPI key:RAPIDAPI_KEY=your_api_key_here
The following code fetches the tags a user is interested in:
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
Medium
: This package provides access to Medium’s API.dotenv
: Used to load environment variables from a .env
file into process.env
.dotenv.config()
: Initializes the environment variable configuration.const medium = new Medium(process.env.RAPIDAPI_KEY);
Creates an instance of the Medium API client using your API key stored in the .env
file.
const userId = '1985b61817c3';
Replace '1985b61817c3'
with the actual user ID whose interests you want to retrieve.
medium.getUserInterests(userId)
.then(data => console.log('User Interests:', data));
getUserInterests(userId)
: Calls the method of the Medium API client with the user ID..then(data => { ... })
: Prints the response data to the console, including the list of tags and the total count.Save the code in a file named getUserInterests.js
and run the following command in your terminal:
node getUserInterests.js
The script outputs a JSON object to the console. Here’s a sample output:
id
: The user ID of the Medium user.tags_followed
: An array containing all the tags the user follows.count
: The total number of tags the user follows.Fetching user interests using their Medium user ID is straightforward with the medium-api-js
package. This process can unlock a plethora of opportunities, from improving user experiences to driving better engagement through tailored content and insights.
If you have any questions or need further assistance, feel free to reach out to nishu@mediumapi.com.
Happy coding!