ഉള്ളടക്കത്തിലേക്ക് പോകുക
API key നേടുക

ഗൈഡുകൾ · പുസ്തകങ്ങൾ

ഒരു പുസ്തക റീഡർ നിർമ്മിക്കുക

സംസ്കൃത ശ്ലോകങ്ങളോ രാമായണത്തിന്റെയും മഹാഭാരതത്തിന്റെയും ഇംഗ്ലീഷ് പുനരാഖ്യാനങ്ങളോ വായിക്കുക.

ഈ റിക്വസ്റ്റ് സാൻഡ്‌ബോക്സിൽ പരീക്ഷിക്കുക

1 പ്രവർത്തിക്കുന്ന ഒരു റിക്വസ്റ്റ് നടത്തുക

GET /v1/books
curl "$MANTRILO_API_URL/v1/books" \
  -H "Authorization: Bearer $MANTRILO_API_KEY"
പുസ്തക ലിസ്റ്റിന്റെ ഭാഗം200 OK
{
  "data": [
    {
      "id": "bhagavadGita",
      "title": "Bhagavad Gītā",
      "author": "Vyāsa · Spoken by Lord Krishna",
      "verseCount": 700
    }
  ],
  "nextCursor": null
}
  • പുസ്തകവും അധ്യായ ഇൻഡക്സുംGET /v1/books/bhagavadGita
  • ആദ്യ അധ്യായംGET /v1/books/bhagavadGita/chapters/1

2 നിങ്ങളുടെ സെർവറിൽ നിന്ന് വിളിക്കുക

കീ നിങ്ങളുടെ ബാക്കെൻഡിൽ സൂക്ഷിച്ച് ഉള്ളടക്കം മാത്രം ആപ്പിലേക്ക് തിരികെ നൽകുക. ഈ Node.js സെർവർ നിങ്ങളുടെ ആപ്പിന് വിളിക്കാവുന്ന ഒരു റൂട്ട് ലഭ്യമാക്കുന്നു.

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');
നിങ്ങളുടെ കാഴ്ചയിൽ
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 ഇൻപുട്ടുകളും പേജിനേഷനും

GET /v1/books പരമാവധി 24 പുസ്തകങ്ങൾ നൽകുന്നു. nextCursor, cursor ആയി പിന്തുടരുക. അധ്യായ ഇൻഡക്സിനായി /v1/books/{id} ഫെച്ച് ചെയ്യുക, തുടർന്ന് അതിലെ അധ്യായ നമ്പർ ഉപയോഗിച്ച് /v1/books/{id}/chapters/{chapter} ഫെച്ച് ചെയ്യുക. കൂടുതൽ ശ്ലോകങ്ങൾക്കായി data.nextOffset, offset ആയി പിന്തുടരുക. പണമടച്ചുള്ള അധ്യായ പേജുകളിൽ പരമാവധി 30 ശ്ലോകങ്ങൾ ഉണ്ടാകും. സാൻഡ്‌ബോക്സ് അതിന്റെ സാമ്പിൾ പുസ്തകങ്ങളുടെ ആദ്യ അധ്യായവും ആദ്യത്തെ മൂന്ന് ശ്ലോകങ്ങളും കാണിക്കുന്നു.

നാവിഗേഷനായി പുസ്തകത്തിന്റെ ശീർഷകവും അധ്യായ ഇൻഡക്സും ഉപയോഗിക്കുക. ഗ്രന്ഥ അധ്യായങ്ങൾ ലഭ്യമായ സംസ്കൃതവും ലിപ്യന്തരണവും verses ആയി നൽകുന്നു. ലൈവ് കീ ഉപയോഗിച്ച് /v1/books/ramayana അല്ലെങ്കിൽ /v1/books/mahabharata തുറക്കുക, ആദ്യ അധ്യായത്തിനായി /chapters/1 ചേർക്കുക. format "prose" ആണെങ്കിൽ, ഉറവിട ഉദ്ധരണി കാണിച്ച് ഓരോ sections എൻട്രിയുടെയും title, paragraphs എന്നിവ റെൻഡർ ചെയ്യുക. അധ്യായം മുഴുവൻ ഒറ്റ റെസ്പോൺസിൽ ലഭിക്കും.

4 ക്വോട്ട

വിജയകരമായ ഓരോ പുസ്തക ലിസ്റ്റും പുസ്തക വിശദാംശവും അധ്യായ പേജും ഓരോ റിക്വസ്റ്റ് ഉപയോഗിക്കുന്നു. താഴെയുള്ള പൂർണ്ണ ഉദാഹരണം മൂന്ന് കോളുകൾ ചെയ്യുന്നു. ഉചിതമായിടത്ത് കാഷെ ചെയ്ത ഇൻഡക്സുകളും പേജുകളും വീണ്ടും ഉപയോഗിക്കുക.

റിക്വസ്റ്റുകൾ ബേസ് URL MANTRILO_API_URL-ൽ നിന്ന് വായിക്കുന്നു. സെർവർ ഉദാഹരണം ബദലായി https://api.mantrilo.com ഉപയോഗിക്കുന്നു.