মূল কনটেন্টে যান
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টি বই দেয়। cursor হিসেবে nextCursor অনুসরণ করুন। অধ্যায় সূচির জন্য /v1/books/{id} আনুন, তারপর সংখ্যায় লেখা অধ্যায় নম্বর দিয়ে /v1/books/{id}/chapters/{chapter}। আরও শ্লোকের জন্য data.nextOffset-কে offset হিসেবে পাঠান। পেইড অধ্যায় পেজে সর্বোচ্চ 30টি শ্লোক থাকে। স্যান্ডবক্সে নমুনা বইগুলোর প্রথম অধ্যায় ও প্রথম তিনটি শ্লোক দেখা যায়।

নেভিগেশনের জন্য বইয়ের শিরোনাম ও অধ্যায় সূচি ব্যবহার করুন। শাস্ত্রের অধ্যায়ে উপলব্ধ সংস্কৃত ও প্রতিবর্ণীকরণসহ শ্লোক আসে। লাইভ key দিয়ে /v1/books/ramayana বা /v1/books/mahabharata খুলুন এবং প্রথম অধ্যায়ের জন্য শেষে /chapters/1 যোগ করুন। format "prose" হলে উৎসের উল্লেখ দেখান এবং sections-এর প্রতিটি এন্ট্রির title ও paragraphs রেন্ডার করুন। পুরো অধ্যায় একটি রেসপন্সেই আসে।

4 কোটা

সফল বইয়ের তালিকা, বইয়ের বিস্তারিত বা অধ্যায় পেজ, প্রতিটিতে একটি করে রিকোয়েস্ট খরচ হয়। নিচের পূর্ণ উদাহরণে তিনটি কল হয়। প্রয়োজনমতো ক্যাশ করা সূচি ও পেজ আবার ব্যবহার করুন।

রিকোয়েস্টগুলো MANTRILO_API_URL থেকে বেস URL নেয়। সার্ভারের উদাহরণে তা না থাকলে https://api.mantrilo.com ব্যবহার হয়।