Searching for articles on Medium based on specific topics or keywords can help you gather insightful content, analyze trends, or even curate content for your projects.
This guide demonstrates how to search for articles on Medium using the "medium-api-js" package, breaking the process down step by step.
To get started, ensure you have the following:
medium-api-js
Package: Install the Medium API JavaScript wrapper.dotenv
Package: Manage your environment variables securely.Install the required packages using npm:
npm install medium-api-js dotenv
This script searches for articles on Medium using a keyword and prints the results to the console. Here is a step-by-step explanation:
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
Medium
: Imported from the medium-api-js
package to interact with the Medium API.dotenv
: Used to load environment variables from a .env
file, ensuring sensitive data like API keys remain secure.dotenv.config()
: Initializes the .env
configuration.const medium = new Medium(process.env.RAPIDAPI_KEY);
process.env.RAPIDAPI_KEY
: Fetches the API key from the .env
file.new Medium
: Initializes the Medium API client with the provided key.medium.getSearchArticles('technology').then(data => {
console.log('Search Results for Articles:', data);
});
getSearchArticles('technology')
: Searches Medium for articles matching the keyword "technology"..then(data => { ... })
: Returns the result as a promise and logs the data to the console.Save the script as getSearchArticles.js
and execute it in the terminal:
node getSearchArticles.js
The output will be an object containing:
articles
: An array of article IDs matching the search query.search_query
: The keyword used for the search.articles
array represents an article related to the search query.By following this guide, you’ve learned how to search for articles on Medium programmatically using JavaScript and the medium-api-js
library.
Whether you’re a developer, researcher, or content curator, this approach simplifies the process of accessing relevant Medium content.
For any questions or assistance, feel free to reach out at nishu@mediumapi.com.