Documentation menu

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.

View as Markdown

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

NameInTypeDefaultDescription
table_namerequiredqueryacc | gyr | ain | fft | data | env | connected

Table to read. See https://asense.semitronica.pt/docs/data-model/

idrequiredquerystring

Device ID, ASENSE followed by eight digits.

start_timerequiredqueryinteger

Start of the window, Unix milliseconds, UTC, inclusive.

end_timerequiredqueryinteger

End of the window, Unix milliseconds, UTC, inclusive.

limitqueryinteger256

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.

ascendingquerytrue | falsetrue

Sort by time, oldest first. false (newest first) is allowed on data, env and connected only; then next_timestamp is the next page's end_time.

mergequerytrue | falsetrue

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, seq, odr and scale are removed from every item. Any value other than false counts as true.

output_formatquerymap | tuple_array | dict_array | combined_tuple | combined_dictmap

Layout of every series. An unknown value falls back to map.

timestamps_onlyquerytrue | falsefalse

On data and env, return only the record times in the window (never paged, ignores limit).

auto_odrquerytrue | falsetrue

acc, gyr, ain. Space samples by the measured time between packets (1.0 to 1.5 s apart). Only true enables it.

enable_correctionquerytrue | false

acc, gyr, ain. Repair single packets whose timestamp is out of step. Default depends on the device.

sync_acc_gyrquerytrue | false

gyr. Move each gyroscope packet to the accelerometer packet recorded up to 200 ms before it. Defaults to the value of enable_correction.

load_prior_elementsquerytrue | false

acc, gyr, ain. Read up to 3 s before start_time to time the first packet, then drop it. Defaults to true when any timing correction is on.

Responses

StatusDescriptionBody
200

Records in the window. An empty window returns {"data": []}.

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 table_name.

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

StatusDescriptionBody
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

NameInTypeDefaultDescription
idrequiredpathstring

Device ID, ASENSE followed by eight digits.

Responses

StatusDescriptionBody
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

NameInTypeDefaultDescription
idrequiredpathstring

Device ID, ASENSE followed by eight digits.

Request body: DeviceConfig

FieldTypeDescription
labelstring
sitestring
latnumber

Overrides the GPS latitude, decimal degrees

lonnumber

Overrides the GPS longitude, decimal degrees

positionobject

Position in the site's own frame, metres.

chainobject

Place in a tilt or inclinometer chain.

wellobject

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

StatusDescriptionBody
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