If you're looking to fetch articles from a specific Medium publication, automating this process with a script can save time and effort. This tutorial will guide you through fetching articles using the medium-api-js package, which acts as a wrapper for the Medium API.
By the end of this tutorial, you'll be able to retrieve a list of articles from a Medium publication, using just a few lines of JavaScript code.
Before you get started, make sure you have the following:
medium-api-js and dotenv package. You can do this using the command:npm install medium-api-js dotenv.env file in your project directory to store your API key securely. Add this line to your .env file:RAPIDAPI_KEY=your_api_key_hereimport Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();Here, dotenv is used to load environment variables from the .env file, ensuring your API key stays secure. The Medium class from medium-api-js is imported to interact with the Medium API.
const medium = new Medium(process.env.RAPIDAPI_KEY);An instance of the Medium API is created using your API key. The process.env.RAPIDAPI_KEY fetches the key from your .env file.
const publicationId = '98111c9905da';
const from = '2023-01-31T13:10:00';The publicationId is the unique identifier of the Medium publication whose articles you want to fetch. Replace this with the desired publication’s ID. The from variable specifies the starting date and time for fetching articles.
medium.getPublicationArticles(publicationId, from)
.then(data => {
console.log('Publication Articles:', data);
});The getPublicationArticles method fetches articles for the specified publicationId starting from the given date and time. The then block logs the retrieved data to the console.
Save the code in a file named getPublicationArticles.js. Run the script in your terminal using the following command:
node getPublicationArticles.jsThe output will display the articles retrieved from the specified publication, including their IDs, publication ID, and the time range used for the query:
By following this tutorial, you’ve successfully learned how to fetch articles from a Medium publication using the medium-api-js package. This process is a powerful way to automate content retrieval and opens up possibilities for analytics, custom applications, and integrations.
For any doubts or queries related to Medium API or this tutorial, feel free to contact me at nishu@mediumapi.com.