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

# Copy every allocation

> Read the allocations feed page by page, and read the new allocations as Engrate stores them.

This guide reads every allocation of your organization from [Read Allocations Feed](https://docs.engrate.io/api-reference/timeseries/read-allocations-feed.md) and stores them in a local SQLite database. When the copy has every allocation, it waits and then reads the new ones.

> [!NOTE]
>
> This is a simplified implementation. It shows how to read from the API. How you store the records, handle errors and run the loop in your own system is up to you.

You need an API key (see [Authentication](https://docs.engrate.io/guides/authentication.md)) and Python with [httpx](https://www.python-httpx.org) installed.

## How the script reads the feed

1. It reads a page. `after` is the `feed_sequence` of the last record in the copy, or 0 when the copy is empty.
2. It reads the response line by line and stores each line in the same transaction. The response is JSON Lines: each line is one allocation record.
3. When a response is empty, the copy has every record that Engrate stored. The script waits and reads again.

## The script

```python copy_allocations.py theme={null}
import json
import os
import sqlite3
import time

import httpx

# The most records in one response. 10,000 if you leave it out, 50,000 at most.
LIMIT = 10_000
# The seconds to wait when your copy has every record.
WAIT = 60

# httpx asks for gzip and decodes the response for you.
api = httpx.Client(
    base_url="https://api.engrate.io",
    headers={"authorization": os.environ["API_KEY"]},
    timeout=60,
)

db = sqlite3.connect("allocations.db")
db.execute("CREATE TABLE IF NOT EXISTS allocations (record TEXT NOT NULL)")

# Continue after the last record in your copy, or start from the beginning.
(after,) = db.execute(
    "SELECT coalesce(max(json_extract(record, '$.feed_sequence')), 0) FROM allocations"
).fetchone()

while True:
    last = None
    try:
        # One transaction for each page. If the read breaks, nothing of the page is stored.
        with db, api.stream(
            "GET",
            "/market-access/v1alpha1/timeseries/feed/allocations",
            params={"after": after, "limit": LIMIT},
        ) as response:
            response.raise_for_status()
            # The response is JSON Lines: one record on each line.
            # Read it line by line, so the whole response is never in memory.
            for line in response.iter_lines():
                db.execute("INSERT INTO allocations VALUES (?)", (line,))
                last = line
    except httpx.TransportError:
        # The read broke. Nothing was stored, so read the same records again.
        time.sleep(WAIT)
        continue

    # The page is stored, so the next read starts after its last record.
    if last is not None:
        after = json.loads(last)["feed_sequence"]
    else:
        # The response was empty: your copy has every record that Engrate stored.
        # Wait for new ones.
        time.sleep(WAIT)
```

Run it with your API key:

```bash theme={null}
API_KEY="<your API key>" python copy_allocations.py
```

## Next steps

* [Allocation feed record](https://docs.engrate.io/api-reference/allocations/models/allocation-feed-record.md) gives the fields of each record.
* A timestep can have more than one value. See [Multiple values per timestep](https://docs.engrate.io/api-reference/allocations/overview.md#multiple-values-per-timestep) for how to find the current one.


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