If you want to extract Medium's top articles for a specific tag programmatically, the Medium API is your go-to solution.
This article will guide you through how to fetch top feeds for a given tag and mode using JavaScript. By the end of this guide, you'll have a clear understanding of the code implementation, its output, and its potential applications.
Before diving into the implementation, make sure you have the following:
dotenv
to securely manage your API key.npm install medium-api-js
.The code leverages the medium-api-js
package to fetch the top feeds for a specified tag and mode. Here's a detailed breakdown:
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
medium-api-js
package provides an easy-to-use wrapper for the Medium API..env
file, keeping sensitive data like API keys secure.const medium = new Medium(process.env.RAPIDAPI_KEY);
The Medium
instance is initialized with the API key, which is stored in the environment variable RAPIDAPI_KEY
. Make sure to create a .env
file with the following content:
RAPIDAPI_KEY=your_api_key_here
medium.getTopFeeds('data-science', 'NEW')
.then(data => {
console.log('Top Feeds:', data);
});
getTopFeeds(tag, mode)
: This method fetches the top feeds for the specified tag and mode. In this case:tag
: 'data-science'
(fetch articles tagged with "data-science").mode
: 'NEW'
(fetch the newest articles).To execute the code:
getTopFeeds.js
.node getTopFeeds.js
The output displays the top feeds for the specified tag and mode:
topfeeds
: An array of IDs representing the top articles.tag
: The queried tag.mode
: The queried mode.count
: The total number of articles fetched.Fetching top feeds from Medium using the Medium API is both simple and powerful. This article demonstrated how to retrieve curated content for specific tags and modes, enabling diverse applications like trend analysis and personalized content delivery.
If you have any questions about the Medium API or need further assistance, feel free to reach out to me at nishu@mediumapi.com.