गाइडहरू · पुस्तकहरू
पुस्तक पढ्ने एप बनाउनुहोस्
संस्कृत श्लोकहरू, वा रामायण र महाभारतका अङ्ग्रेजी पुनर्कथन पढ्नुहोस्।
यो रिक्वेस्ट sandbox मा चलाएर हेर्नुहोस्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 सर्भरले तपाईंको एपले कल गर्न मिल्ने एउटा route खोल्छ।
// 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 श्लोक हुन्छन्। Sandbox ले नमुना पुस्तकहरूको सुरुको अध्याय र पहिला तीन श्लोक देखाउँछ।
नेभिगेसनका लागि पुस्तकको शीर्षक र अध्याय सूची प्रयोग गर्नुहोस्। धर्मग्रन्थका अध्यायले उपलब्ध संस्कृत र लिप्यन्तरणसहित श्लोकहरू दिन्छन्। लाइभ key भए /v1/books/ramayana वा /v1/books/mahabharata खोल्नुहोस् र पहिलो अध्यायका लागि अन्त्यमा /chapters/1 जोड्नुहोस्। format "prose" का लागि स्रोत उद्धरण देखाउनुहोस् र हरेक sections प्रविष्टिको title र paragraphs रेन्डर गर्नुहोस्। अध्याय एउटै रेस्पोन्समा आउँछ।
4 कोटा
सफल पुस्तक सूची, पुस्तक विवरण वा अध्याय पेज हरेकले एक रिक्वेस्ट खर्च गर्छ। तलको पूरा उदाहरणले तीन कल गर्छ। उपयुक्त भएमा क्यास गरिएका सूची र पेज फेरि प्रयोग गर्नुहोस्।
रिक्वेस्टहरूले base URL MANTRILO_API_URL बाट पढ्छन्। सर्भरको उदाहरणमा त्यो नभए https://api.mantrilo.com प्रयोग हुन्छ।
