# Statements

Export an account statement, ledger, or balances report for a period as one CSV file, built in the background with StatementExportService.

A statement export covers one account for a period. Choose a grouped account statement, an individual-movement ledger, or a balances report. Polyester builds each in the background as one CSV file: [`StatementExportService`](https://testnet.polyester.com/docs/api-docs/connect/ledger.read.v1.StatementExportService/CreateStatementExport) is the statement API, over REST and ConnectRPC.

> **The paged statement is gone**
>
> `LedgerReadService.ExportStatement` and REST `GET /v1/statements` no longer exist. Create a statement export with `type: ACCOUNT_STATEMENT` for the same account, Asset, and period instead; its file has the same columns.

## Access

API keys need the `READ_STATEMENTS` policy action, for every call, cancel and create included. The statement covers one account:

- Omit `subaccountId` for the Main Account. A Subaccount-scoped key always reads its own Subaccount.
- A Main Account key reads the Main Account and the Subaccounts it owns.
- A session reads the Main Account and the Subaccounts its role gives access to.

See [API key and Subaccount policies](https://testnet.polyester.com/docs/developer-docs/account/api-key-policies). Access is checked again on every read and cancel of an export, on every download through the file endpoint, and whenever a download URL is issued. An export of an account you cannot read, or can no longer read, answers `404`, as one that does not exist. A download URL already issued is not checked again: it downloads the file until it stops working, at most 60 seconds after it was issued.

## Export types

Choose the required `type` when creating an export:

- `ACCOUNT_STATEMENT` groups one ledger operation into rows by balance area and Asset, with the amount, fees paid, and balance after the entry. Its columns are unchanged.
- `LEDGER` lists each individual ledger movement, with its transfer reason, side, counterparty, and balance after the movement. A trade has separate rows for its base and quote legs and each fee.
- `BALANCES` summarizes opening and closing balances, total credits and debits, and movement counts by balance area and Asset. It is available through the API only, not in the Polyester app.

All three types have the same period limit, Asset filter, exports-in-progress and movement limits, retention, and export lifecycle. A balances export also requires its start time to be within the available account history. Create, get, list, and cancel return the export's `type`. Exports created before 0.93.12 are returned as `ACCOUNT_STATEMENT`.

> **Breaking change in 0.93.12**
>
> Existing clients must send `type: ACCOUNT_STATEMENT` to request the previous CSV layout. Omitting `type`, sending `STATEMENT_EXPORT_TYPE_UNSPECIFIED` (0), or sending an undefined value returns HTTP 400 `BAD_REQUEST` (`invalid_argument` over ConnectRPC). The suggested download filename has also changed.

## CSV format

All three files are RFC 4180 CSV in UTF-8 with CRLF line endings. The first row is the header. For account statements and ledgers, a period without movements gives the header row alone, and rows are oldest first. Amounts are exact decimal strings with up to 18 fractional digits and no trailing zeros.

### Account statement (ACCOUNT_STATEMENT)

Each row is the effect of one entry on one balance area in one Asset. An entry is one ledger operation, such as a trade with its fees, a deposit, a withdrawal step, or an internal transfer.

The columns, in order:

- `time` · the entry time, RFC 3339 in UTC with microseconds
- `entry_id` · the `transferId` of the entry's first leg on the account within the export's period, as [`ListTransfers`](https://testnet.polyester.com/docs/api-docs/connect/ledger.read.v1.LedgerReadService/ListTransfers) returns it; an unsigned 128-bit integer as a decimal string, the same across Asset filters for that period
- `type` · `trade` for a trade's settlement and its fees; otherwise the transfer reason, such as `deposit`, `withdraw`, `internal_transfer`, `funding_to_trading`, or `system_adjustment`
- `account` · the balance area, `funding` or `trading`
- `asset` · the Asset symbol
- `amount` · the signed balance change in whole units, without the fees in `fee`; fees the account receives are included here
- `fee` · the fees the account paid in this Asset for the entry, `0` when none
- `balance` · the balance area's balance in this Asset after the entry
- `link_id` · an ID shared by related legs, the match ID for a trade; empty when unavailable
- `flow_id` · the lifecycle flow ID of a deposit, withdrawal, or internal transfer linked before the export was created; empty otherwise

For consecutive rows of the same balance area and Asset, each `balance` equals the previous one plus `amount` minus `fee`.

An Asset filter selects an entry's rows after the entry is built from all its movements on the account. For the same account and covered period, an Asset-filtered file has the same `entry_id`, `time`, `type`, and `link_id` as the corresponding rows in an all-Assets file. The `flow_id` also matches when the links are the same at both exports' creation times. Match rows using `entry_id`, `account`, and `asset` within that period.

A period boundary can fall between an entry's legs. Each export then contains its own part, under the `transferId` of that part's first leg, so do not join rows across periods on `entry_id` alone. Reconcile amounts and balances across split periods too. Asset-filtered files produced before 0.93.10 can have different IDs for the same entry; create new exports to reconcile them with all-Assets files.

### Ledger (LEDGER)

Each row is one transfer leg on the account, as [`ListTransfers`](https://testnet.polyester.com/docs/api-docs/connect/ledger.read.v1.LedgerReadService/ListTransfers) returns it. The columns, in order:

- `time` · the transfer time, RFC 3339 in UTC with microseconds
- `transfer_id` · the transfer identifier, an unsigned 128-bit integer as a decimal string; together with `account` and `side`, it identifies the row
- `account` · the balance area, `funding` or `trading`
- `asset` · the Asset symbol
- `type` · the transfer reason, such as `trade_base`, `trade_quote`, `maker_fee`, `taker_fee`, `rebate`, `deposit`, `withdraw`, `internal_transfer`, or `funding_to_trading`
- `side` · `debit` when the movement takes from the balance area, `credit` when it adds to it
- `amount` · the signed balance change in whole units of the Asset; negative for a debit, positive for a credit
- `counterparty` · the kind of the other side, `funding_account`, `trading_account`, `external_address`, `private_counterparty`, `fee_account`, or `system_account`; empty when unknown
- `balance` · the balance area's balance in the Asset after the movement
- `link_id` · an ID shared by related legs, the match ID for a trade; empty when unavailable
- `flow_id` · the lifecycle flow ID linked before the export was created; empty otherwise

There is no `entry_id` or separate `fee` column. Fees are movements in their own rows. For consecutive rows of the same balance area and Asset, each `balance` equals the previous one plus `amount`.

An Asset filter keeps only the selected Asset's legs, without grouping or changing their `transfer_id`. All export types still read movements of all Assets for the covered period; all count toward the 4,000,000-movement limit. The `movements` count in a ledger export counts the retained legs, one per data row.

### Balances (BALANCES)

Each row summarizes one balance area in one Asset over the covered period. Balances are posted balances at the boundaries, before any movement at that instant. The columns, in order:

- `account` · the balance area, `funding` or `trading`
- `asset` · the Asset symbol
- `opening_balance` · the balance at `startTime`
- `credits` · the sum of the period's credit legs, as a positive amount, or `0` when none
- `debits` · the sum of the period's debit legs, as a positive amount, or `0` when none
- `closing_balance` · the balance at `completeThrough`, not necessarily the requested `endTime`
- `movements` · the number of posted ledger transfer legs in the covered period for this balance area and Asset, including fee legs; not the number of grouped entries

Every row satisfies `closing_balance = opening_balance + credits - debits`. The credit and debit totals correspond to the LEDGER file's legs for the same balance area, Asset, and covered period. At `READY`, the export's `movements` field is the sum of the CSV's movement counts, not its row count.

Rows are sorted by `account`, then `asset`. A balance area and Asset with zero balances at both ends and no movements has no row. A nonzero balance with no movements still has a row, with zero credits, debits, and movements. The file has only its header when there are no qualifying rows. An Asset filter keeps only that Asset's rows; the movement read limit still counts all Assets.

## How far a statement is complete

An export covers movements from `startTime` up to `completeThrough`, exclusive. `completeThrough` is the earliest of `endTime`, the moment the export was created, and just after the newest movement recorded in the ledger then (its timestamp plus 1 ns), but never before `startTime`. It is fixed at creation, so it can be earlier than the creation time, and time the export spends queued or processing does not extend it. Every movement recorded before the export was created and before `completeThrough` is in the file, so the file never changes once it is written. Read the period's end from the returned `completeThrough` rather than computing it, and create another export later for the rest of the period.

### Flow links as of creation

For ACCOUNT_STATEMENT and LEDGER, files generated under 0.93.12 use `createdAt` as the flow-link cutoff, not the period end or when the file becomes ready. The export waits for transfer history to cover `completeThrough` and for the flow-link watermark to reach its creation time. This watermark confirms that all link versions written before that time are available. Each movement uses its latest link written strictly before `createdAt`, even when file generation resumes after an interruption.

If a flow was not linked by creation time, its `flow_id` is empty. A link written later is not added to this export, even if the export is still processing. Once the link is available, `ListTransfers` and a new export whose creation is after the link was written can show it. For ACCOUNT_STATEMENT and LEDGER, if `completeThrough` equals `startTime`, the file has only its header and needs neither watermark.

### Balance history coverage

BALANCES uses the transfer history and verified balance history, not the flow-link watermark. It waits for those histories to agree before producing the file, even when there are no movements in the covered period.

Account balance history starts at a point in time. On test networks it starts again at each reset, so periods starting before that history are refused. Exportable periods build up as history grows. If the history is not yet verified, a create can be accepted and wait for it. Once the earliest available instant is known, a new BALANCES request starting before it is refused with HTTP 409 `STATEMENT_HISTORY_UNAVAILABLE`. The problem's `historyAvailableFrom` member gives that instant as an RFC 3339 timestamp in UTC. ConnectRPC returns `FAILED_PRECONDITION` with `ERROR_CODE_STATEMENT_HISTORY_UNAVAILABLE` and `ErrorDetail.history_available_from`.

Create again with `startTime` at or after that instant, keeping `endTime` later than the new start. If the available history subsequently starts later than an accepted export's start, the export can end `FAILED` with `HISTORY_UNAVAILABLE` instead.

## Lifecycle

1. Create

   Create an export for one account, a type, an optional Asset, and a period. It starts `QUEUED`.

2. Follow

   Polyester writes the file in the background (`PROCESSING`). `movements` and `bytes` report its progress. Poll the export until it is final: `READY`, `FAILED`, or `CANCELED`. Cancel it while it is `QUEUED` or `PROCESSING` if you no longer need it.

3. Download

   Download a `READY` file until `expiresAt`, 7 days after it became ready. The export is then `EXPIRED` and its file is deleted; the export itself stays readable.

### Endpoints

- **Create** · REST [`POST /v1/statements/exports`](https://testnet.polyester.com/docs/api-docs/rest/POST/v1/statements/exports), ConnectRPC [`CreateStatementExport`](https://testnet.polyester.com/docs/api-docs/connect/ledger.read.v1.StatementExportService/CreateStatementExport)
- **Get one** · REST [`GET /v1/statements/exports/{exportId}`](https://testnet.polyester.com/docs/api-docs/rest/GET/v1/statements/exports/export_id), ConnectRPC [`GetStatementExport`](https://testnet.polyester.com/docs/api-docs/connect/ledger.read.v1.StatementExportService/GetStatementExport)
- **List** · REST [`GET /v1/statements/exports`](https://testnet.polyester.com/docs/api-docs/rest/GET/v1/statements/exports), ConnectRPC [`ListStatementExports`](https://testnet.polyester.com/docs/api-docs/connect/ledger.read.v1.StatementExportService/ListStatementExports)
- **Cancel** · REST [`POST /v1/statements/exports/{exportId}/cancel`](https://testnet.polyester.com/docs/api-docs/rest/POST/v1/statements/exports/export_id/cancel), ConnectRPC [`CancelStatementExport`](https://testnet.polyester.com/docs/api-docs/connect/ledger.read.v1.StatementExportService/CancelStatementExport)
- **Download URL** · REST [`GET /v1/statements/exports/{exportId}/download-url`](https://testnet.polyester.com/docs/api-docs/rest/GET/v1/statements/exports/export_id/download-url), ConnectRPC [`GetStatementExportDownloadUrl`](https://testnet.polyester.com/docs/api-docs/connect/ledger.read.v1.StatementExportService/GetStatementExportDownloadUrl)
- **Download the file** · REST `GET /v1/statements/exports/{exportId}/file` only

Every endpoint needs a session or a signed API-key request. Only a download URL works without credentials, for 60 seconds.

### Create an export

```json
{
	"idempotencyKey": "statement-2026-10",
	"type": "ACCOUNT_STATEMENT",
	"startTime": "2026-10-01T00:00:00Z",
	"endTime": "2026-11-01T00:00:00Z",
	"assetId": 1
}
```

- `idempotencyKey` · required, 1 to 64 letters, digits, `.`, `_`, `:`, or `-`, unique per Main Account. The same request with the same key returns the same export. The same key with other fields, including a different export type, is refused with `IDEMPOTENCY_KEY_REUSED`.
- `type` · required, `ACCOUNT_STATEMENT`, `LEDGER`, or `BALANCES`. There is no default.
- `startTime` · required, inclusive, no later than the request.
- `endTime` · required, exclusive, after `startTime` and at most 366 days later. It may be in the future: the period then ends no later than the request; see `completeThrough` above.
- `assetId` · optional. Omit it for every Asset; an ID that names no Asset is refused.
- `subaccountId` · optional. Omit it for the Main Account.

The response is the export, with its `exportId` and status `QUEUED`. `exportId` and `subaccountId` are public IDs, base58 strings in REST JSON.

For a balances report, send `BALANCES` and use dates within the available account history:

```json
{
	"idempotencyKey": "balances-2026-11",
	"type": "BALANCES",
	"startTime": "2026-11-01T00:00:00Z",
	"endTime": "2026-12-01T00:00:00Z"
}
```

### Follow an export

Get returns one export. List returns an account's exports newest first; it takes `subaccountId`, `limit` (up to 100, default 20), and `pageToken` from the previous `nextPageToken`. Each export reports `type`, `status`, `movements` and `bytes`, `completeThrough`, `readyAt`, `expiresAt`, and `failureReason`. REST spells the enum values without their prefix. ConnectRPC reports `STATEMENT_EXPORT_STATUS_FAILED` with `STATEMENT_EXPORT_FAILURE_REASON_TOO_LARGE` for an export that exceeds the movement limit. This is a background failure recorded on the export; read its status to learn the outcome.

- **`QUEUED`** · waiting for transfer history to cover the period, for flow-link coverage (ACCOUNT_STATEMENT and LEDGER) or verified balance history (BALANCES), or for another export of the same account: one export per account is written at a time. Poll later.
- **`PROCESSING`** · the file is being written or waits for the required history coverage before resuming. BALANCES also waits if the balance and transfer histories disagree. Poll later.
- **`READY`** · download it before `expiresAt`.
- **`FAILED`** with `TOO_LARGE` · the account's covered period holds more than 4,000,000 ledger movements across all Assets, even when `assetId` selects only one. Split the period into shorter exports, each with a new `idempotencyKey`, until each is within the limit. Retrying the same period or selecting fewer Assets does not avoid the limit.
- **`FAILED`** with `UNAVAILABLE` · the file could not be written. A processing export that resumes more than 24 hours after creation and whose history or flow-link watermark is behind or unreadable also ends in this state and frees its processing slot. Before that deadline, an unreadable watermark is retried. ConnectRPC reports `STATEMENT_EXPORT_STATUS_FAILED` with `STATEMENT_EXPORT_FAILURE_REASON_UNAVAILABLE`. A BALANCES export also fails with this reason if the balance and transfer histories still disagree past the deadline. Create a new export with a new key.
- **`FAILED`** with `HISTORY_UNAVAILABLE` · a BALANCES export starts before the available balance history, for example if its origin moved after creation. The export carries `historyAvailableFrom` (ConnectRPC `StatementExport.history_available_from`) and the ConnectRPC failure reason is `STATEMENT_EXPORT_FAILURE_REASON_HISTORY_UNAVAILABLE`. Create a new export with a new key and a start at or after that instant.
- **`EXPIRED`** · the file was deleted. Create a new export.
- **`CANCELED`** · the export was canceled and has no file.

### Exports in progress

List also returns `inProgressExports` and `maxInProgressExports`. `inProgressExports` counts the exports `QUEUED` or `PROCESSING` across the whole Main Account, its Subaccounts included, whichever account the list selects. It is the number a new create counts against `maxInProgressExports`; while the two are equal, a create answers `STATEMENT_EXPORT_LIMIT_REACHED`. Read the count from List instead of counting statuses yourself: another client, or the Polyester app, may have exports in progress on the same Main Account.

### Cancel an export

Cancel ends a `QUEUED` or `PROCESSING` export `CANCELED` at once and returns it. The export no longer counts toward the exports in progress, the account's next queued export can start, and any partial file is deleted.

- **Repeat a cancel** · canceling a `CANCELED` export returns it as it is, so a cancel that timed out can be sent again.
- **Too late** · a `READY`, `FAILED`, or `EXPIRED` export can no longer be canceled. Cancel answers `STATEMENT_EXPORT_NOT_CANCELABLE` (REST `409`, ConnectRPC `failed_precondition`). Read the export for its final state.

### Download the file

There are two ways to download a `READY` file:

- **Download URL** · Get the download URL for the export. The answer carries `url`, `expiresAt`, and the suggested `filename`. The URL needs no credentials, so a browser can open it to save the file. It works until `expiresAt`: 60 seconds after it was signed for your request, or when the file expires if that is sooner. Request a new URL after that. Anyone who holds the URL can download the file until it ends, so do not share, store, or log it.
- **Authenticated file** · `GET /v1/statements/exports/{exportId}/file` with your session or a signed API-key request returns `text/csv` with `Content-Disposition`, an `ETag`, and `Accept-Ranges: bytes`. To resume a download, send one `Range: bytes=START-END`; the answer is `206` with `Content-Range`, and a range outside the file answers `416`. Add `If-Range` with the `ETag` you saw, so a changed file comes back whole. The file route has no `redirect` parameter; use the download URL instead.

Both download methods save the file as `account-statement-<exportId>.csv`, `ledger-<exportId>.csv`, or `balances-<exportId>.csv`, according to the export's type, rather than the previous `statement-<exportId>.csv` name. Update any clients that depend on that name.

Either way, the file is not downloadable in every state:

- **Before `READY`** · `409` with `STATEMENT_EXPORT_NOT_READY`.
- **`FAILED`** · `409` with `STATEMENT_EXPORT_FAILED` (ConnectRPC `failed_precondition` with `ERROR_CODE_STATEMENT_EXPORT_FAILED`). Read the export's `failureReason`; for `TOO_LARGE`, split the period as above. For `HISTORY_UNAVAILABLE`, start at or after `historyAvailableFrom`.
- **`CANCELED`** · `409` with `STATEMENT_EXPORT_CANCELED`.
- **After `expiresAt`** · `410` with `STATEMENT_EXPORT_EXPIRED`. A download URL is also refused this way when the file has less than 5 seconds left.

### Limits

- **Period** · up to 366 days
- **Exports in progress** · at most 5 `QUEUED` or `PROCESSING` per Main Account, Subaccounts included. Another create answers `STATEMENT_EXPORT_LIMIT_REACHED` (REST `429`, ConnectRPC `resource_exhausted`). Cancel an export you no longer need to free its place.
- **Movements read** · up to 4,000,000 ledger movements of the account across all Assets in the covered period, including Assets an `assetId` filter leaves out of the file
- **Retention** · 7 days after the file is ready
- **Download URL** · valid for 60 seconds

## Errors

Statement export REST errors are `application/problem+json` with an uppercase `code`, such as `IDEMPOTENCY_KEY_REUSED`. ConnectRPC carries the same code in `ledger.read.v1.ErrorDetail`, such as `ERROR_CODE_IDEMPOTENCY_KEY_REUSED`.

- **`400`** · `BAD_REQUEST`: a field is invalid, including a missing, unspecified, or undefined export type; the detail names it.
- **`403`** · the API key lacks `READ_STATEMENTS`, or the role does not allow it.
- **`404`** · no such export or Subaccount for you.
- **`409`** · `IDEMPOTENCY_KEY_REUSED`, `STATEMENT_EXPORT_NOT_READY`, `STATEMENT_EXPORT_FAILED`, `STATEMENT_EXPORT_CANCELED`, `STATEMENT_EXPORT_NOT_CANCELABLE`, or `STATEMENT_HISTORY_UNAVAILABLE`. For the latter, read the problem's `historyAvailableFrom` to choose a new start time.
- **`410`** · `STATEMENT_EXPORT_EXPIRED`.
- **`413`** · `REQUEST_TOO_LARGE`: the request body is too large.
- **`416`** · `RANGE_NOT_SATISFIABLE`: the `Range` of a file download is outside the file.
- **`429`** · `STATEMENT_EXPORT_LIMIT_REACHED`.
- **`500`** · `SERVER_ERROR`: the request failed unexpectedly.
- **`503`** · a dependency is briefly unavailable, such as `UPSTREAM_ERROR`: retry the same request.

> **Retry a create with its key**
>
> A create that timed out may still have created the export. Repeat it with the same `idempotencyKey` and fields: it returns the existing export instead of a second one.

Field schemas are in the [API reference](https://testnet.polyester.com/docs/api-docs), and every code under [Balances, transfer history, and statements](https://testnet.polyester.com/docs/developer-docs/error-handling/error-codes#balances-transfer-history-and-statements) in the error codes.
