> ## Documentation Index
> Fetch the complete documentation index at: https://docs.olostep.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Paginate or Stream using Cursor

> Page through batch items with cursor and limit on GET /v1/batches/{id}/items, or stream them while the job is still running.

We use a `cursor` and `limit` based mechanism to help you paginate or stream data efficiently.

`GET /v1/batches/{id}/items` defaults to **10000** items per page (maximum 10000). When more items remain, the response includes a `cursor`. Pass it on the next request until `cursor` is absent or null.

The response also includes a `cursor` while the batch is still running and more items can come. Use it to read items as they complete, before the whole batch is done.

The example below waits for the batch to finish. It then reads the result pages.

```python batch-paginate.py theme={null}
import time

import requests

API_URL = "https://api.olostep.com/v1"
API_KEY = "<YOUR_API_KEY>"
HEADERS = {"Authorization": f"Bearer {API_KEY}"}


def initiate_batch(data):
    response = requests.post(
        f"{API_URL}/batches", headers=HEADERS, json=data, timeout=30
    )
    response.raise_for_status()
    return response.json()


def wait_for_job(path, timeout_s=1800, interval_s=10):
    """Check GET /v1/<path> until the job is completed or failed.

    Wait interval_s seconds between checks. Start no new check after
    timeout_s seconds. Stopping does not cancel the job.
    """
    deadline = time.monotonic() + timeout_s
    while time.monotonic() < deadline:
        response = requests.get(
            f"{API_URL}/{path}",
            headers={"Authorization": f"Bearer {API_KEY}"},
            timeout=30,
        )
        response.raise_for_status()  # Stop on an HTTP error, for example 401 or 429
        job = response.json()
        if not isinstance(job.get("status"), str):
            raise ValueError(f"{path}: no status in the response")
        print("Status:", job["status"])
        if job["status"] == "completed":
            return job
        if job["status"] == "failed":
            raise RuntimeError(f"{path} failed")
        time.sleep(interval_s)
    raise TimeoutError(f"{path} is not complete after {timeout_s} seconds")


def get_batch_list(batch_id, cursor=None, limit=None, status=None):
    params = {"cursor": cursor, "limit": limit, "status": status}
    response = requests.get(
        f"{API_URL}/batches/{batch_id}/items",
        headers=HEADERS,
        params=params,
        timeout=30,
    )
    response.raise_for_status()
    return response.json()


def example_batch_with_cursor():
    data = {
        "items": [
            {"custom_id": "1", "url": "https://github.com"},
            {"custom_id": "2", "url": "https://github.com/features"},
        ]
    }
    batch = initiate_batch(data)
    batch_id = batch["id"]

    wait_for_job(f"batches/{batch_id}")

    cursor = None  # The first request has no cursor
    seen = set()  # Cursors from the API, to stop if one repeats
    for _ in range(1000):  # At most 1,000 pages
        batch_list = get_batch_list(batch_id, cursor=cursor, limit=10)
        for item in batch_list["items"]:
            print(f"Custom ID: {item['custom_id']}")
            print(f"URL: {item['url']}")
            # Items have no content. Pass retrieve_id to GET /v1/retrieve to get it.
            print(f"Retrieve ID: {item['retrieve_id']}")
        cursor = batch_list.get("cursor")
        if cursor is None:  # No cursor: this was the last page
            return
        if cursor in seen:
            raise RuntimeError(f"Pagination stopped: cursor {cursor} repeated")
        seen.add(cursor)
    raise RuntimeError("Pagination stopped after 1000 pages")


example_batch_with_cursor()
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.