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.
Before running the code, ensure the following:
medium-api-js
: npm install medium-api-js.dotenv
: npm install dotenv..env
file in your project directory with the following content:RAPIDAPI_KEY=your_rapidapi_key_here
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.
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.
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
.
const articleId = '67fa62fc1971';
Replace '67fa62fc1971'
with the unique ID of the article you want to fetch.
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.
To run the script in your terminal, use the following command:
node getArticleContent.js
Upon execution, the output logs the article's content in JSON format. Here's an example:
Key Details in the Output:
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!