# Python SDK

> The asense package reads your sensor data into pandas or Polars frames and follows the API's paging for you. It also has a command-line tool.

> **Coming soon:** The package is not on PyPI yet. This page shows how it will work once it is published.

## Install

```bash
pip install asense            # with pandas
pip install 'asense[polars]'  # adds Polars
```

## First request

The SDK reads your API key from the `ASENSE_API_KEY` environment variable, or you can pass it to the client.

```bash
export ASENSE_API_KEY=...
```

```python
from asense import Client

client = Client()  # or Client(api_key="...")

# Hourly summaries for one day, one row per hour
hourly = client.get("data", "ASENSE00000005", "2025-10-01", "2025-10-02")

# Five minutes of raw acceleration, one row per sample
acc = client.get("acc", "ASENSE00000005",
                 "2025-10-01T10:00:00Z", "2025-10-01T10:05:00Z")
acc.plot(x="timestamp", y=["acc_x", "acc_y", "acc_z"])

# The same as a Polars frame
acc_pl = client.get("acc", "ASENSE00000005",
                    "2025-10-01T10:00:00Z", "2025-10-01T10:05:00Z", engine="polars")
```

Times can be Unix milliseconds, ISO 8601 strings, or `datetime` and `date` objects. A time without a time zone is read as UTC.

## What a frame looks like

Every frame has `id`, `time` in Unix milliseconds and `timestamp` as a UTC datetime. The other columns depend on the table, as described in [the data model](https://asense.semitronica.pt/docs/data-model/).

| Table | One row per | Columns |
| --- | --- | --- |
| `acc` | sample | `acc_x`, `acc_y`, `acc_z`, plus `tamb`, `w_s` and `w_d` where the packet has them |
| `gyr` | sample | `gyr_x`, `gyr_y`, `gyr_z` |
| `ain` | sample | `ain_a`, `ain_b` |
| `fft` | record and frequency bin | `freq`, `fft_x`, `fft_y`, `fft_z` |
| `data` | hourly record | every summary field; `w_s_avg` is split into `w_s_avg_0` to `w_s_avg_5` |
| `env` | record | every field of the record |
| `connected` | connection | every field of the record |

## Options

`Client.get`, `Client.query`, `Client.pages` and `Client.items` take the API's parameters as keyword arguments: `limit`, `ascending`, `merge`, `output_format`, `enable_correction`, `sync_acc_gyr`, `auto_odr` and `load_prior_elements`. An option you leave out is not sent, so the API's default applies. `get()` always asks for `dict_array` output, because that is what it turns into a frame. Use `extra={...}` to send any other query parameter as it is. [Requests and parameters](https://asense.semitronica.pt/docs/api/) describes each one.

## Pages and raw responses

```python
client.query("env", "ASENSE00000005", start, end)        # one raw page, as a dict
for page in client.pages("acc", "ASENSE00000005", start, end, limit=64):
    ...                                                   # every raw page in turn
client.timestamps("data", "ASENSE00000005", start, end)  # record times only (data, env)
client.health("ASENSE00000005", start, end)              # connection times
```

`pages()` follows `next_timestamp` for you. It becomes the next page's start time, or its end time when `ascending=False`. [Pagination](https://asense.semitronica.pt/docs/api/pagination/) explains the rule.

## Errors

Every error the SDK raises is an `asense.AsenseError`. `ValidationError` means an argument was wrong, and no request was sent. `BadRequestError` (400), `AuthenticationError` (401 and 403), `RateLimitError` (429) and `ServerError` (5xx) are API answers, and each carries `status_code`, `message` and `body`. `TransportError` means no answer came back, for example because of a network failure or a timeout.

## Command line

The package installs an `asense` command. It reads the key from `--api-key` or `ASENSE_API_KEY`.

```bash
asense tables
asense get data ASENSE00000005 --start 2025-10-01 --end 2025-10-02
asense get acc ASENSE00000005 --start 2025-10-01T10:00Z --end 2025-10-01T10:05Z -o acc.csv
asense get env ASENSE00000005 --start 2025-10-01 --end 2025-10-08 --descending --format json
asense get fft ASENSE00000005 --start 2025-10-01 --end 2025-10-02 --raw
asense timestamps data ASENSE00000005 --start 2025-10-01 --end 2025-11-01
asense health ASENSE00000005 --start 2025-10-01 --end 2025-10-02
```

Writing to a `.parquet` file with `-o` needs `pyarrow` installed.

Source page: https://asense.semitronica.pt/docs/sdk/python/
