How to Get a Medium User's Articles

Using Medium API JS Package

Medium is one of the most popular blogging platforms, offering a space for creators to share their thoughts and stories with a global audience. Sometimes, developers need to programmatically fetch all articles associated with a specific user, whether for analytics, content management, or other purposes. This guide walks you through how to achieve this using the medium-api-js package.

By the end of this guide, you'll know how to fetch a Medium user's articles and understand the underlying code, the output, and potential use cases.

Prerequisites

To get started, you need the following:

Install the necessary dependencies with the following commands:

npm install medium-api-js dotenv

Code Explanation

Here's the code to fetch articles associated with a specific Medium user. We'll break it down step by step for clarity.

Step 1: Import Required Modules

import Medium from 'medium-api-js';
import dotenv from 'dotenv';

Here, the medium-api-js package provides a convenient wrapper to interact with Medium's API, while dotenv helps manage sensitive information like the API key securely.

Step 2: Load Environment Variables

dotenv.config();

This line ensures the environment variables in your .env file (such as your RAPIDAPI_KEY) are loaded into process.env.

Step 3: Initialize the Medium API Client

const medium = new Medium(process.env.RAPIDAPI_KEY);

Here, a new Medium API object is created using the API key stored in the environment variable. Replace process.env.RAPIDAPI_KEY with your actual API key if not using dotenv.

Step 4: Specify the User ID

const userId = '6e2475a6e38a';

The userId variable stores the unique identifier of the Medium user whose articles you want to fetch. Replace this ID with the desired user's ID.

Step 5: Fetch the User's Articles

medium.getUserArticles(userId)
    .then(data => {
        console.log('User Articles:', data);
    });

The getUserArticles method fetches all articles associated with the specified user ID. The result is logged to the console within the .then block.

Running the Code

To run the code, use the following command in your terminal:

node getUserArticles.js

Make sure you have saved the code in a file named getUserArticles.js and included a .env file with your API key:

RAPIDAPI_KEY=your-rapidapi-key

Output and Explanation

When the code runs successfully, it will output the user's articles, along with metadata:

Explanation of the Output

Potential Use Cases

Summary

Fetching a Medium user's articles programmatically can simplify tasks like content analytics, reporting, and archiving. With the medium-api-js package, you can quickly and efficiently access this data.

If you have any questions or need further assistance with the Medium API, feel free to reach out at nishu@mediumapi.com.

Happy coding!

nishu@mediumapi.com