Accessing the top articles of a Medium user can provide valuable insights, whether you're conducting an analysis of popular content or curating top-performing posts for promotional purposes.
This guide walks you through a simple JavaScript implementation to fetch the top articles of a Medium user using the medium-api-js
package.
Before diving into the code, ensure you have the following set up:
medium-api-js
package by running:npm install medium-api-js
dotenv
to manage environment variables securely:npm install dotenv
.env
file in the root of your project and add your RapidAPI key:RAPIDAPI_KEY=your_rapidapi_key_here
The implementation involves importing the necessary modules, initializing the Medium API client, and fetching the user's top articles. Here's a step-by-step breakdown:
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
medium-api-js
provides methods to interact with Medium's API..env
file.const medium = new Medium(process.env.RAPIDAPI_KEY);
process.env.RAPIDAPI_KEY
: Fetches the RapidAPI key from the .env
file.new Medium(...)
: Creates an instance of the Medium API client for making authenticated requests.const userId = '1985b61817c3';
userId
: Represents the unique identifier of the Medium user whose top articles you want to fetch. Replace this with the desired user ID.medium.getUserTopArticles(userId)
.then(data => console.log('User Top Articles:', data));
medium.getUserTopArticles(userId)
: Makes an API request to retrieve the user's top articles..then(data => ...)
: Processes the returned data and logs it to the console.To execute the script, open your terminal and run:
node getUserTopArticles.js
Upon successful execution, the console displays the top articles of the specified user in a JSON format:
id
: The user ID of the Medium profile.top_articles
: An array of article IDs representing the top-performing articles of the user.By using the medium-api-js
package and following this simple implementation, you can effortlessly fetch the top articles of any Medium user. This functionality can be a powerful tool for content creators, marketers, and researchers aiming to leverage Medium data effectively.
If you have any questions or need further assistance with Medium API, feel free to reach out to nishu@mediumapi.com.