Sort Order
Control whether scan results are returned oldest-first or newest-first using the order field in any SaQL scan query.
| Method | Path | Relevant field | Values |
|---|---|---|---|
POST | /v1/agents/:id/query | order | "asc" | "desc" |
Default Order
When order is omitted, SapixDB returns records in ascending strand order — oldest record first, newest last. This mirrors the physical write order inside the strand.
POST /v1/agents/events/query
Authorization: Bearer spx_root_YOUR_ROOT_KEY
Content-Type: application/json
{
"type": "scan",
"limit": 5
}{
"type": "scan",
"limit": 5,
"order": "asc"
}Descending Order
Set "order": "desc"to flip the result set so the most-recent record appears first. This is useful for dashboards, activity feeds, and "latest N events" queries.
{
"type": "scan",
"limit": 5,
"order": "desc"
}curl -s -X POST http://localhost:7475/v1/agents/events/query \
-H "Content-Type: application/json" \
-H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \
-d '{"type": "scan", "limit": 5, "order": "desc"}' \
| python3 -m json.toolFilters and Sort Order
Filtering always happens before the sort direction is applied. SapixDB collects all records that satisfy the filter predicate, then reverses the result set when order is "desc". The limit is applied after filtering and after reversal.
{
"type": "scan",
"limit": 10,
"order": "desc",
"filter": {
"field": "type",
"op": "eq",
"value": "purchase"
}
}limit.Field Reference
| Field | Type | Default | Description |
|---|---|---|---|
order | "asc" | "desc" | "asc" | Sort direction for the returned records. asc = oldest first (strand order); desc = newest first. |
limit | integer | — | Maximum number of records to return. Applied after filtering and after sort direction. |
filter | object | none | Predicate applied before sorting. See the SaQL reference for filter syntax. |
Python SDK Example
The Python SDK passes the full SaQL body as a dictionary. Sorting is controlled by the same order key.
import requests
BASE = "http://localhost:7475"
AGENT = "events"
HEADERS = {
"Content-Type": "application/json",
"Authorization": "Bearer spx_root_YOUR_ROOT_KEY",
}
# Newest 20 purchase events
payload = {
"type": "scan",
"limit": 20,
"order": "desc",
"filter": {"field": "type", "op": "eq", "value": "purchase"},
}
resp = requests.post(
f"{BASE}/v1/agents/{AGENT}/query",
json=payload,
headers=HEADERS,
)
resp.raise_for_status()
for record in resp.json()["records"]:
print(record["timestamp_hlc"], record["data"])"order": "desc" and use after_hlc with the timestamp_hlc of the last record on each page. See Cursor Pagination.