Skip to content

Search and Retrieval

Citations

Every passage the API returns carries its own citation: the book, its author, the volume and page, and links to the passage at its source. This page shows how to read those fields, how to write a reference from them, and how to show a passage so that its reference stays true.

What a passage carries

Every route that answers passages builds them with one function, from one row shape: POST /v1/search, POST /v1/search/guided, GET /v1/tafsir/:surah/:ayah, GET /v1/quotes/:surah/:ayah, GET /v1/passages/:id/context and GET /v1/passages/:id/source. A citation is never a second request. These are the fields a citation is made from.

FieldTypeWhat it holds
book.idnumberturath.io’s id for the book. 0 is the Qur’an.
book.namestringturath’s name for the book, which can name the edition: تفسير ابن كثير - ط ابن الجوزي.
book.editionstring or nullturath’s edition line, one item to a line, such as title, author, editor, publisher and printing.
authorobject or nullid, name (in Arabic, as turath gives it) and died, the Hijri year of death, or null when it is not known. diedLabel is there only when turath marks the year as not exact. null for the Qur’an.
volstring or nullThe volume label as turath gives it: "2", or a named part such as المقدمة. For a passage that runs into the next volume, the volume of its first page.
pagenumber or nullThe page where the passage starts.
pageEndnumber or nullThe page where it ends.
headingstring[]The headings the passage sits under, outermost first, as the page prints them.
ayahsobject or nullThe ayahs the passage explains: { surah, from, to }.
quotesobject[]The ayahs its ﴿…﴾ quotes were matched to, in the Qur’an’s order.
textstringThe passage, words unchanged. Paragraphs on "\n", Qur’an quotes in ﴿…﴾.
notesstring or nullThe editor’s footnotes. null unless the service’s SERVE_NOTES is "1".
citationobjectThe reference, already written. See The citation object.
sourceobjectWhere to read the passage at its source. See Links to the source.

The page numbers are the printed edition’s only when citation.printed is true. Otherwise they are not known to be, and the citation marks them as the numbering of turath’s electronic copy.

An example passage

Passage 1503000934 is Ibn Kathir on 2:156–157: the first of his four passages on these ayahs, in volume 2 of the Dar Ibn al-Jawzi edition, on page 22. The context route with before=0 and after=0 answers it alone.

curl
curl "https://grep.getbayan.app/v1/passages/1503000934/context?before=0&after=0"
Response
{"passages": [{"id": "1503000934","kind": "passage","book": {"id": 1503,"name": "تفسير ابن كثير - ط ابن الجوزي","edition": "الكتاب: تفسير القرآن العظيم\nالمؤلف: ابن كثير\nالمحقق: حكمت بن بشير بن ياسين\n(اختصر عمل الشيخ أبي إسحاق الحويني من أول الكتاب إلى الآية ١٤١ من سورة البقرة (وهو جـ ١ هذه الطبعة) ثم أكمل تحقيق الكتاب)\nالناشر: دار ابن الجوزي للنشر والتوزيع - السعودية\nالطبعة: الأولى، ١٤٣١ هـ\nعدد الأجزاء: ٧\n[ترقيم الكتاب موافق للمطبوع]"},"author": {"id": 3,"name": "ابن كثير","died": 774},"vol": "2","page": 22,"pageEnd": 22,"heading": ["تفسير سُورَةُ البَقَرَة","ذكر بناء قريش الكعبة بعد إبراهيم الخليل ﵇، بمدد طويلة وقبل مبعث رسول الله ﷺ بخمس سنين وقد نقل معهم في الحجارة، وله من العمر خمس وثلاثون سنةً:"],"ayahs": {"surah": 2,"from": 156,"to": 157},"text": "ثم بيَّن تعالى من الصابرين الذين شكرهم فقال: ﴿الَّذِينَ إِذَا أَصَابَتْهُمْ مُصِيبَةٌ قَالُوا إِنَّا لِلَّهِ وَإِنَّا إِلَيْهِ رَاجِعُونَ (١٥٦)﴾ أي: تسلَّوا بقولهم هذا عما أصابهم، وعلموا أنهم مُلك لله يتصرف في عبيده بما يشاء، وعلموا أنه لا يضيع لديه مثقال ذرة يوم القيامة، فأحدث لهم ذلك اعترافهم بأنهم عبيده وأنهم إليه راجعون في الدار الآخرة، ولهذا أخبر تعالى عما أعطاهم على ذلك فقال: ﴿أُولَئِكَ عَلَيْهِمْ صَلَوَاتٌ مِنْ رَبِّهِمْ وَرَحْمَةٌ﴾ أي: ثناء من الله عليهم ورحمة.\nقال سعيد بن جبير: أي: أمنة من العذاب.\n﴿وَأُولَئِكَ هُمُ الْمُهْتَدُونَ﴾ قال أمير المؤمنين عمر بن الخطاب: نِعم العدلان ونعمت العلاوة. ﴿أُولَئِكَ عَلَيْهِمْ صَلَوَاتٌ مِنْ رَبِّهِمْ وَرَحْمَةٌ﴾ فهذان العدلان، ﴿وَأُولَئِكَ هُمُ الْمُهْتَدُونَ﴾ فهذه العلاوة، وهي: ما توضع بين العدلين، وهي: زيادة في الحمل، فكذلك هؤلاء أعطوا ثوابهم وزيدوا أيضًا.","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}}]}

Its citation, as the API writes it:

ابن كثير (ت ٧٧٤هـ)، تفسير القرآن العظيم، ٢/ ٢٢

The citation object

citation is the reference, already written. Show citation.text as it is and the passage is cited. The other fields are its parts, for an app that lays a reference out itself.

FieldTypeWhat it holds
textstringThe full reference in Arabic, with Arabic-Indic digits.
authorstring or nullturath’s name for the author. null for the Qur’an, and when the name is blank.
diednumber or nullThe Hijri year of death. null when it is not known, or cannot be written truly.
diedLabelstring or nullنحو (about), بعد (after) or ق (sometime in that century), when turath marks the year so. null when the year is exact or not known.
bookstringThe title as the edition line gives it (its الكتاب: line), else book.name.
editionstring or nullThe edition line’s editor, publisher and printing lines, each as worded there, joined with ؛. Tanzil’s text and version for the Qur’an. null when the line names none.
vol, page, pageEndAs on the passage.
printedboolean or nulltrue when the page numbers are the printed edition’s. false when turath’s record says anything else, such as that turath numbered the book itself. null when turath does not say, and for the Qur’an.

How the reference is written

The reference is put together from turath’s own records: its names for the book and the author, the edition line, the volume label, the page numbers and the year of death in turath’s catalog. Nothing is reworded. Only what a reference adds is added: its punctuation, the marks ت, هـ and ص, Arabic-Indic digits and, where it applies, the numbering note. Its parts, joined with ،:

  1. The author, then the year of death after ت, in brackets: (ت ٧٧٤هـ). نحو and بعد are written before the year. ق writes the century, (ت ق ٤هـ), and died is then the century’s middle year, 350 for the fourth. A year that is not known, is below 1, or is marked in any other way is left out rather than written wrongly.
  2. The title, from the edition line. That is why the citation of 1503000934 says تفسير القرآن العظيم while book.name says تفسير ابن كثير - ط ابن الجوزي.
  3. The place. Volume/ page in a book of several volumes: ٢/ ٢٢. ص and the page in a book of one volume. The part’s name, then the page, in a named part. A passage over several pages gets a range: ٢/ ٢٢-٢٣. When pageEnd is not after page (the passage runs into the next volume, or the book’s numbers go backwards), the first page alone.
  4. The numbering. When printed is not true, (ترقيم النسخة الإلكترونية) follows the place: the numbers are those of turath’s electronic copy, and the reference claims no more than that.

Every form, from real passages

CasePassagecitation.text
A book of several volumes: volume/ page1503000934ابن كثير (ت ٧٧٤هـ)، تفسير القرآن العظيم، ٢/ ٢٢
A passage over two pages1503000935ابن كثير (ت ٧٧٤هـ)، تفسير القرآن العظيم، ٢/ ٢٢-٢٣
A book of one volume: the page alone42000186عبد الرحمن السعدي (ت ١٣٧٦هـ)، تيسير الكريم الرحمن في تفسير كلام المنان، ص ٧٥
A named part: its name, then the page36432000002محمد عبد اللطيف الخطيب (ت ١٤٠٢هـ)، أوضح التفاسير، المقدمة، ص ١١-١٢
The last page is not after the first (here page 380, pageEnd 7): the first page alone41002006البغوي، أبو محمد (ت ٥١٦هـ)، معالم التنزيل في تفسير القرآن = تفسير البغوي، ٣/ ٣٨٠
Page numbers that are not the printed edition’s (printed false)2000001000030قتادة بن دعامة السدوسي (ت ١١٧هـ)، تفسير قتادة، ص ٣٦ (ترقيم النسخة الإلكترونية)
A year of death turath marks as not exact (diedLabel نحو)30518000002أبو الفضائل ابن المظفر الرازي (ت نحو ٦٣٨هـ)، مباحث التفسير (وهو استدركات وتعليقات على تفسير الكشف والبيان للثعلبي)، ص ٦٧-٧٠
An ayah of the Qur’an (book 0)162القرآن الكريم، سورة البقرة، الآية ١٥٦

The short form

An Arabic reference is often written short: the book, then the volume and page. The library’s home page cites this passage as تفسير ابن كثير ٢/ ٢٢. The API sends the full form only. Before you write the short one yourself:

  • The API does not carry a book’s common nameتفسير ابن كثير is what readers call this book. The API gives the edition’s title in citation.book and turath’s name in book.name. For common names, keep your own table keyed by book.id.
  • Names are in ArabicThe API has no English names. The library home page’s English line, “Ibn Kathir · d. 774 AH”, names him in the page’s own words; only the year is the API’s, from author.died.
  • Keep the numbering markA short form without (ترقيم النسخة الإلكترونية) presents turath’s numbering as the printed edition’s.

A citation helper

This helper formats a citation from a passage’s fields: the full reference as the API sends it, the short form, the place in English for a line beside it, the ayahs, and the heading path. It follows the API’s rules for the place, with one difference. A passage does not carry its book’s list of volumes, so the helper cannot tell a book of one volume. For as-Sa’di it writes ١/ ٧٥ where citation.text writes ص ٧٥. Both point to the same page. Where the full form fits, show citation.text.

TypeScript
// The parts of a Passage this helper reads.export interface AyahRange {surah: number;from: number;to: number;} export interface Citation {text: string;author: string | null;died: number | null;diedLabel: string | null;book: string;edition: string | null;vol: string | null;page: number | null;pageEnd: number | null;printed: boolean | null;} export interface Passage {id: string;kind: "passage" | "ayah";heading: string[];ayahs: AyahRange | null;citation: Citation;} const DIGITS = "٠١٢٣٤٥٦٧٨٩";const ELECTRONIC = "ترقيم النسخة الإلكترونية"; /** Western digits as Arabic-Indic ones: 22 becomes ٢٢. */export function arabicDigits(value: number | string): string {return String(value).replace(/[0-9]/g, (digit) => DIGITS[Number(digit)]!);} /** A volume label that is a number ("2", "02", "٢") as that number; null for a named part. */function volumeNumber(label: string): number | null {const western = label.trim().replace(/[٠-٩]/g, (digit) => String(digit.charCodeAt(0) - 0x0660));return /^[0-9]+$/.test(western) ? Number(western) : null;} /** The last page, only when it is after the first: otherwise the passage is cited by its first page. */function lastPage(c: Citation): number | null {return c.page !== null && c.pageEnd !== null && c.pageEnd > c.page ? c.pageEnd : null;} /** The place in Arabic, as the API writes it, or null when the passage has no page. */export function arabicPlace(c: Citation): string | null {if (c.page === null) return null;const end = lastPage(c);const pages = end === null ? arabicDigits(c.page) : `${arabicDigits(c.page)}-${arabicDigits(end)}`;const label = c.vol?.trim() ?? "";const volume = label === "" ? null : volumeNumber(label);let place: string;if (label === "") place = `ص ${pages}`;else if (volume === null) place = `${arabicDigits(label)}، ص ${pages}`;else place = `${arabicDigits(volume)}/ ${pages}`;return c.printed === true ? place : `${place} (${ELECTRONIC})`;} /** The place in English, for a line beside the Arabic, or null when the passage has no page. */export function englishPlace(c: Citation): string | null {if (c.page === null) return null;const end = lastPage(c);const pages = end === null ? `p. ${c.page}` : `pp. ${c.page}–${end}`;const label = c.vol?.trim() ?? "";const volume = label === "" ? null : volumeNumber(label);let place: string;if (label === "") place = pages;else if (volume === null) place = `${label}, ${pages}`;else place = `vol. ${volume}, ${pages}`;return c.printed === true ? place : `${place} (numbering of the electronic copy)`;} /** The short form: the book, then the place. An ayah keeps the API's own text. */export function shortCitation(p: Passage): string {const c = p.citation;if (p.kind === "ayah") return c.text;const place = arabicPlace(c);if (place === null) return c.book;const named = c.vol !== null && c.vol.trim() !== "" && volumeNumber(c.vol) === null;return `${c.book}${named ? "، " : " "}${place}`;} export interface FormattedCitation {/** The full reference, exactly as the API sends it. */full: string;/** The book and the place. */short: string;/** The place in English, or null. */place: string | null;/** The ayahs the passage explains ("2:156–157"), or null. */ayahs: string | null;/** The headings it sits under, outermost first, on one line. */heading: string;} export function formatCitation(p: Passage): FormattedCitation {const a = p.ayahs;return {full: p.citation.text,short: shortCitation(p),place: p.kind === "ayah" ? null : englishPlace(p.citation),ayahs: a === null ? null : a.from === a.to ? `${a.surah}:${a.from}` : `${a.surah}:${a.from}–${a.to}`,heading: p.heading.join(" › "),};}

For passage 1503000934:

TypeScript
const cited = formatCitation(passage);
Response
{"full": "ابن كثير (ت ٧٧٤هـ)، تفسير القرآن العظيم، ٢/ ٢٢","short": "تفسير القرآن العظيم ٢/ ٢٢","place": "vol. 2, p. 22","ayahs": "2:156–157","heading": "تفسير سُورَةُ البَقَرَة › ذكر بناء قريش الكعبة بعد إبراهيم الخليل ﵇، بمدد طويلة وقبل مبعث رسول الله ﷺ بخمس سنين وقد نقل معهم في الحجارة، وله من العمر خمس وثلاثون سنةً:"}

For the other forms above:

Passageshortplace
1503000935تفسير القرآن العظيم ٢/ ٢٢-٢٣vol. 2, pp. 22–23
42000186تيسير الكريم الرحمن في تفسير كلام المنان ١/ ٧٥vol. 1, p. 75
36432000002أوضح التفاسير، المقدمة، ص ١١-١٢المقدمة, pp. 11–12
41002006معالم التنزيل في تفسير القرآن = تفسير البغوي ٣/ ٣٨٠vol. 3, p. 380
2000001000030تفسير قتادة ١/ ٣٦ (ترقيم النسخة الإلكترونية)vol. 1, p. 36 (numbering of the electronic copy)
30518000002مباحث التفسير (وهو استدركات وتعليقات على تفسير الكشف والبيان للثعلبي) ١/ ٦٧-٧٠vol. 1, pp. 67–70
162القرآن الكريم، سورة البقرة، الآية ١٥٦null

Set an Arabic reference in its own element marked as Arabic and right to left (in HTML, lang="ar" dir="rtl"). Beside English text, its digits, slash and brackets then keep their order.

Headings and ayahs

heading says where a passage sits in its book, outermost first. Each heading is as the page prints it, with its harakat, ﴿…﴾ and the editor’s [ ] kept, or the table of contents’ title where the heading has no line of its own.

A heading is not a title for the passage. Books differ in how often they set one. In this edition of Ibn Kathir, the heading over passage 1503000934 stands over 434 passages, from 2:127 to 2:238:

ذكر بناء قريش الكعبة بعد إبراهيم الخليل ﵇، بمدد طويلة وقبل مبعث رسول الله ﷺ بخمس سنين وقد نقل معهم في الحجارة، وله من العمر خمس وثلاثون سنةً:

To say what a passage explains, use the ayahs:

  • ayahs is read from the book’s own structure: ayah anchors, or headings. It is null for a book with no such structure, and for a passage under a surah heading before the surah’s first ayah.
  • quotes lists the ayahs its ﴿…﴾ quotes were matched to. Use it to show the ayahs themselves beside the passage. Some books do not mark quotes with ﴿…﴾: al-Qurtubi, ar-Razi, ash-Shawkani, Ibn ʿAtiyya and al-Qushayri among them have none, and their quotes is always empty.

Footnotes

notes holds the editor’s footnotes marked in the passage. Footnotes are the modern editor’s words, not the author’s, so the service stores them and serves them only when its SERVE_NOTES setting is "1". In the configuration as built it is "0", and notes is null in every answer. It is a setting of the service, not a parameter of a request.

When notes are served:

  • Each footnote starts with its number, as (٢) or [٢]. Footnotes are joined with "\n", and a footnote of several lines keeps its line breaks.
  • The markers are taken out of text, so a footnote cannot be put back at a point in the passage. Show notes after the passage, as the editor’s.
  • A footnote whose marker was not found, such as the end of one begun on the page before, goes with the passage that holds the page’s first line. A continued footnote has no label.

Passage 1503000934 has one footnote, labelled (٢): the editor’s source for the saying of Saʿid ibn Jubayr, whose marker followed it in turath’s text.

Show the text unchanged

A citation says that these words are on that page. It stays true only while the words you show are the words on the page. text is cleaned of markup and layout only: tags, entities (written out as the characters they stand for), page and footnote markers, runs of spaces, and separator lines such as * * *. Harakat, spellings, honorific signs and the editor’s brackets stay as printed.

  • Do not reword, correct or normalise it. The library keeps normalised copies of the text for search only, and never shows them.
  • Do not machine-translate it. Bayan never does. A translation would be someone else’s sentence, and the citation under it would name words the scholar did not write.
  • Paragraphs are on "\n". Each is a paragraph of the source. Where a printed page cut a paragraph in the middle of a sentence or a quote, the library has already joined it again. Split on "\n"; do not join or reflow.
  • Qur’an quotes are in ﴿…﴾, as the edition prints them, with its ayah numbers, such as (١٥٦). Set them apart from the commentary and keep what is inside as it is. In this edition the quote is in standard Arabic spelling, not the mushaf’s script. To show the ayah in the mushaf’s script, look it up by quotes or ayahs.
  • To show part of a passage, cut at a paragraph or a sentence, never inside ﴿…﴾, mark the cut with an ellipsis, and keep the citation with it.

This function splits a passage into paragraphs and marks its Qur’an quotes, without adding, dropping or moving a character. Joined back, the segments give the text exactly: we checked it on all 159,017 passages of the sample index. Some quotes are never closed in the source; a ﴿ with no ﴾ after it stays commentary.

TypeScript
export interface Segment {text: string;/** True for a Qur'an quote, ﴿ and ﴾ included. */quran: boolean;} /*** The passage's paragraphs, each cut into Qur'an quotes and commentary.* Nothing is added, dropped or reordered: joining every segment, with "\n"* between paragraphs, gives back the text. A ﴿ with no ﴾ after it stays* commentary; a quote that runs over a paragraph break stays a quote.*/export function paragraphs(text: string): Segment[][] {const out: Segment[][] = [[]];const push = (part: string, quran: boolean) => {part.split("\n").forEach((piece, i) => {if (i > 0) out.push([]);if (piece !== "") out[out.length - 1]!.push({ text: piece, quran });});};let at = 0;for (const match of text.matchAll(/﴿[^﴾]*﴾/g)) {const start = match.index ?? 0;push(text.slice(at, start), false);push(match[0], true);at = start + match[0].length;}push(text.slice(at), false);return out;}

Passage 1503000934 gives three paragraphs. The first opens with commentary, then the quote of 2:156; the third opens with a quote.

source says where a reader can check the passage.

FieldTypeWhat it holds
readerstring or nullturath’s reader at the passage. Its page parameter is a position in turath’s pages, not the printed page: for 1503000934 it is https://app.turath.io/book/1503?page=727, printed page 22. Quran.com at the ayah for the Qur’an. null for a passage with no position.
pdfobject or nullturath’s scan of the printed page: { url, page }. page is the printed page to open it at; null when the page numbers are not the printed edition’s, and the file then opens at its start. pdf is null when turath has no scan Bayan links to, as for at-Tabari.
providerstring"turath.io", or "Tanzil Project" for an ayah.
attributionstring or nullnull for a scholar’s passage. For an ayah, Tanzil’s credit, link and license.

The printed page

GET /v1/passages/:id/source answers { passage, page }: the passage, and every passage of the same book that shares a page of turath’s text with it, in book order, itself included. At most 20, the nearest. Use it to show a passage in its page, beside its citation. An ayah’s page is the ayah alone.

The route takes no parameters; an unknown one is a 400 invalid_request, and an id that does not exist is a 404 not_found. Answers carry Cache-Control: public, max-age=3600, s-maxage=86400.

curl
curl -s https://grep.getbayan.app/v1/passages/1503000934/source \| jq '{citation: .passage.citation.text, page: [.page[] | {id, vol, page, pageEnd}]}'
Response
{"citation": "ابن كثير (ت ٧٧٤هـ)، تفسير القرآن العظيم، ٢/ ٢٢","page": [{"id": "1503000933","vol": "2","page": 21,"pageEnd": 22},{"id": "1503000934","vol": "2","page": 22,"pageEnd": 22},{"id": "1503000935","vol": "2","page": 22,"pageEnd": 23}]}

Passage 1503000933 runs from page 21 onto page 22, and 1503000935 from page 22 onto page 23. Both share turath’s page with 1503000934.

Citing an ayah

The Qur’an is book 0 of the library, one passage per ayah, so a search can return an ayah itself, with kind "ayah". An ayah’s id is its place in the Qur’an, counting from 0: 1:1 is 0, and 2:156 is 162. Its citation names the surah and the ayah. It has no author, volume or page.

Its text is Tanzil Project’s, word for word. Tanzil’s terms ask that every use names Tanzil Project with a link to tanzil.net, and allow verbatim copies only. source.attribution is that credit: show it with the ayah. GET /v1/books/0 carries Tanzil’s copyright block in notice.

curl
curl -s "https://grep.getbayan.app/v1/passages/162/context?before=0&after=0" \| jq '.passages[0] | {id, kind, ayahs, citation, source}'
Response
{"id": "162","kind": "ayah","ayahs": {"surah": 2,"from": 156,"to": 156},"citation": {"text": "القرآن الكريم، سورة البقرة، الآية ١٥٦","author": null,"died": null,"diedLabel": null,"book": "القرآن الكريم","edition": "Tanzil Quran Text (Uthmani, Version 1.1) — Tanzil Project, https://tanzil.net","vol": null,"page": null,"pageEnd": null,"printed": null},"source": {"reader": "https://quran.com/2/156","pdf": null,"provider": "Tanzil Project","attribution": "Tanzil Quran Text (Uthmani, Version 1.1), Tanzil Project (https://tanzil.net). License: Creative Commons Attribution 3.0. Reproduced verbatim."}}

If a model is in your app

There is no answer for a model to write: the passages are the answer. If your app lets a model choose among them, have it return passage ids, and show each chosen passage’s own text and citation. Agentic search works this way: the model writes search terms and a verdict, and the answer is the passages. A sentence a model writes under a scholar’s name is a sentence the scholar never wrote, and no citation can make it theirs.

Checklist

  • Show citation.text, or a form built from its fields, with every passage.
  • Keep (ترقيم النسخة الإلكترونية) whenever printed is not true.
  • Show text as sent: paragraphs on "\n", ﴿…﴾ set apart, no word changed.
  • Use ayahs, not heading, to say what a passage explains.
  • Show notes, when served, after the passage and as the editor’s.
  • Link source.reader or source.pdf, so a reader can check the page.
  • Show source.attribution with every ayah of the Qur’an.