కంటెంట్‌కు వెళ్లండి
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 మీ సర్వర్ నుంచి దానిని పిలవండి

key ను మీ బ్యాకెండ్‌లో ఉంచి, కంటెంట్‌ను మాత్రమే మీ యాప్‌కు తిరిగి ఇవ్వండి. ఈ 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 ను ఇస్తాయి. లైవ్ key తో /v1/books/ramayana లేదా /v1/books/mahabharata ను తెరిచి, మొదటి అధ్యాయం కోసం చివర /chapters/1 జోడించండి. format "prose" కోసం, మూల ఉల్లేఖనాన్ని చూపించి, ప్రతి sections ఎంట్రీ యొక్క title, paragraphs ను రెండర్ చేయండి. అధ్యాయం మొత్తం ఒకే రెస్పాన్స్‌లో వస్తుంది.

4 కోటా

విజయవంతమైన గ్రంథ జాబితా, గ్రంథ వివరాలు, అధ్యాయ పేజీ ఒక్కొక్కటి ఒక రిక్వెస్ట్‌ను ఉపయోగిస్తాయి. కింద ఉన్న పూర్తి ఉదాహరణ మూడు కాల్స్ చేస్తుంది. వీలైనప్పుడు క్యాష్ చేసిన సూచికలను, పేజీలను మళ్లీ వాడండి.

రిక్వెస్ట్‌లు బేస్ URL ను MANTRILO_API_URL నుంచి చదువుతాయి. సర్వర్ ఉదాహరణ ప్రత్యామ్నాయంగా https://api.mantrilo.com ను ఉపయోగిస్తుంది.