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 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:
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:
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.
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. The parameters of GET / are the same as in the v2 reference.
Get sensor data
get/
Returns records from table_name for device id between start_time and end_time (both inclusive). A page reads at most limit stored records; when more remain, the response carries next_timestamp. Refused with 403 when the caller may not read id.
Authentication (one of)
- API key in the x-api-key header
- Signed-in user: Authorization: Bearer <token>
Parameters
| Name | In | Type | Default | Description |
|---|---|---|---|---|
table_namerequired | query | acc | gyr | ain | fft | data | env | connected | Table to read. See https://asense.semitronica.pt/docs/data-model/ | |
idrequired | query | string | Device ID, | |
start_timerequired | query | integer | Start of the window, Unix milliseconds, UTC, inclusive. | |
end_timerequired | query | integer | End of the window, Unix milliseconds, UTC, inclusive. | |
limit | query | integer | 256 | Most stored records read per page. On acc, gyr and ain one stored record is one packet of samples. A value that is not an integer falls back to 256. |
ascending | query | true | false | true | Sort by time, oldest first. |
merge | query | true | false | true | Join stored packets. acc, gyr, ain: the whole page becomes one item. fft: the X, Y and Z records of each UTC hour become one item with fft_x, fft_y, fft_z. data, env: no joining. When true, |
output_format | query | map | tuple_array | dict_array | combined_tuple | combined_dict | map | Layout of every series. An unknown value falls back to |
timestamps_only | query | true | false | false | On data and env, return only the record times in the window (never paged, ignores |
auto_odr | query | true | false | true | acc, gyr, ain. Space samples by the measured time between packets (1.0 to 1.5 s apart). Only |
enable_correction | query | true | false | acc, gyr, ain. Repair single packets whose timestamp is out of step. Default depends on the device. | |
sync_acc_gyr | query | true | false | gyr. Move each gyroscope packet to the accelerometer packet recorded up to 200 ms before it. Defaults to the value of | |
load_prior_elements | query | true | false | acc, gyr, ain. Read up to 3 s before |
Responses
| Status | Description | Body |
|---|---|---|
200 | Records in the window. An empty window returns | StandardResponse or TimestampsOnlyResponse |
400 | Missing or invalid parameters. | Error |
401 | No credential, or the credential is invalid (sent by API Gateway). | object |
403 | The caller may not read this device. | Error |
500 | The request could not be completed, for example for an unknown | Error |
List the caller's devices
get/devices
The device IDs the caller may read, each with its type. An unrestricted API key gets every device on the account. A signed-in user who has not been given any devices gets an empty list. Never paged.
Authentication (one of)
- API key in the x-api-key header
- Signed-in user: Authorization: Bearer <token>
Responses
| Status | Description | Body |
|---|---|---|
200 | The caller's devices, sorted by ID. | DeviceList |
401 | No credential, or the credential is invalid (sent by API Gateway). | object |
Get a device's saved configuration
get/devices/{id}/config
Returns {"config": null} when nothing was saved yet.
Authentication (one of)
- API key in the x-api-key header
- Signed-in user: Authorization: Bearer <token>
Parameters
| Name | In | Type | Default | Description |
|---|---|---|---|---|
idrequired | path | string | Device ID, |
Responses
| Status | Description | Body |
|---|---|---|
200 | The saved configuration, or null. | object |
401 | No credential, or the credential is invalid (sent by API Gateway). | object |
403 | The caller may not read this device. | Error |
Save a device's configuration
put/devices/{id}/config
Replaces the whole saved configuration. Cognito users only; API keys get 403. The body is validated against the DeviceConfig schema below and may be at most 16 KB. The server sets id, updated_by (Cognito sub) and updated_at.
Authentication
- Signed-in user: Authorization: Bearer <token>
Parameters
| Name | In | Type | Default | Description |
|---|---|---|---|---|
idrequired | path | string | Device ID, |
Request body: DeviceConfig
| Field | Type | Description |
|---|---|---|
label | string | |
site | string | |
lat | number | Overrides the GPS latitude, decimal degrees |
lon | number | Overrides the GPS longitude, decimal degrees |
position | object | Position in the site's own frame, metres. |
chain | object | Place in a tilt or inclinometer chain. |
well | object | Groundwater well geometry. |
{
"label": "North abutment",
"site": "Ponte 25 de Abril",
"lat": 38.6916,
"lon": -9.1773,
"position": {
"x": 12.5,
"y": 0,
"z": -3.2
},
"chain": {
"id": "chain-a",
"order": 2,
"depth_m": 4.5
}
}Responses
| Status | Description | Body |
|---|---|---|
200 | The configuration as saved. | object |
400 | Body is not JSON or fails the schema. | Error |
401 | No credential, or the credential is invalid (sent by API Gateway). | object |
403 | The device is not allowed, or the caller used an API key. | Error |
413 | Body larger than 16 KB. | Error |