nHentai Node.JS API Client

3.4.3 · active · verified Wed Apr 22

nhentai-api is a Node.JS client library designed for interacting with the undocumented APIs of nhentai.net. Currently at version 3.4.3, the library exhibits an active release cadence, with recent minor updates addressing type fixes and introducing new features such as `getRandomBook`. It provides an objective-oriented API abstraction, facilitating tasks like bidirectional inspection (e.g., retrieving an image from a book or a book from an image). A key differentiating feature is its built-in support for proxies, allowing users to configure custom agents for requests, enhancing flexibility and privacy. The library focuses on returning structured data from the nhentai.net API and provides direct URLs for binary content like images. It also offers advanced search functionalities and helper methods for filtering and managing various tag types. The package ships with comprehensive TypeScript types, significantly improving developer experience and type safety.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes the nhentai API client, fetches a specific book by ID, retrieves a random book, performs a search query, and demonstrates iterating through search results using an async generator. It logs key information like book titles, image URLs, and tag details.

import { API, TagTypes } from 'nhentai-api';

async function main() {
  const api = new API();
  console.log('Fetching a specific book (ID: 177013)...');
  try {
    const book = await api.getBook(177013);
    console.log(`Book Title: ${book.title.pretty}`);
    console.log(`Cover URL: ${api.getImageURL(book.cover)}`);
    if (book.pages.length > 0) {
      console.log(`First page URL: ${api.getImageURL(book.pages[0])}`);
    }
    console.log(`Tags: ${book.tags.map(t => t.name).join(', ')}`);
    console.log(`Artists: ${book.artists.map(a => a.name).join(', ')}`);

    console.log('\nFetching a random book...');
    const randomBook = await api.getRandomBook();
    console.log(`Random Book ID: ${randomBook.id}`);
    console.log(`Random Book Title: ${randomBook.title.pretty}`);

    console.log('\nSearching for books with query "test" (first page)...');
    const searchResult = await api.search('test');
    console.log(`Search query: "${searchResult.query}"`);
    console.log(`Total search pages: ${searchResult.pages}`);
    if (searchResult.books.length > 0) {
      console.log(`First book from search: ${searchResult.books[0].title.pretty}`);
    }

    // Example of using search generator
    console.log('\nUsing search generator for the first 2 pages of "hentai"...');
    let pageCount = 0;
    for await (const search of api.searchGenerator('hentai')) {
      console.log(`  Page ${search.page}: Found ${search.books.length} books.`);
      if (search.page >= 2) break;
    }

  } catch (error: any) {
    console.error('An error occurred during API interaction:', error.message);
  }
}

main();

view raw JSON →