गाइड · पुस्तकें
पुस्तक रीडर बनाएँ
संस्कृत श्लोक पढ़ें, या रामायण और महाभारत की अंग्रेज़ी पुनर्कथा।
यह रिक्वेस्ट सैंडबॉक्स में आज़माएँ1 काम करने वाली रिक्वेस्ट भेजें
curl "$MANTRILO_API_URL/v1/books" \
-H "Authorization: Bearer $MANTRILO_API_KEY"{
"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 · 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 कोटा
सफल पुस्तक सूची, पुस्तक विवरण या अध्याय पेज, हर एक में एक रिक्वेस्ट लगती है। नीचे दिया पूरा उदाहरण तीन कॉल करता है। जहाँ उचित हो, कैश किए गए इंडेक्स और पेज दोबारा इस्तेमाल करें।
रिक्वेस्ट base URL को MANTRILO_API_URL से पढ़ती हैं। सर्वर उदाहरण में यह न मिलने पर https://api.mantrilo.com का उपयोग होता है।
