In this guide, you'll learn how to fetch the latest Medium posts for a specific topic using the medium-api-js
package.
Whether you’re building an analytics dashboard, creating a content curation tool, or just exploring trending articles, this method provides an efficient way to interact with Medium’s API to retrieve up-to-date content.
To get started, make sure you have the following:
medium-api-js
Package Installed: This is a wrapper for the Medium API. Install it using npm.dotenv
Package Installed: Used to load environment variables from a .env
file securely.To install the required packages, run:
npm install medium-api-js dotenv
Let’s break down the script step by step to understand its functionality.
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
medium-api-js
: Provides an interface for interacting with Medium’s API easily.dotenv
: Used to securely manage environment variables by loading the .env
file.dotenv.config()
: Initializes the loading of environment variables, such as the API key.const medium = new Medium(process.env.RAPIDAPI_KEY);
Medium
: A new instance is created by passing the API key stored as process.env.RAPIDAPI_KEY
.medium.getLatestPosts('technology').then(data => {
console.log('Latest Posts:', data);
});
getLatestPosts
: Takes a topic as an argument (e.g., 'technology'
) and returns a promise..then(data => { ... })
: Logs the resolved data to the console.data
: Contains a list of post identifiers related to the topic.Save the code in a file named getLatestPost.js
and run the script using the following command:
node getLatestPost.js
Ensure your .env
file contains the API key as:
RAPIDAPI_KEY=your_api_key_here
Upon running the script, you’ll see an output like this:
latestposts
: Contains unique identifiers for Medium posts related to the specified topic.In this article, we explored how to fetch the latest Medium posts by topic using the medium-api-js
package. This approach simplifies interaction with Medium's API, making it easier to integrate relevant content into various applications.
Whether you're building tools for content curation, analysis, or automation, this solution is both versatile and effective.
If you have any questions or need assistance with Medium’s API, feel free to reach out to me at nishu@mediumapi.com.
Best of luck with your projects!