Skip to content
Get API key

Guides · Books

Build a book reader

Read Sanskrit verses or English retellings of the Ramayana and Mahabharata.

Try this request in the sandbox

1 Make a working request

GET /v1/books
curl "$MANTRILO_API_URL/v1/books" \
  -H "Authorization: Bearer $MANTRILO_API_KEY"
Book list excerpt200 OK
{
  "data": [
    {
      "id": "bhagavadGita",
      "title": "Bhagavad Gītā",
      "author": "Vyāsa · Spoken by Lord Krishna",
      "verseCount": 700
    }
  ],
  "nextCursor": null
}
  • Book and chapter indexGET /v1/books/bhagavadGita
  • First chapterGET /v1/books/bhagavadGita/chapters/1

2 Call it from your server

Keep the key in your backend and return only the content to your app. This Node.js server exposes one route your app can call.

server.mjs
// server.mjs · Node.js 22+
import { createServer } from 'node:http';

const apiKey = process.env.MANTRILO_API_KEY;
if (!apiKey) throw new Error('Set MANTRILO_API_KEY on your server.');
const base = process.env.MANTRILO_API_URL ?? "https://api.mantrilo.com";

async function mantrilo(path, input) {
  const response = await fetch(base + path, {
    method: input ? 'POST' : 'GET',
    headers: {
      Authorization: 'Bearer ' + apiKey,
      ...(input ? { 'Content-Type': 'application/json' } : {}),
    },
    ...(input ? { body: JSON.stringify(input) } : {}),
    signal: AbortSignal.timeout(20000),
    redirect: 'error',
  });
  const result = await response.json();
  if (!response.ok) throw Object.assign(new Error('Mantrilo request failed'), {
    status: response.status, requestId: result.requestId,
  });
  return result;
}

async function load(incoming) {
  const listing = await mantrilo('/v1/books');
  const first = listing.data[0];
  if (!first) return { data: null };
  const path = '/v1/books/' + encodeURIComponent(first.id);
  const book = await mantrilo(path);
  const chapter = book.data.chapters[0];
  if (!chapter) return { data: null, book: book.data };
  const offset = incoming.searchParams.get('offset');
  const page = await mantrilo(path + '/chapters/' + chapter.number +
    (offset ? '?' + new URLSearchParams({ offset }) : ''));
  return { ...page, book: book.data };
}

createServer(async (req, res) => {
  const incoming = new URL(req.url, 'http://localhost');
  res.setHeader('Content-Type', 'application/json');
  if (req.method !== 'GET' || incoming.pathname !== '/api/mantrilo-guide') {
    res.writeHead(404).end(JSON.stringify({ error: 'Not found' }));
    return;
  }
  try {
    const result = await load(incoming);
    res.end(JSON.stringify(result));
  } catch (error) {
    res.writeHead(error.status ?? 502).end(JSON.stringify({
      error: 'Could not load this content',
      requestId: error.requestId,
    }));
  }
}).listen(3000, '127.0.0.1');
In your view
if (data?.format === 'prose') {
  const { sections, source } = data;
  // Display source, then each section’s title and paragraphs.
} else {
  const verses = data?.verses ?? [];
  // Display the verse page and follow nextOffset when present.
}

3 Inputs and pagination

GET /v1/books returns up to 24 books. Follow nextCursor as cursor. Fetch /v1/books/{id} for the chapters index, then /v1/books/{id}/chapters/{chapter} using its numeric chapter number. Follow data.nextOffset as offset for more verses. Paid chapter pages contain up to 30 verses. Sandbox shows the opening chapter and first three verses of its sample books.

Use the book title and chapter index for navigation. Scripture chapters return verses for available Sanskrit and transliteration. With a live key, open /v1/books/ramayana or /v1/books/mahabharata and append /chapters/1 for the first chapter. For format "prose", show the source citation and render each sections entry’s title and paragraphs. The chapter comes in one response.

4 Quota

A successful book list, book detail, or chapter page each uses one request. The complete example below makes three calls. Reuse cached indexes and pages when appropriate.

Requests read the base URL from MANTRILO_API_URL. The server example falls back to https://api.mantrilo.com.