# API v3 and signing in

> The next version of the Asense API adds a device list, saved device settings and sign-in with your Asense account. It is coming to customers soon.

> **Coming soon:** API v3 is not open to customers yet. Keep using [API v2](https://asense.semitronica.pt/docs/api/) until we announce v3 and its address. Your v2 API key will work on v3 too.

## What changes from v2

Reading data works the same way. `GET /` takes the same parameters as v2 and returns the same response, so code written for v2 keeps working. There are two differences in what you get back.

First, FFT frequencies are worked out from each record's own output data rate (`odr`), so a spectrum recorded at a different rate gets the right frequency axis. Second, each caller can only read the devices they are allowed to see. A request for any other device gets a 403 answer.

v3 also adds two kinds of request. `GET /devices` lists the devices you can read, each with its type. `GET` and `PUT /devices/{id}/config` read and save a device's settings: a label, a site name, a position on the map or on the structure, and its place in a sensor chain or a well.

## Two ways to sign in

Every request carries one of two credentials.

An **API key** is the key you already use with v2, sent in the `x-api-key` header. It suits scripts, servers and data pipelines. A key can read data and list devices, but it cannot save device settings.

A **signed-in user** is a person with an Asense account. Your app signs them in with Amazon Cognito and sends their ID token in the `Authorization` header, as `Bearer <token>`. A user can read the devices on their account and save their settings. A user whose account has no devices yet gets an empty device list.

With the Amplify JavaScript library, the ID token comes from the current session:

```ts
import { fetchAuthSession } from "aws-amplify/auth";

const { tokens } = await fetchAuthSession();
const res = await fetch(`${API_URL}/devices`, {
  headers: { Authorization: `Bearer ${tokens?.idToken?.toString()}` },
});
const { devices } = await res.json();
```

With an API key, the same request is:

```bash
curl -H "x-api-key: $ASENSE_API_KEY" "$API_URL/devices"
```

Send the ID token, not the access token. The API refuses access tokens.

## Saving a device's settings

A `PUT` replaces the whole saved configuration, so send every field you want to keep. The body must be JSON of at most 16 KB, and unknown fields are refused. The answer is the configuration as saved, with the time and the user who saved it.

```bash
curl -X PUT "$API_URL/devices/ASENSE00000005/config" \
  -H "Authorization: Bearer $ID_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"label": "North abutment", "position": {"x": 12.5, "y": 0, "z": -3.2}}'
```

## Reference

The rest of this page is built from the [OpenAPI 3.1 file for v3](https://asense.semitronica.pt/docs/openapi/asense-v3.yaml). The parameters of `GET /` are the same as in [the v2 reference](https://asense.semitronica.pt/docs/api/reference/).

The full reference is the OpenAPI 3.1 file: https://asense.semitronica.pt/docs/openapi/asense-v3.yaml

Source page: https://asense.semitronica.pt/docs/api-v3/
