Skip to content

Search and Retrieval

Surrounding context

A search result is one passage of a longer discussion. GET /v1/passages/:id/context returns that passage with the passages just before and after it in the same book, in reading order, so you can read it in its place.

The library cuts each book into passages of at most 1,200 letters, harakat not counted. A few run longer to keep a Qur’an quote whole. A passage never crosses a heading and never runs from one ayah’s commentary into the next. So a scholar’s discussion of an ayah is often several passages long, and a search returns only the one that matched best. This call returns the text around it.

Request

curl
curl "https://grep.getbayan.app/v1/passages/2547000167/context?before=1&after=1"

Path parameter

ParameterTypeDescription
idwhole numberA passage id, as any answer gives it in id. Written as a plain decimal number: no sign, no leading zeros, no decimal point. 0 is a valid id: the Qur’an’s first ayah.

Query parameters

ParameterRangeDefaultDescription
before0 to 52How many passages before the passage to return.
after0 to 52How many passages after the passage to return.

Each parameter may be given once, as a whole number. Any other parameter is refused with 400, so a mistake such as ?around=2 fails at once instead of quietly returning the defaults.

The call takes no body and needs no key. It counts against the read limit that every GET call shares: 300 requests a minute per address, counted at each Cloudflare location, where an IPv6 address counts as its /64. Answers served from Cloudflare’s cache do not count (see Caching). The API sends no CORS headers, so call it from a server or a native app, not from a web page’s script.

How passage ids work

A passage id is its book’s id times 1,000,000, plus the passage’s position in that book:

id = book × 1,000,000 + position

Positions start at 0 and follow the book’s reading order, one per passage, with no gaps. The book and the position can be read straight off the id:

TypeScript
const id = Number("2547000167");const book = Math.floor(id / 1_000_000); // 2547: al-Wajizconst position = id % 1_000_000;         // 167: the 168th passage of the book // The Qur'an is book 0, one passage per ayah: id = the ayah's place in the whole Qur'an - 1.// 1:1 is 0, 1:7 is 6, 2:1 is 7, 2:255 is 261, 114:6 is 6235.
IdBookPositionThe passage
25470001672547, الوجيز للواحدي167al-Wahidi’s gloss on 2:156
15030009351503, تفسير ابن كثير - ط ابن الجوزي935Part of Ibn Kathir’s commentary on 2:156–157
00, the Qur’an0Ayah 1:1
2610, the Qur’an261Ayah 2:255
  • Strings in answersEvery answer gives a passage’s id as a string, such as "2547000167". In the path, write the same digits.
  • The Qur’an is book 0It has one passage per ayah, and an ayah’s id is its place in the whole Qur’an minus 1, so the ids run from 0 to 6,235. Its passages have kind "ayah" and no author.
  • A book’s ids are one blockThey run from book × 1,000,000 to book × 1,000,000 + passages − 1, where passages is the count that GET /v1/books/:id gives.
  • Only the author’s pages have positionsPages turath marks as not the author’s, and an editor’s front and back matter such as an introduction or indexes, are left out when a book is cut. Context never returns them.
  • Short passages do have positionsA few words under a sub-heading are kept for reading in place but left out of search. Search never returns them; context does.

Why the neighbours are a range

Because the position is part of the id, the passages around a passage are the ids just below and above it. The call reads one range of ids, held inside the passage’s own book:

TypeScript
// The ids one call reads: never outside the passage's own book.const book = Math.floor(id / 1_000_000);const from = Math.max(book * 1_000_000, id - before);const to = Math.min(book * 1_000_000 + 999_999, id + after);
  • One queryThe passages are one range of the table’s primary key in D1, read in one indexed query. No second index is needed, and the vector index is not involved.
  • Never another bookAt the start or end of a book you get fewer passages than you asked for. before=5 on a book’s first passage returns nothing before it, and the next book’s first passage is never returned as context.
  • By position, not by topicThe neighbours are the next passages in the book, whatever they are about. They can cross a heading, an ayah or a surah. The default context of 1503000935, Ibn Kathir on 2:156–157, reaches back into 2:155. The context of id 6 (1:7, the last ayah of al-Fatiha) with before=1&after=1 is 1:6, 1:7 and 2:1.

Response

200, with one field, passages: the passage and its neighbours in book order (ascending id), the passage itself included. That is at most before + 1 + after passages, 11 at the most.

Find the passage you asked for by its id, not by its index. Near the start of a book there are fewer passages before it than you asked for.

Each item is the same Passage object that search and the ayah lookup return, without score and rerankScore, which only search sets.

FieldTypeDescription
idstringbook × 1,000,000 + position.
kind"passage" or "ayah""ayah" for the Qur’an’s own ayahs (book 0); "passage" for a scholar’s words.
bookobjectid, name, and edition: turath’s edition line, one fact to a line (title, author, editor, publisher, printing and so on), or Tanzil’s text and version for the Qur’an. edition may be null.
authorobject or nullid, name, died (hijri year, or null when not known), and diedLabel only when turath marks the year as not exact. null for the Qur’an.
vol, page, pageEndstring, number, number; each may be nullThe volume, and the pages the passage starts and ends on. citation.printed says whether these are the printed edition’s page numbers.
headingstring[]The headings the passage sits under, outermost first.
ayahsobject or null{ surah, from, to } when the book places the passage on ayahs.
textstringThe scholar’s words, cleaned of markup, footnote markers and page markers, the words themselves unchanged; for an ayah, Tanzil’s Uthmani text. Paragraphs are separated by "\n"; Qur’an quotes are in ﴿…﴾.
notesstring or nullThe editor’s footnotes. They are the modern editor’s words, and the service is set not to serve them, so this is null.
quotesobject[]The ayahs the passage’s ﴿…﴾ quotes were matched to, as { surah, from, to }, in the Qur’an’s order. Empty for an ayah.
citationobjectThe reference in Arabic, text, with the parts it is built from: author, died, diedLabel, book, edition, vol, page, pageEnd and printed (whether the page numbers are the printed edition’s; null when turath does not say, and for the Qur’an). See Citations.
sourceobjectreader: turath’s reader at the passage, or Quran.com at the ayah, or null. In turath’s reader, the page parameter is turath’s own position, not the printed page. pdf: turath’s scan, { url, page }, or null when there is none to link to; page is the printed page to open it at, or null when the page numbers are not the printed edition’s (the scan then opens at its start). provider: "turath.io" or "Tanzil Project". attribution: Tanzil’s credit for an ayah, otherwise null.

Example response

The answer to the request above: al-Wahidi’s glosses on 2:155, 2:156 and 2:157, all on page 140. It is the live API’s answer, on 28 November 2026.

Response
{"passages": [{"id": "2547000166","kind": "passage","book": {"id": 2547,"name": "الوجيز للواحدي","edition": "الكتاب: الوجيز في تفسير الكتاب العزيز\nالمؤلف: أبو الحسن علي بن أحمد بن محمد بن علي الواحدي، النيسابوري، الشافعي (ت ٤٦٨هـ)\nتحقيق: صفوان عدنان داوودي\nدار النشر: دار القلم، الدار الشامية - دمشق، بيروت\nالطبعة: الأولى، ١٤١٥ هـ\nعدد الصفحات: ١٢٤٣\n[ترقيم الكتاب موافق للمطبوع]"},"author": { "id": 1, "name": "الواحدي", "died": 468 },"vol": "1","page": 140,"pageEnd": 140,"heading": ["سورة البقرة", "١٥٥"],"ayahs": { "surah": 2, "from": 155, "to": 155 },"text": "﴿ولنبلونكم﴾ ولنعاملنَّكم مُعاملة المبتلي ﴿بشيء من الخوف﴾ يعني: خوف العدوِّ ﴿والجوع﴾ يعني: القحط ﴿وَنَقْصٍ مِنَ الأَمْوَالِ﴾ يعني: الخسران والنُّقصان فِي المال وهلاك المواشي ﴿والأنفس﴾ يعني: الموت والقتل في الجهاد والمرض والشَّيب ﴿والثمرات﴾ يعني: الجوائح وموت الأولاد فمَنْ صبر على هذه الأشياء استحقَّ الثَّواب ومَنْ لم يصبر لم يستحق يدلُّ على هذا قوله تعالى: ﴿وبشر الصابرين﴾","notes": null,"quotes": [{ "surah": 2, "from": 155, "to": 155 }],"citation": {"text": "الواحدي (ت ٤٦٨هـ)، الوجيز في تفسير الكتاب العزيز، ص ١٤٠","author": "الواحدي","died": 468,"diedLabel": null,"book": "الوجيز في تفسير الكتاب العزيز","edition": "تحقيق: صفوان عدنان داوودي؛ دار النشر: دار القلم، الدار الشامية - دمشق، بيروت؛ الطبعة: الأولى، ١٤١٥ هـ","vol": "1","page": 140,"pageEnd": 140,"printed": true},"source": {"reader": "https://app.turath.io/book/2547?page=163","pdf": {"url": "https://files.turath.io/pdf/%D8%A7%D9%84%D8%AA%D9%81%D8%A7%D8%B3%D9%8A%D8%B1/%D8%A7%D9%84%D9%88%D8%AC%D9%8A%D8%B2%20%D9%81%D9%8A%20%D8%AA%D9%81%D8%B3%D9%8A%D8%B1%20%D8%A7%D9%84%D9%83%D8%AA%D8%A7%D8%A8%20%D8%A7%D9%84%D8%B9%D8%B2%D9%8A%D8%B2%20-%20%D8%A7%D9%84%D9%88%D8%A7%D8%AD%D8%AF%D9%8A%20-%20%D8%AA%20%D8%A7%D9%84%D8%AF%D8%A7%D9%88%D9%88%D8%AF%D9%8A%20-%20%D8%B7%20%D8%A7%D9%84%D9%82%D9%84%D9%85%2001-02/18544.pdf","page": 140},"provider": "turath.io","attribution": null}},{"id": "2547000167","kind": "passage","book": {"id": 2547,"name": "الوجيز للواحدي","edition": "الكتاب: الوجيز في تفسير الكتاب العزيز\nالمؤلف: أبو الحسن علي بن أحمد بن محمد بن علي الواحدي، النيسابوري، الشافعي (ت ٤٦٨هـ)\nتحقيق: صفوان عدنان داوودي\nدار النشر: دار القلم، الدار الشامية - دمشق، بيروت\nالطبعة: الأولى، ١٤١٥ هـ\nعدد الصفحات: ١٢٤٣\n[ترقيم الكتاب موافق للمطبوع]"},"author": { "id": 1, "name": "الواحدي", "died": 468 },"vol": "1","page": 140,"pageEnd": 140,"heading": ["سورة البقرة", "١٥٦"],"ayahs": { "surah": 2, "from": 156, "to": 156 },"text": "﴿الذين إذا أصابتهم مصيبة﴾ ممَّا ذُكر ﴿قالوا إنا لله وإنا إليه راجعون﴾ أَيْ: أموالنا لله ونحن عبيدة يصنع بنا ما يشاء ثمَّ وعدهم على هذا القول المغفرة","notes": null,"quotes": [{ "surah": 2, "from": 156, "to": 156 }],"citation": {"text": "الواحدي (ت ٤٦٨هـ)، الوجيز في تفسير الكتاب العزيز، ص ١٤٠","author": "الواحدي","died": 468,"diedLabel": null,"book": "الوجيز في تفسير الكتاب العزيز","edition": "تحقيق: صفوان عدنان داوودي؛ دار النشر: دار القلم، الدار الشامية - دمشق، بيروت؛ الطبعة: الأولى، ١٤١٥ هـ","vol": "1","page": 140,"pageEnd": 140,"printed": true},"source": {"reader": "https://app.turath.io/book/2547?page=164","pdf": {"url": "https://files.turath.io/pdf/%D8%A7%D9%84%D8%AA%D9%81%D8%A7%D8%B3%D9%8A%D8%B1/%D8%A7%D9%84%D9%88%D8%AC%D9%8A%D8%B2%20%D9%81%D9%8A%20%D8%AA%D9%81%D8%B3%D9%8A%D8%B1%20%D8%A7%D9%84%D9%83%D8%AA%D8%A7%D8%A8%20%D8%A7%D9%84%D8%B9%D8%B2%D9%8A%D8%B2%20-%20%D8%A7%D9%84%D9%88%D8%A7%D8%AD%D8%AF%D9%8A%20-%20%D8%AA%20%D8%A7%D9%84%D8%AF%D8%A7%D9%88%D9%88%D8%AF%D9%8A%20-%20%D8%B7%20%D8%A7%D9%84%D9%82%D9%84%D9%85%2001-02/18544.pdf","page": 140},"provider": "turath.io","attribution": null}},{"id": "2547000168","kind": "passage","book": {"id": 2547,"name": "الوجيز للواحدي","edition": "الكتاب: الوجيز في تفسير الكتاب العزيز\nالمؤلف: أبو الحسن علي بن أحمد بن محمد بن علي الواحدي، النيسابوري، الشافعي (ت ٤٦٨هـ)\nتحقيق: صفوان عدنان داوودي\nدار النشر: دار القلم، الدار الشامية - دمشق، بيروت\nالطبعة: الأولى، ١٤١٥ هـ\nعدد الصفحات: ١٢٤٣\n[ترقيم الكتاب موافق للمطبوع]"},"author": { "id": 1, "name": "الواحدي", "died": 468 },"vol": "1","page": 140,"pageEnd": 140,"heading": ["سورة البقرة", "١٥٧"],"ayahs": { "surah": 2, "from": 157, "to": 157 },"text": "﴿أولئك عليهم صلوات من ربهم﴾ أَيْ: مغفرةٌ ﴿ورحمة﴾ ونعمةٌ ﴿وأولئك هم المهتدون﴾ إلى الجنَّة والثَّواب والحقِّ والصَّواب وقيل: زيادة الهدى وقيل: هم المنتفعون بالهداية","notes": null,"quotes": [{ "surah": 2, "from": 157, "to": 157 }],"citation": {"text": "الواحدي (ت ٤٦٨هـ)، الوجيز في تفسير الكتاب العزيز، ص ١٤٠","author": "الواحدي","died": 468,"diedLabel": null,"book": "الوجيز في تفسير الكتاب العزيز","edition": "تحقيق: صفوان عدنان داوودي؛ دار النشر: دار القلم، الدار الشامية - دمشق، بيروت؛ الطبعة: الأولى، ١٤١٥ هـ","vol": "1","page": 140,"pageEnd": 140,"printed": true},"source": {"reader": "https://app.turath.io/book/2547?page=165","pdf": {"url": "https://files.turath.io/pdf/%D8%A7%D9%84%D8%AA%D9%81%D8%A7%D8%B3%D9%8A%D8%B1/%D8%A7%D9%84%D9%88%D8%AC%D9%8A%D8%B2%20%D9%81%D9%8A%20%D8%AA%D9%81%D8%B3%D9%8A%D8%B1%20%D8%A7%D9%84%D9%83%D8%AA%D8%A7%D8%A8%20%D8%A7%D9%84%D8%B9%D8%B2%D9%8A%D8%B2%20-%20%D8%A7%D9%84%D9%88%D8%A7%D8%AD%D8%AF%D9%8A%20-%20%D8%AA%20%D8%A7%D9%84%D8%AF%D8%A7%D9%88%D9%88%D8%AF%D9%8A%20-%20%D8%B7%20%D8%A7%D9%84%D9%82%D9%84%D9%85%2001-02/18544.pdf","page": 140},"provider": "turath.io","attribution": null}}]}

Errors

Errors have the same envelope as every other call. Branch on code; the message is for people.

Response
{ "error": { "code": "not_found", "message": "There is no passage with this id." } }
StatusCodeWhen
400invalid_requestThe id is not a plain whole number (x, 00, -5, a decimal, or one too large to be exact). before or after is not a whole number from 0 to 5. A parameter is given twice. A parameter other than before and after is given. The message names the field, for example "before must be a whole number from 0 to 5." or "Unknown parameter. Allowed: before, after."
404not_foundNo passage has this id, even when its neighbours exist. al-Wajiz’s last passage is 2547006169; 2547006170 is a 404.
405method_not_allowedAny method but GET. The answer carries Allow: GET. A HEAD for an answer Cloudflare’s cache already holds is answered from the cache, which keeps one entry for GET and HEAD.
429rate_limitedOver the read limit. Wait the Retry-After seconds (60) and try again.
500internalSomething failed on our side. Try again later.

Caching

A context answer is the same for everyone, so it is sent with Cache-Control: public, max-age=3600, s-maxage=86400.

  • A day at the edgeCloudflare keeps the answer for up to a day and serves repeats from its cache without running the Worker. Those answers do not count against the rate limit.
  • An hour in your clientYour HTTP client or app may keep it for an hour.
  • Purged on a re-loadWhen a book is loaded again, the edge cache is purged. Nothing can purge a client’s copy, so it can be up to an hour old.
  • Errors are never cachedThey carry Cache-Control: no-store.

Example: read a search result in its place

Search, take a result, and ask for one passage either side of it.

TypeScript
const BASE = "https://grep.getbayan.app"; // 1. Search.const search = await fetch(`${BASE}/v1/search`, {method: "POST",headers: { "Content-Type": "application/json" },body: JSON.stringify({ query: "الصبر عند المصيبة", topK: 5 }),}).then((response) => response.json());const hit = search.results[0]; // 2. Ask for the result with one passage either side of it.const response = await fetch(`${BASE}/v1/passages/${hit.id}/context?before=1&after=1`);if (!response.ok) throw new Error((await response.json()).error.message);const { passages } = await response.json(); // 3. Read them in book order. Find the result by its id, not by its index.for (const passage of passages) {console.log(passage.id === hit.id ? "*" : " ", passage.citation.text);console.log(passage.text);}

If the result is 2547000167, al-Wahidi’s gloss on 2:156, the answer is the one under Example response: his glosses on 2:155, 2:156 and 2:157. All three are on the same page and carry the same citation:

الواحدي (ت ٤٦٨هـ)، الوجيز في تفسير الكتاب العزيز، ص ١٤٠

Reading further

Five passages a side is the most one call returns, and a long discussion runs further. In Ibn Kathir (book 1503), the default context of 1503000935 is these five passages:

IdAyahsVolume and pages
15030009332:1552, pp. 21–22
15030009342:156–1572, p. 22
1503000935 (asked for)2:156–1572, pp. 22–23
15030009362:156–1572, p. 23
15030009372:156–1572, pp. 23–24

His commentary on 2:156–157 is four passages, 1503000934 to 1503000937. The context reaches back into 2:155 because neighbours go by position. His commentary on 2:255 is 22 passages, 1503001354 to 1503001375. To read on, call again from the last passage you have, with before=0, and stop when the passages leave the ayah or the book ends:

TypeScript
const BASE = "https://grep.getbayan.app"; type Passage = {id: string;ayahs: { surah: number; from: number; to: number } | null;text: string;citation: { text: string };}; async function context(id: string, before: number, after: number): Promise<Passage[]> {const response = await fetch(`${BASE}/v1/passages/${id}/context?before=${before}&after=${after}`);if (!response.ok) throw new Error((await response.json()).error.message);return (await response.json()).passages;} const onAyah = (passage: Passage, surah: number, ayah: number) =>passage.ayahs !== null && passage.ayahs.surah === surah && passage.ayahs.from <= ayah && passage.ayahs.to >= ayah; /** The passages after `id`, five per call, for as long as they are on the ayah. */async function readOn(id: string, surah: number, ayah: number): Promise<Passage[]> {const read: Passage[] = [];for (;;) {const next = (await context(id, 0, 5)).slice(1); // the first passage is `id` itselffor (const passage of next) {if (!onAyah(passage, surah, ayah)) return read;read.push(passage);}if (next.length < 5) return read; // the book endedid = next[next.length - 1].id;}} // Ibn Kathir on 2:255, from its first passage on.const rest = await readOn("1503001354", 2, 255);

Started from 1503001354, this reads the 21 passages after it, 1503001355 to 1503001375, in five calls, and stops at 1503001376, which is on 2:256.

Three calls read passages in their book. They answer different questions.

CallReturnsUse it to
GET /v1/passages/:id/contextThe passage and up to 5 passages either side, by position in the book. Works on any passage of any book, the Qur’an included.Read a result in its place.
GET /v1/passages/:id/sourceThe passages that share a page of turath’s text with the passage, itself included, at most 20. Takes no parameters.Show a passage on its page, beside its citation and its scan.
GET /v1/tafsir/:surah/:ayahThe passages the chosen tafsirs have on one ayah, paged with after. Only books whose ayah mapping is ready answer.Read everything a tafsir says on an ayah.