SDK Examples

Use the typed clients on your server. Start with source search, then add chat when you need a generated answer.

Install From a Checkout

Run these commands from the Madeenan repository root. The npm command creates a local package archive. Install that archive in your server application. These instructions do not require a registry release.

$pnpm install --frozen-lockfile
pnpm --filter @madeenan/sdk build
npm pack ./packages/sdk
# In your server application (replace with the archive's absolute path):
npm install /absolute/path/madeenan-sdk-0.1.0.tgz
# Python: use a virtual environment, then install from the checkout:
python -m venv .venv
.venv/bin/python -m pip install ./packages/python

Set MADEENAN_API_KEY in your server environment. Never commit it or use a NEXT_PUBLIC_ variable for it. The JavaScript client uses the native Fetch API; use Node.js 22 or supply a compatible fetch adapter.

Search and Read a Source

import { MadeenanClient } from "@madeenan/sdk";
const client = new MadeenanClient({
apiKey: process.env.MADEENAN_API_KEY,
});
const results = await client.search({
q: "patience in adversity",
indexes: ["quran", "hadith"],
limit_per_source: 3,
});
for (const source of results.results.quran ?? []) {
console.log(source.ref, source.translation);
}
const verse = await client.quran("2:153");
console.log(verse.ref, verse.edition);

Results are grouped by source type. A group can be empty. Preserve the reference, translation, grading, and metadata when you display a source. A retrieved passage is evidence to review, not a religious ruling.

Stream an Answer

import { MadeenanClient } from "@madeenan/sdk";
const client = new MadeenanClient({ apiKey: process.env.MADEENAN_API_KEY });
for await (const event of client.chatStream({
message: "What do the sources say about patience?",
})) {
if (event.event === "source") console.log(event.data.citation_label);
if (event.event === "token") process.stdout.write(event.data.token);
if (event.event === "done") console.log(event.data.citations);
if (event.event === "error") throw new Error(event.data.message);
}

Render citations from the source and done events. Handle an error event separately from an HTTP failure. Hosted chat uses Madeenan's model configuration and quota; see the Model Key Policy. Do not retry chat automatically: another request can consume more usage.

Cancel Requests and Handle Errors

import { MadeenanClient, MadeenanError } from "@madeenan/sdk";
const client = new MadeenanClient({ apiKey: process.env.MADEENAN_API_KEY });
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 30_000);
try {
await client.search("patience", { signal: controller.signal });
} catch (error) {
if (error instanceof MadeenanError) {
console.error(error.status, error.code, error.requestId);
} else {
throw error;
}
} finally {
clearTimeout(timer);
}

Keep the request ID for support. Do not log API keys, provider keys, or private request text. Network and cancellation errors can come from the Fetch API rather than MadeenanError.

Use Python

from madeenan_sdk import MadeenanClient, AsyncMadeenanClient
with MadeenanClient() as client:
results = client.search("patience", indexes=["quran", "hadith"])
for source in results.results.get("quran", []):
print(source.ref, source.translation)
# Inside your application's async function:
async def read_source():
async with AsyncMadeenanClient() as client:
verse = await client.quran_verse("2:153")
return verse

Use AsyncMadeenanClient in an async application. Both clients support source lookups, chat, streaming, and structured errors.

Security note

Browser and mobile applications must call your backend. Use the React or React Native components to render the response; do not give them a production API key.

For assistant tools instead of application code, follow MCP Setup. See Attribution before displaying source content.

Published by Madeenan Engineering · Updated