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.
To get started, you need the following:
medium-api-js
package in your Node.js project.Install the necessary dependencies with the following commands:
npm install medium-api-js dotenv
Here's the code to fetch articles associated with a specific Medium user. We'll break it down step by step for clarity.
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.
dotenv.config();
This line ensures the environment variables in your .env
file (such as your RAPIDAPI_KEY
) are loaded into process.env
.
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
.
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.
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.
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
When the code runs successfully, it will output the user's articles, along with metadata:
id
: The user's unique identifier.associated_articles
: An array of article IDs associated with the user.count
: The total number of articles.next
: A token for fetching additional results if the user has more than 500 articles.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!