मार्गदर्शिका · ग्रंथ
ग्रंथ वाचक तयार करा
रामायण आणि महाभारताचे संस्कृत श्लोक किंवा इंग्रजी पुनर्कथन वाचा.
ही रिक्वेस्ट सँडबॉक्समध्ये वापरून पाहा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 कोटा
यशस्वी ग्रंथ-यादी, ग्रंथ-तपशील किंवा अध्याय-पान यासाठी प्रत्येकी एक रिक्वेस्ट वापरली जाते. खालील संपूर्ण उदाहरण तीन कॉल करते. योग्य असेल तेव्हा कॅशे केलेल्या अनुक्रमणिका आणि पाने पुन्हा वापरा.
रिक्वेस्ट बेस URL MANTRILO_API_URL मधून घेतात. ते नसल्यास सर्व्हर उदाहरण https://api.mantrilo.com वापरते.
