How to Get Articles from a Medium Publication

Using the Medium API JS Package

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.

Prerequisites

Before you get started, make sure you have the following:

Code Walkthrough

Step 1: Import Required Modules

import 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.

Step 2: Initialize the Medium API Instance

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.

Step 3: Specify the Publication ID and Time Frame

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.

Step 4: Fetch Articles and Log the Response

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.

Running the Code

Save the code in a file named getPublicationArticles.js. Run the script in your terminal using the following command:

node getPublicationArticles.js

Output Explanation

The output will display the articles retrieved from the specified publication, including their IDs, publication ID, and the time range used for the query:

Potential Use Cases

Summary

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.

nishu@mediumapi.com