Fetching data from Medium can provide valuable insights for developers building content-driven applications or analytics tools. A common requirement is retrieving the lists (collections or series of articles) associated with a specific Medium user.
This guide explains how to use the medium-api-js
package to fetch these lists by leveraging a user’s unique Medium ID.
By the end of this tutorial, you’ll understand how to set up your environment, write code to fetch Medium lists, and interpret the output.
Let’s dive in!
Before we start, ensure you have the following set up:
medium-api-js
Package: Install the package using npm:npm install medium-api-js
dotenv
Package: This package helps manage environment variables. Install it using:npm install dotenv
.env
File: Create a .env
file in the root of your project to store the API key securely:RAPIDAPI_KEY=your_rapidapi_key_here
Let’s break down the code into small, easy-to-understand parts:
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
import Medium
: Brings in the medium-api-js
package to interact with the Medium API.import dotenv
: Loads environment variables from the .env
file for secure configuration.dotenv.config()
: Ensures that the environment variables defined in .env
are available in the code.const medium = new Medium(process.env.RAPIDAPI_KEY);
new Medium(process.env.RAPIDAPI_KEY)
: Creates an instance of the Medium API client using the API key stored in the environment variable.process.env.RAPIDAPI_KEY
: Securely fetches the API key from the .env
file.const userId = '5142451174a3';
userId
: Represents the unique identifier for the Medium user whose lists you want to retrieve. Replace this value with the desired user’s ID.medium.getUserListsById(userId)
.then(data => console.log('User Lists:', data));
medium.getUserListsById(userId)
: Calls the Medium API to fetch all lists associated with the specified user ID..then(data => console.log('User Lists:', data))
: Handles the response by printing the fetched lists to the console.To execute the code, run the following command in your terminal:
node getUserListsById.js
When the code runs successfully, it fetches and displays the lists associated with the specified user ID. For example:
id
: The user’s unique identifier.lists
: An array of IDs representing the lists created by the user.count
: The total number of lists associated with the user.By following this guide, you’ve successfully learned how to fetch lists created by a Medium user using their unique ID. This functionality can enhance applications that rely on Medium’s content ecosystem, providing a seamless way to manage and analyze user-generated collections.
If you have any questions about the Medium API or this implementation, feel free to reach out at nishu@mediumapi.com.
Happy coding! 🚀