Get Medium Article's Content

(Using Javascript)

Medium has become a go-to platform for insightful articles, especially for developers, enthusiasts, and professionals. Accessing Medium content programmatically can be useful for analysis, content curation, or personalized applications.

This guide demonstrates how to fetch the content of a specific Medium article using the "medium-api-js" package. By the end, you'll know how to retrieve an article's details, such as its text content, using a Medium API key.

Prerequisites

Before running the code, ensure the following:

Code Walkthrough

1. Import Dependencies

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

Medium: It provides methods to interact with Medium's API.

dotenv: A package to load environment variables from a .env file.

2. Load Environment Variables

dotenv.config();

This line reads the .env file and loads environment variables like RAPIDAPI_KEY into process.env. It enables secure, reusable storage of sensitive information.

3. Initialize the Medium Client

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

A new instance of the Medium class is created using the API key loaded from process.env.

4. Specify the Article ID

const articleId = '67fa62fc1971';

Replace '67fa62fc1971' with the unique ID of the article you want to fetch.

5. Fetch and Log Article Content

medium.getArticleContent(articleId).then(data => {
    console.log('Article Content:', data);
});

getArticleContent(articleId): Fetches the article content using the medium-api-js library.

then(data => {...}): Handles the asynchronous response, logging the article's content to the console.

Running the Code

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

node getArticleContent.js

Output Explanation

Upon execution, the output logs the article's content in JSON format. Here's an example:

Key Details in the Output:

Potential Use Cases

Summary

Using the medium-api-js package makes it seamless to interact with Medium's API and fetch article content programmatically. This tutorial demonstrated how to set up the environment, write the code, and retrieve the content of a Medium article efficiently.

If you have any questions about Medium API or need further clarification, feel free to reach out to nishu@mediumapi.com.

Happy coding!

nishu@mediumapi.com