Have you ever wanted to discover books written by a specific Medium user? Whether you’re a book lover seeking new reads or a developer building a tool to explore authors’ works, accessing this information programmatically can be incredibly useful.
This guide demonstrates how to retrieve the list of books published by a Medium user using the medium-api-js
package. Let’s dive in!
Before running the code, make sure you have the following:
medium-api-js
package uses RapidAPI for authentication..env
file.The goal is to fetch books published by a Medium user with a specific user ID. Here's the breakdown of the code:
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
The medium-api-js
library is used to interact with the Medium API. The dotenv
package loads environment variables from a .env
file, ensuring your API key remains secure and not hardcoded in the source file.
const medium = new Medium(process.env.RAPIDAPI_KEY);
An instance of the Medium API is created using your RapidAPI key, which is retrieved securely from the environment variables.
const userId = '6e2475a6e38a';
The user ID corresponds to the Medium account for which you want to fetch the books. Replace this value with the appropriate user ID.
medium.getUserBooks(userId)
.then(data => {
console.log('User Books:', data);
});
The getUserBooks
method retrieves books associated with the specified user ID. The data is then logged to the console.
Save the code in a file, for example, getUserBooks.js
. To run the code:
npm install medium-api-js dotenv
.env
file in the same directory with the following content:RAPIDAPI_KEY=your-rapidapi-key
node getUserBooks.js
When you run the code, you’ll see an output similar to this:
The output contains the user’s ID, a list of books with details such as name, description, authors, publication date, cover URL, and more. The count
property indicates the total number of books retrieved.
By following this guide, you can easily retrieve books published by any Medium user programmatically. This feature is valuable for developers, book enthusiasts, and content creators aiming to explore Medium authors' published works.
If you have any questions about the Medium API or this code, feel free to reach out to me at nishu@mediumapi.com.
Start exploring and build amazing applications with the Medium API today!