Skip to content

Get Started

Introduction

Bayan Library is retrieval infrastructure for Islamic apps: building blocks for search over the books of tafsir. You send an ayah or a question; it answers with the scholars' own passages, word for word, cited to book, volume and page.

What Bayan Library is

Bayan Library is an HTTP API over the books of tafsir on turath.io: all 275 in turath's catalog file, and the Qur'an. Each book is decoded, cleaned and cut into passages, and where the book's own structure allows, each passage is mapped to the ayahs it explains. The passages can then be read by ayah, searched by keyword, and searched by meaning.

It is for developers who build Islamic apps and agents: a Qur'an reader that shows what the mufassirun say beside an ayah, a study tool, or an assistant that must point to a scholar's words instead of writing its own. The API is plain HTTP and JSON.

The scholars speak; Bayan doesn't

No model writes anything a person is shown as an answer. The answer is always the scholars' passages, or the ayahs of the Qur'an themselves. This rule is written into the code:

  • Search returns passages, never prosePOST /v1/search answers with passages and their citations. Nothing is summarised or generated.
  • Agentic search only searchesIn POST /v1/search/guided a language model writes search queries and, each round, a true or false verdict: do these passages answer the question. The passages it finds are the answer.
  • Words stay as publishedCleaning touches the markup only: tags, footnote markers and page markers are taken out, and entities are turned back into their characters. Copies with harakat stripped and letter forms unified exist only inside search, what the models read, and quote matching. Nobody is shown them.
  • The Arabic is never machine-translatedA model may turn a query with no Arabic letter, such as an English one, into Arabic search terms. Those terms are searched, never shown.
  • Every passage is citedEach scholar's passage carries an Arabic reference built from turath's own records, such as ابن كثير (ت ٧٧٤هـ)، تفسير القرآن العظيم، ٢/ ٢٢, and links to turath's reader and, where there is one, turath's scan of the printed book.

What is in the library

  • TafsirThe 275 books in the tafsir category of turath's catalog file: 675,049 pages. The catalog marks 214 of them as having a PDF scan. Of the authors the catalog gives a death year for, the earliest is Mujahid ibn Jabr (d. 104 AH), with تفسير مجاهد.
  • The Qur'anThe Qur'an is book 0 of the library: Tanzil's Uthmani text, unchanged, one passage per ayah, credited to Tanzil Project with a link to tanzil.net as Tanzil's terms ask. Search can include the ayahs, leave them out or search them alone (filter.quran). The ayah lookup never answers with them.
  • Only the author's text is searchedThe modern editor's introductions, indexes and printer's notes are left out.
  • Footnotes are stored, not servedThey are the modern editor's words, so a passage's notes is null unless the service is set to serve them.

A book answers the ayah lookup on its own only when its own structure maps it: only the ayahs the book itself names count (by its ayah anchors, its headings or the ayah numbers it prints, not by sections that merely open by quoting an ayah), and it must either name at least 5,900 of the 6,236 ayahs or be complete over what it covers, such as a volume on a few surahs. Otherwise a person decides. A book that does not answer the ayah lookup is still searched. Of the 275 tafsirs, 80 answer the ayah lookup, 19 of them with a passage on every ayah. Of the other 195, 70 have no ayah structure at all, and 125 map only part of the Qur'an or wait for a person, among them the selective tafsir of Qatada. Books lists every one.

What is loaded, and what comes next

Phase 1 is loaded and live: every tafsir, 999,048 passages with the Qur'an's ayahs, 995,568 of them searchable. The library grows in phases:

PhaseWhatBooks in turath's catalog file
1Tafsir, and the Qur'an as book 0275
2Hadith commentaries, and the Qur'anic sciences265 and 312
3The rest of the catalog file8,609 in all, across 40 categories

The counts are those of the catalog file turath.io publishes (data-v4.sqlite). turath.io's site lists more: 502 tafsirs, and 67,430 books in all from 3 libraries.

The building blocks

Extraction

turath's book files are decoded, including the scrambled field names some of them use. Markup is removed and the words are kept. Footnotes are split from the text. The editor's pages are found and left out. Each passage is mapped to the ayahs it explains from the book's own structure: its ayah anchors and its headings, never a guess. Qur'an quotes in ﴿…﴾ are matched to the ayahs they quote, so the service can say which passages quote an ayah.

Chunking

Passages are at most 1,200 letters, counted without harakat, because a fully vowelled text has about twice the characters for the same words. A passage is not cut inside a ﴿…﴾ quote: a few run past 1,200 letters to keep a quote whole. A paragraph that the printed page cut in two is joined again. Headings are kept as the page prints them. Every passage keeps its volume, page range, heading path and ayah range.

Retrieval

There are three ways in:

  • The ayah lookupGET /v1/tafsir/:surah/:ayah is an indexed lookup, the same for everyone, and cached at the edge.
  • Keyword searchSQLite FTS5 in Cloudflare D1, with an Arabic normaliser and a light stemmer (after light10). Each word matches in its normalised form or its stem. A query of two or more words is also run as a phrase: phrase matches rank first, then passages that hold every word. When those are fewer than 10, passages that hold any of the query's three rarest words are added. Each tier is ranked by bm25.
  • Meaning searchThe query is embedded (the configured model is @cf/google/embeddinggemma-2 on Workers AI, at 256 dimensions) and matched against the passages' vectors in Cloudflare Vectorize.

The default search runs keyword and meaning search at the same time and fuses the two lists with reciprocal rank fusion (k = 60). As configured, nothing is reranked: the service has no reranker set (RERANKER is "none"), because in its model test @cf/baai/bge-reranker-base lowered the quality of results. When a reranker is set, the best 30 passages, or rerankLimit if that is more, are reranked, and if reranking fails or times out the fused order is kept. If one search path fails, the other still answers.

POST /v1/search/guided runs a short loop. In each round a language model writes up to 5 searches, each keyword or semantic. The first round also searches the person's own words by meaning. The service runs the searches, merges what they find and reranks it against the person's own words. The model then says whether the passages answer the question. The loop stops when the model says yes, after 3 rounds, or once 4,096 tokens are spent. It stops early too when the model has no new search to offer, when a model call fails, or when 30 seconds have passed before the next round would start. The answer holds the passages, the searches the model wrote, the number of rounds and the verdict.

Base URL, access and limits

Every call is made to one base URL:

Base URL
https://grep.getbayan.app

No API key and no sign-in are needed. Requests are rate-limited per IP address. An IPv4 address is counted on its own; an IPv6 address is counted with its whole /64. Counts run over 60 seconds.

LimiterWhat countsLimit
SearchPOST /v1/search counts 1. POST /v1/search/guided counts 5.60 per 60 seconds
ReadEvery GET of an endpoint that reaches the service, /health included300 per 60 seconds

Over the limit, the answer is 429 with the code rate_limited and a Retry-After: 60 header. A GET answered from Cloudflare's cache never reaches the service, so it is not counted. Cloudflare keeps the counters per location.

Caching

  • GET answers carry Cache-Control: public, max-age=3600, s-maxage=86400: a day at the edge, an hour in your app. Loading a book purges the edge. /health is never cached.
  • Search answers carry Cache-Control: no-store. The service keeps each answer itself for an hour, keyed by the library's version, so loading a book changes the key.
  • Error answers are always no-store.

Conventions

  • JSON in and out. A POST body must be a JSON object sent with Content-Type: application/json, at most 8,192 bytes.
  • Unknown parameters and fields are refused with 400, so a typo such as ?book=1503 fails at once instead of answering for other books. /health is the one exception.
  • Numbers in paths and query strings are plain decimals, with no sign and no leading zeros.
  • Passage ids are strings. An id is the book's id × 1,000,000 + the passage's place in the book, counted from 0: 1503000934 is the 935th passage of book 1503. Neighbouring passages have neighbouring ids.
  • No CORS headers are sent, and a preflight OPTIONS request is answered 405. Call the API from a server or a native app. Browser code on another origin cannot read its answers.

Errors

Every error has the same shape:

Response
{"error": {"code": "invalid_request","message": "Surah 1 has no ayah 8."}}
StatusCodeMeaning
400invalid_requestThe request is wrong. The message names the field. Do not send it again unchanged.
404not_foundNo such book, passage or path, or agentic search is off.
405method_not_allowedThe path exists but not with this method. The Allow header lists the methods it takes.
413payload_too_largeThe body is over 8,192 bytes.
415unsupported_media_typeThe body was not sent as application/json.
429rate_limitedToo many requests. Wait for Retry-After seconds.
500internalSomething went wrong in the service. Try again later.
503search_unavailableEvery search path that was tried failed. Try again shortly.

Endpoints

CallWhat it answers
GET /v1/tafsir/:surah/:ayahWhat the tafsirs say about one ayah, paged. books (up to 10 ids), limit (1–100, default 40), after.
GET /v1/quotes/:surah/:ayahThe passages that quote the ayah in ﴿…﴾, paged like the ayah lookup.
POST /v1/searchKeyword and meaning search, fused (and reranked, when the service has a reranker). Filters for books, authors, categories, surah, ayah and the Qur'an.
POST /v1/search/guidedAgentic search. Off by default.
GET /v1/passages/:id/contextThe passage with its neighbours in the same book: before and after, 0–5 each, default 2.
GET /v1/passages/:id/sourceThe passage with the rest of its page, at most 20 passages.
GET /v1/booksThe books the library holds. cat picks one category; cat=0 lists the Qur'an.
GET /v1/books/:idOne book. Book 0 is the Qur'an, with Tanzil's credit and copyright notice.
GET /health{ "ok": true, "version": "…" }, where version is the library's version.

Your first request

Look up an ayah

Ask what the tafsirs say about Al-Baqarah 2:156:

curl
curl https://grep.getbayan.app/v1/tafsir/2/156
Response
{"surah": 2,"ayah": 156,"books": [{"book": { "id": 41, "name": "تفسير البغوي - طيبة", "edition": "…" },"author": { "id": 4, "name": "البغوي، أبو محمد", "died": 516 },"passages": 6},…],"passages": [{"id": "1503000934","kind": "passage","book": { "id": 1503, "name": "تفسير ابن كثير - ط ابن الجوزي", "edition": "…" },"author": { "id": 3, "name": "ابن كثير", "died": 774 },"vol": "2","page": 22,"pageEnd": 22,"heading": ["تفسير سُورَةُ البَقَرَة", …],"ayahs": { "surah": 2, "from": 156, "to": 157 },"text": "ثم بيَّن تعالى من الصابرين الذين شكرهم فقال: …","notes": null,"quotes": [{ "surah": 2, "from": 156, "to": 157 }],"citation": {"text": "ابن كثير (ت ٧٧٤هـ)، تفسير القرآن العظيم، ٢/ ٢٢","author": "ابن كثير","died": 774,"diedLabel": null,"book": "تفسير القرآن العظيم","edition": "المحقق: حكمت بن بشير بن ياسين؛ الناشر: دار ابن الجوزي للنشر والتوزيع - السعودية؛ الطبعة: الأولى، ١٤٣١ هـ","vol": "2","page": 22,"pageEnd": 22,"printed": true},"source": {"reader": "https://app.turath.io/book/1503?page=727","pdf": { "url": "https://files.turath.io/pdf/ibnkatheer_jawzee/02_113016.pdf", "page": 22 },"provider": "turath.io","attribution": null}},…],"next": null,"quotedBy": 179}
  • books lists every book that answers the ayah lookup and has a passage on this ayah, with how many passages each has on it.
  • passages are from the books you ask for with ?books=, in your order. With none asked, they are from the default tafsirs: Ibn Kathir (1503), al-Baghawi (41), as-Sa'di (42) and al-Jalalayn (12876), in that order. Each book's passages come in the order they stand in the book.
  • next is the id to pass as ?after= for the next page, or null when there is no more.
  • quotedBy is how many passages quote the ayah. GET /v1/quotes/2/156 lists them.

Search the library

Search al-Qurtubi (20855), Ibn Kathir (1503) and al-Baghawi (41) for الصبر عند المصيبة (patience at a calamity), within Al-Baqarah:

curl
curl https://grep.getbayan.app/v1/search \-H "Content-Type: application/json" \-d '{"query": "الصبر عند المصيبة","mode": "keyword","rerank": false,"topK": 3,"filter": { "surah": 2, "books": [20855, 1503, 41] }}'
Response
{"query": "الصبر عند المصيبة","language": "ar","translated": false,"results": [{"id": "20855001039","kind": "passage","book": { "id": 20855, "name": "تفسير القرطبي = الجامع لأحكام القرآن", "edition": "…" },"author": { "id": 115, "name": "القرطبي", "died": 671 },"vol": "2","page": 174,"pageEnd": 174,"heading": ["تفسير سورة البقرة", "[سورة البقرة (٢): آية ١٥٥]"],"ayahs": { "surah": 2, "from": 155, "to": 155 },"text": "… لَكِنْ لَا يَكُونُ ذَلِكَ إِلَّا بِالصَّبْرِ عِنْدَ الصَّدْمَةِ الْأُولَى …","notes": null,"quotes": [],"citation": { "text": "القرطبي (ت ٦٧١هـ)، الجامع لأحكام القرآن، ٢/ ١٧٤", "printed": true, … },"source": {"reader": "https://app.turath.io/book/20855?page=646","pdf": { "url": "https://files.turath.io/pdf/156609/02_156605.pdf", "page": 174 },"provider": "turath.io","attribution": null},"score": 0.01639344262295082},{ "id": "1503000494", … },{ "id": "20855000598", … }],"ayahs": [{ "surah": 2, "from": 155, "to": 155, "passages": 1 },{ "surah": 2, "from": 45, "to": 46, "passages": 2 }]}
  • This example asks for keyword search alone and no reranking, so it needs no model. Leave out mode and rerank for the default: both searches, fused, and reranked when the service has a reranker. As configured it has none, so rerank changes nothing for now. When reranking runs, each result also carries a rerankScore from 0 to 1.
  • language is "ar" when the query has an Arabic letter and "other" when it has none. Keyword search holds Arabic only, so it skips a query with no Arabic letter unless the query is translated.
  • translated says whether Arabic search terms a model wrote for the query were searched too. The terms themselves are never returned.
  • score is the fused rank score. ayahs lists the ayahs the results point at, their own and the ones they quote, best first, at most 10.

The passage

Every call that returns text returns it as passages of one shape:

FieldTypeWhat it holds
idstringThe passage's id.
kind"passage" | "ayah"A scholar's words, or an ayah of the Qur'an (book 0).
bookobjectid, name, and edition: turath's edition line (editor, publisher, printing).
authorobject | nullid, name, died (hijri year or null), and diedLabel only when the year is not exact. null for the Qur'an.
vol, page, pageEndstring, number, numberWhere the passage is in the book. Each may be null.
headingstring[]The headings the passage sits under, outermost first, as the page prints them where it can.
ayahsobject | null{ surah, from, to }: the ayahs the passage explains, or null.
textstringThe words, unchanged. Paragraphs on \n; Qur'an quotes in ﴿…﴾.
notesstring | nullThe editor's footnotes. null unless the service serves them.
quotesobject[]The ayahs its ﴿…﴾ quotes were matched to, in the Qur'an's order.
citationobjecttext, the Arabic reference, and its parts: author, died, diedLabel, book, edition, vol, page, pageEnd, and printed (whether the page numbers are the printed edition's).
sourceobjectreader (turath's reader, or Quran.com for an ayah), pdf ({ url, page } or null), provider, and attribution (Tanzil's credit for an ayah).
scorenumberSearch only: the fused rank score.
rerankScorenumberSearch only, when reranking ran: 0 to 1.

When turath does not say that a book's page numbers are the printed edition's, the citation says so after the page: ترقيم النسخة الإلكترونية.