Medium offers a vast repository of articles across diverse topics, making it a go-to platform for readers and writers alike. Suppose you're interested in fetching recommended articles for a specific tag and page using Medium’s API. This article will guide you through a simple script to achieve that.
In this example, you’ll learn how to retrieve the recommended feed of articles tagged under a certain tag. You’ll also understand how the code works step-by-step, making it easy to adapt for your own requirements.
To follow this guide, ensure you have the following:
npm install medium-api-js dotenv
(.env)
: Create a .env
file in the root directory of your project and add your RAPIDAPI_KEY
:RAPIDAPI_KEY=your_api_key_here
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
medium-api-js
package, which simplifies interacting with Medium’s API.dotenv.config()
: Loads the .env
file and makes its variables available in process.env
.const medium = new Medium(process.env.RAPIDAPI_KEY);
Medium
instance using the RAPIDAPI_KEY
fetched from the .env
file.const tag = 'technology';
const page = 2;
“technology”
) and the page number (e.g., 2
) for the API request. You can replace these with your preferred tag and page.medium.getRecommendedFeed(tag, page)
.then(data => {
console.log('Recommended Feed:', data);
});
medium.getRecommendedFeed(tag, page)
: Sends a request to Medium’s API to retrieve the recommended articles..then(data => {...})
: Handles the response from the API. The response is logged to the console for review.Save the script in a file named getRecommendedFeed.js
and run the following command in your terminal:
node getRecommendedFeed.js
When you run the script, you’ll see output like this:
recommended_feed
: An array of article IDs representing the recommended articles for the specified tag.tag
: Confirms the tag used for fetching the feed (“technology” in this case).count
: The number of articles returned (25 here).page
: Indicates the page number requested (2 in this case).Fetching recommended feed articles from Medium by tag is straightforward using the medium-api-js
package. This script can be customized for various applications, from analytics to personalized recommendations.
If you have any questions about Medium’s API or the code, feel free to reach out to nishu@mediumapi.com for assistance.