# Read a search (/flights/read-a-search)

Wait for a partial search to finish, then sort, filter, page or read one result in full, all without starting another search.

Every search has a `search_id`. Read it again with `GET /v1/flights/search/{search_id}` to get the rest of a partial search, to sort, filter or page it differently, or to read one result in full. Reading a search never starts a supplier search, and it counts only toward your key's requests, not its new trips.

## What a read takes [#what-a-read-takes]

A read takes only the fields that shape the answer. The trip is already fixed by the search.

<TypeTable type={{"wait_ms":{"type":"integer","description":"0 to 30,000. Wait up to this long for a search that is still running. Left out, the read answers at once with what there is."},"sort":{"type":"string","description":"best, price or duration.","default":"best"},"max_stops":{"type":"integer","description":"0 to 3 stops in each direction."},"airlines":{"type":"string","description":"2-character airline codes, comma separated. Keeps itineraries sold entirely by these airlines."},"limit":{"type":"integer","description":"Results in this response, 1 to 50.","default":"10"},"cursor":{"type":"string","description":"The next_cursor of the previous page."},"itinerary_id":{"type":"string","description":"A result's id, exactly as an answer gave it. Keeps only that result."},"detail":{"type":"string","description":"summary or full.","default":"summary"},"fares":{"type":"integer","description":"Fares per result, 1 to 10. Read only when detail is full.","default":"3"}}} />

A key reads only searches made in its own mode: a test key cannot read a live key's search, and a live key cannot read a test key's.

## Wait for a partial search [#wait-for-a-partial-search]

While a search runs, its answers have `status` `partial` and a `retry_after_ms` hint: how many milliseconds to wait before you ask again. This is part of a real partial answer:

```json title="Partial answer (excerpt)"
{
  "search_id": "srch_2795d01a-c7bf-4851-afd8-438ab774b547",
  "status": "partial",
  "retry_after_ms": 2000,
  "counts": { "found": 2, "matched": 2, "returned": 2 },
  "next_cursor": null
}
```

There are two ways to wait for the rest:

- **Let the read wait.** Send `wait_ms`, up to 30,000. The read answers as soon as the search is complete, or when the wait runs out with what has arrived. This is the simplest loop, and the one below.
- **Read at once, then pause.** Leave `wait_ms` out, and wait `retry_after_ms` before each read.

Read with a `wait_ms` of 10,000, the same search answered complete with all six itineraries:

```json title="The same search, read again (excerpt)"
{
  "search_id": "srch_2795d01a-c7bf-4851-afd8-438ab774b547",
  "status": "complete",
  "counts": { "found": 6, "matched": 6, "returned": 2 },
  "next_cursor": "c_eyJvIjoyLCJzIjoiMjc5NWQwMWEtYzdiZi00ODUxLWFmZDgtNDM4YWI3NzRiNTQ3IiwidiI6ImJlc3R8fCJ9"
}
```

While a search is partial, you can show its results as "so far": they are real fares. Its `counts` and `airlines` are also "so far". Most itineraries arrive within the first 5 to 15 seconds, and a large route can take about 30 seconds to complete. The supplier is given 45 seconds; a search still running after that ends with `supplier_timeout`.

A test key replays its saved sample in well under a second, so its searches are usually complete before you read them. To see a partial answer with a test key, search with `response_mode=partial&wait_ms=0`.

## A polling loop [#a-polling-loop]

This loop starts a search, then reads it until it is complete. It stops on the first error, and after a minute of its own.

<Tabs items={["curl","JavaScript","Python"]}>
<Tab value="curl">
```bash title="poll.sh"
# Needs jq. SEARCH_ID is data.search_id from your search.
BASE="https://api.voyagelabs.ai"
SEARCH_ID="srch_2795d01a-c7bf-4851-afd8-438ab774b547"

# Up to six reads, each waiting up to 10 seconds.
for attempt in 1 2 3 4 5 6; do
  body=$(curl -sS "$BASE/v1/flights/search/$SEARCH_ID?wait_ms=10000" \
    -H "X-API-Key: $VOYAGE_LABS_API_KEY")
  status=$(printf '%s' "$body" | jq -r '.data.status // .error.code')
  echo "$status"
  [ "$status" = "partial" ] || break
done
```
</Tab>

<Tab value="JavaScript">
```js title="poll.mjs"
const BASE = "https://api.voyagelabs.ai";
const headers = { "X-API-Key": process.env.VOYAGE_LABS_API_KEY };

async function get(path) {
  const response = await fetch(`${BASE}${path}`, { headers });
  return response.json();
}

// Start the search, and take what has arrived within 8 seconds.
const trip = new URLSearchParams({
  origin: "SFO",
  destination: "NRT",
  depart_date: "2027-02-09",
  return_date: "2027-02-16",
  adults: "2",
  response_mode: "partial",
});
let body = await get(`/v1/flights/search?${trip}`);

// Read the same search until it is complete, fails, or a minute has passed.
const deadline = Date.now() + 60_000;
while (body.data?.status === "partial" && Date.now() < deadline) {
  const id = encodeURIComponent(body.data.search_id);
  body = await get(`/v1/flights/search/${id}?wait_ms=10000`);
}

if (body.error) {
  // supplier_timeout, supplier_error or search_not_found: search the trip again.
  console.error(body.error.code, body.error.message);
} else {
  // complete, or still partial at the deadline: show what there is.
  console.log(body.data.status, body.data.counts.found, "itineraries");
}
```
</Tab>

<Tab value="Python">
```python title="poll.py"
import os
import time

import requests

BASE = "https://api.voyagelabs.ai"
HEADERS = {"X-API-Key": os.environ["VOYAGE_LABS_API_KEY"]}

def get(path, params):
    response = requests.get(BASE + path, params=params, headers=HEADERS, timeout=40)
    return response.json()

# Start the search, and take what has arrived within 8 seconds.
body = get(
    "/v1/flights/search",
    {
        "origin": "SFO",
        "destination": "NRT",
        "depart_date": "2027-02-09",
        "return_date": "2027-02-16",
        "adults": 2,
        "response_mode": "partial",
    },
)

# Read the same search until it is complete, fails, or a minute has passed.
deadline = time.monotonic() + 60
while "data" in body and body["data"]["status"] == "partial" and time.monotonic() < deadline:
    search_id = body["data"]["search_id"]
    body = get(f"/v1/flights/search/{search_id}", {"wait_ms": 10000})

if "error" in body:
    # supplier_timeout, supplier_error or search_not_found: search the trip again.
    print(body["error"]["code"], body["error"]["message"])
else:
    # complete, or still partial at the deadline: show what there is.
    print(body["data"]["status"], body["data"]["counts"]["found"], "itineraries")
```
</Tab>
</Tabs>

### When to stop [#when-to-stop]

| The read answers | Do this |
| --- | --- |
| `status` `complete` | Stop. The list will not change. |
| `supplier_timeout` or `supplier_error` | Stop. That search is over, and reading it again gives the same error. Search the trip again. |
| `search_not_found` | Stop. The search is no longer kept. Search the trip again. |
| `search_capacity` | Wait `error.retry_after_s` seconds, then search the trip again. |
| `rate_limited` | Wait `error.retry_after_s` seconds, then read again. |
| `status` `partial` past your own deadline | Stop, and show what you have as "so far". |

<Callout type="warn" title="Don't search again while a search runs">
Calling `GET /v1/flights/search` again for the same trip answers from the same running search, but it counts toward your new-trips limit each time. Read the `search_id` instead.
</Callout>

## Sort, filter and page [#sort-filter-and-page]

Once a search is complete, read it with any `sort`, `max_stops`, `airlines` and `limit`. Each read reshapes the same stored itineraries, so you can offer "cheapest", "fastest" and "nonstop only" views without searching again.

```bash title="Two pages, cheapest first"
curl -sS "https://api.voyagelabs.ai/v1/flights/search/srch_2795d01a-c7bf-4851-afd8-438ab774b547?sort=price&limit=2" \
  -H "X-API-Key: $VOYAGE_LABS_API_KEY"

# Then pass that answer's data.next_cursor as cursor, with the same sort.
curl -sS "https://api.voyagelabs.ai/v1/flights/search/srch_2795d01a-c7bf-4851-afd8-438ab774b547?sort=price&limit=2&cursor=c_eyJvIjoyLCJzIjoiMjc5NWQwMWEtYzdiZi00ODUxLWFmZDgtNDM4YWI3NzRiNTQ3IiwidiI6InByaWNlfHwifQ" \
  -H "X-API-Key: $VOYAGE_LABS_API_KEY"
```

- **A cursor belongs to one search, sort and set of filters.** Send it with the same `sort`, `max_stops` and `airlines` as the page it came from. Sent with any others, it is refused with `invalid_parameter` on `cursor`.
- **`next_cursor` is `null` on the last page.**
- **Page only a complete search.** While a search is partial the list is still changing: a page can repeat or miss itineraries, and `next_cursor` can be `null` although more are coming.

## Read one result in full [#read-one-result-in-full]

To show the itinerary a traveller picked, read it with `itinerary_id` set to that result's `id`, exactly as given, and `detail=full`. The answer holds just that result, wherever it would sort or page, with every flight segment, layover and up to `fares` fares (3 unless set, 10 at most).

```bash
curl -sS "https://api.voyagelabs.ai/v1/flights/search/srch_2795d01a-c7bf-4851-afd8-438ab774b547?itinerary_id=it_U0ZPLk5SVC4yMDI3MDIwOS4yMDI3MDIxNi4yLjAuMC5ZLlVTRHxaRzI1QDAyMDksWkcyNkAwMjE2&detail=full&fares=3" \
  -H "X-API-Key: $VOYAGE_LABS_API_KEY"
```

- **Send no `cursor` with it.** One result is not a page, so `cursor` is refused. `max_stops` and `airlines` still apply, so leave them out too.
- **An `id` the search does not hold answers `200` with no results**, not an error. Search the trip again and read the new `search_id` with the same `itinerary_id`, waiting while it is partial: the same flights on the same trip always get the same `id`. Only an empty answer from a complete search means those flights are no longer offered.

## How long a search is kept [#how-long-a-search-is-kept]

A search is kept for at least 30 minutes. After that, reading it answers `404` with `search_not_found`: search the trip again.

Reading a search never refreshes its prices: its `pricing.age_s` keeps growing. Before you show a price or a booking link that is more than 240 seconds old, search the trip again and read the chosen result from the new search by its `itinerary_id`.

<Cards>
<Card title="Send travellers to book" href="/flights/booking-links" description="What each booking link opens, and what to tell the traveller." />

<Card title="Read a search reference" href="/api-reference/flights/read-a-search" description="Every parameter, response field and error." />

<Card title="Errors" href="/errors" description="Every error code and whether to retry." />
</Cards>