How to Get Books Published by a Medium User

Using JavaScript and Medium API

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!

Prerequisites

Before running the code, make sure you have the following:

Code Explanation

The goal is to fetch books published by a Medium user with a specific user ID. Here's the breakdown of the code:

1. Import Required Packages

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.

2. Initialize the Medium API

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.

3. Define the User ID

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.

4. Fetch and Display Books

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.

Running the Code

Save the code in a file, for example, getUserBooks.js. To run the code:

  1. Install the required packages:
    npm install medium-api-js dotenv
  2. Set up a .env file in the same directory with the following content:
    RAPIDAPI_KEY=your-rapidapi-key
  3. Execute the script:
    node getUserBooks.js

Output and Explanation

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.

Potential Use Cases

Summary

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!

nishu@mediumapi.com