On Medium you can find content on various topics, and can get curated recommendations based on specific tags that can help users discover articles tailored to their interests.
This article explains how to fetch recommended lists for a specific tag using the Medium API and the "medium-api-js" package. By the end, you’ll know how to implement this process, understand the output, and explore its potential applications.
Before diving into the code, ensure you have the following:
npm install medium-api-js
.npm install dotenv
to manage environment variables.Here’s how you can fetch recommended lists for a specific tag on Medium. Let’s break down the code into digestible parts:
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
medium-api-js
: This package simplifies interacting with the Medium API.dotenv
: Used to load environment variables from a .env
file, ensuring sensitive information like the API key is kept secure.dotenv.config()
: Loads the variables defined in the .env
file.const medium = new Medium(process.env.RAPIDAPI_KEY);
process.env.RAPIDAPI_KEY
: Retrieves the API key from the .env
file.new Medium()
: Creates an instance of the Medium client using the provided API key.const tag = 'technology';
'technology'
is specified to fetch recommended lists for content related to this topic. You can change this to any tag of your choice, such as 'science'
or 'design'
.medium.getRecommendedLists(tag)
.then(data => {
console.log('Recommended Lists:', data);
});
medium.getRecommendedLists(tag)
: Calls the Medium API to get recommended lists for the specified tag..then(data => {...})
: Handles the resolved promise and logs the returned data to the console.Save the script in a file named getRecommendedLists.js
. To run it, execute the following command in your terminal:
node getRecommendedLists.js
When you run the code, the output will look like this:
recommended_lists
: An array of unique IDs representing the recommended lists for the specified tag.tag
: The tag used in the query.count
: The total number of recommended lists fetched.You learned how to fetch recommended lists from Medium, understand their output, and explored various use cases for leveraging this feature. With these skills, you can confidently build tools or applications that make content discovery more engaging and tailored.
For questions or further assistance, contact nishu@mediumapi.com.
Best of luck with your projects!