Discovering articles that are thematically connected to a specific Medium article can be incredibly useful. Whether you want to explore more on a topic, curate content for readers, or analyze patterns in writing, fetching related articles programmatically is an efficient approach.
This article explains how to use the medium-api-js
library to get related Medium articles based on a given article ID.
By the end of this guide, you’ll understand the code and process to achieve this, and you'll be equipped to adapt the method for various use cases.
Before diving into the code, ensure you have the following:
medium-api-js
package: This is a wrapper for the Medium API. Install it using npm:npm install medium-api-js
dotenv
package: This is required for securely managing your API key. Install it with:npm install dotenv
.env
file in the following format:RAPIDAPI_KEY=your_medium_api_key_here
https://medium.com/article-id
, the article-id
part is what you need).This code fetches related articles for a specific Medium article. Let’s break it down step by step:
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
Medium
: This is the medium-api-js
package, which provides a set of methods for interacting with the Medium API.dotenv
: This package loads environment variables from a .env
file, ensuring your API key remains secure.dotenv.config()
: Initializes the environment variable loader.const medium = new Medium(process.env.RAPIDAPI_KEY);
new Medium(process.env.RAPIDAPI_KEY)
: Creates an instance of the Medium API client, using the API key stored in the .env
file.const articleId = '67fa62fc1971';
articleId
: This is the ID of the Medium article for which you’ll fetch related articles. Replace it with your desired article’s ID.medium.getRelatedArticles(articleId).then(data => {
console.log('Related Articles:', data);
});
medium.getRelatedArticles(articleId)
: This method retrieves related articles for the given articleId
..then(data => { console.log('Related Articles:', data); })
: Handles the API’s response and logs the related articles to the console.Save the code in a file named getRelatedArticles.js
. Run it in the terminal using the command:
node getRelatedArticles.js
When the code runs successfully, you’ll see output similar to this:
related_articles
: An array of article IDs related to the specified article. These can be used to fetch details about the articles using other Medium API methods.id
: Confirms the ID of the article for which related articles were fetched.In this article, we explored how to use the medium-api-js
library to fetch related Medium articles, a feature that enables efficient content curation, analysis, and recommendations. With minimal setup, this method can be integrated into larger projects to enhance user engagement or streamline research workflows.
If you have any questions about Medium API or the code implementation, feel free to reach out at nishu@mediumapi.com.