Documentation / API

AI assistant

The "Ask my data" assistant over HTTP: status and quota, questions answered as a stream of NDJSON events, stored conversations, confirmation of the actions it proposes, and decoder, rule and dashboard proposals.

All paths are relative to https://apiv1.opensensorcloud.com/api/v1. These routes need a session cookie: a personal API token gets 403 token_not_allowed (to use your own AI client with a token, see the MCP server). How the assistant works for users: The AI assistant.

Availability and errors

The assistant is available for an organization only when the instance has an Anthropic API key and an instance administrator enabled the organization (off by default, see Enabling per organization). Every route below except GET …/ai/status is refused before any model call otherwise:

Status error When
503 ai_disabled the instance has no AI key
403 ai_not_enabled_for_org the organization is not enabled
403 token_not_allowed the request uses a personal API token
403 forbidden role too low (proposals and confirming actions: editor and above)
404 not_found not a member of the organization, or a conversation or action that is not yours
429 ai_quota_exceeded the organization's daily quota is used up
400 validation invalid body

Quotas

Per organization and UTC day, shared by all members: a number of requests (AI_MAX_REQUESTS_PER_ORG_PER_DAY, default 200) and of tokens (AI_MAX_TOKENS_PER_ORG_PER_DAY, default 2,000,000; uncached input and cache writes in full, cache reads count for a tenth, plus output). Checked before each question or proposal, which counts as one request; confirming or cancelling an action is free.

GET /orgs/{orgId}/ai/status — member

{ "enabled": true, "reason": null, "requests": { "used": 12, "limit": 200 }, "tokens": { "used": 184230, "limit": 2000000 } }

reason when enabled is false: instance_disabled (no AI key) or org_disabled (organization not enabled). Members also read the flag as aiEnabled in GET /orgs.

POST /orgs/{orgId}/ai/ask — member

{
  "conversationId": "5b8e0f6a-…",
  "message": "What is the last value here?",
  "timeZone": "Europe/Paris",
  "locale": "en",
  "context": { "route": "device", "deviceId": "0c7f2d1e-…", "deviceName": "Pont" }
}
Field Rules
message 1 to 4,000 characters
conversationId optional: continues one of your conversations in this organization (404 otherwise); omitted, a new conversation starts
timeZone optional IANA time zone, for "yesterday" or "Tuesday"
locale optional, fr or en: the interface language, used for the documentation links of the answer
context optional: the page the question is asked from. route is one of devices, device, dashboards, dashboard, alerts, profiles, integrations, members, settings, audit, assistant, other; deviceId and dashboardId are UUIDs, deviceName and dashboardName at most 200 characters. "Here" and "this device" then refer to that device or dashboard; access checks are unchanged.

The answer is 200 with content-type: application/x-ndjson: one JSON event per line, written as the answer is produced. Errors found before the first line (access, gate, quota, validation) keep their HTTP status and the usual error body.

type Fields Meaning
conversation id first line: the conversation id, to send back as conversationId
tool name the assistant is calling a tool (list_devices, get_device, query_series, list_alerts, list_dashboards, list_profiles, list_notification_channels, list_zones, plot_series, get_profile, list_guide_pages, read_guide, and for editors create_device, create_alert_rule, create_profile, update_profile, create_zone, set_device_attributes)
text text a piece of the answer (Markdown), to append to the previous ones
chart chart a series to plot: { deviceId, deviceName, channels: [{ key, label, unit }], from, to, interval }, to load with GET …/telemetry
action action an action proposed for confirmation (see Actions)
done status last line: the ai/status object, with the updated quota
error error the answer failed after it started (error code, e.g. ai_error); the stream ends
{"type":"conversation","id":"5b8e0f6a-…"}
{"type":"tool","name":"get_device"}
{"type":"text","text":"The last temperature of Pont is "}
{"type":"text","text":"21.4 °C (2 minutes ago)."}
{"type":"done","status":{"enabled":true,"reason":null,"requests":{"used":13,"limit":200},"tokens":{"used":191870,"limit":2000000}}}

One question at a time per conversation. Closing the connection stops the answer; what was written so far is stored.

GET /orgs/{orgId}/ai/conversations/{id} — author

A stored conversation as its author saw it (another user's, unknown or malformed id: 404). Conversations are deleted 30 days after the last message.

{
  "id": "5b8e0f6a-…",
  "messages": [
    { "role": "user", "text": "What is the last value here?", "charts": [], "actions": [] },
    { "role": "assistant", "text": "The last temperature of Pont is 21.4 °C (2 minutes ago).", "charts": [], "actions": [] }
  ]
}

Actions

Editors and above can ask the assistant to create a device, an alert rule, a device profile or a zone, to change a profile, or to set a device's attributes. The assistant never writes: it streams an action event and waits for the user.

{
  "id": "a1b2c3d4-…",
  "status": "pending",
  "summary": {
    "kind": "create_alert_rule", "name": "Cellar too hot",
    "target": { "type": "device", "id": "0c7f2d1e-…", "name": "Cellar" },
    "channel": { "key": "temperature", "label": "Temperature", "unit": "°C" },
    "condition": { "kind": "above", "value": 30 }, "durationSeconds": 600, "severity": "warning",
    "notificationChannels": [{ "id": "d05c4ebc-…", "name": "On call" }]
  },
  "expiresAt": "2026-11-13T10:15:00.000Z",
  "result": null,
  "error": null
}

summary.kind is create_device (name, deviceKind, profile, attributes, location, devEui, sigfoxId) or create_alert_rule (above; for a selector target, target is { "type": "selector", "id": null, "name", "selectorKind", "deviceCount" }: name is the profile name, key = value or the device names, deviceCount the devices matched now). status: pending, applying, applied, failed, cancelled, expired. A pending action expires 15 minutes after it was proposed.

For a profile, summary.kind is create_profile or update_profile, and result holds profileId once applied:

{
  "kind": "update_profile",
  "profile": { "id": "9f2c41aa-…", "name": "Temperature" },
  "deviceCount": 12,
  "changes": [{ "field": "name", "before": "Temperature", "after": "Cellar temperature" }],
  "channels": {
    "added": [{ "key": "humidity", "label": "Humidity", "unit": "%", "type": "number", "min": null, "max": null }],
    "removed": [{ "key": "battery", "label": "Battery", "unit": "V", "type": "number", "min": null, "max": null, "hasData": true }],
    "modified": [{ "key": "temperature", "before": { "key": "temperature", "unit": "°C", … }, "after": { "key": "temperature", "unit": "°F", … } }]
  },
  "samples": []
}
  • create_profile: profile (name, channels, decoder, timestampKey, topicTemplate, locationFromChannels, networkLocationFallback, as saved by POST /profiles) and samples.
  • update_profile: changes lists the changed fields only (name, decoder, transform, timestampKey, topicTemplate, locationFromChannels, networkLocationFallback); channels the channels added, removed and modified (compared by key); deviceCount the devices using the profile; hasData: a device of the profile holds measurements on that channel (they are kept; the channel only loses its definition).
  • A new transform (written, or transformTemplate: geofence-attribute, dew-point, power-ui, moved) is tried in the sandbox; the channels it produces are added to the profile when missing (a template's declared outputs, e.g. zone.inside of type bool, or the keys the trial run returned) and appear in channels.added.
  • samples: [{ payloadHex, result }], the decoder run in the sandbox on the example frames the model passed (as ai/propose/decoder below); empty without a decoder change or without examples. A decoder failing on one example never gets a card: the model receives the error.

At confirmation a profile deleted in the meantime gives status: "failed" with error: "profile_not_found", and a profile changed in the meantime error: "profile_changed"; nothing is written. Audit entries: profile.create or profile.update (with the changed field names), plus assistant.action_applied.

For a zone, summary is { "kind": "create_zone", "name", "zoneKind": "circle" | "polygon", "center": [lng, lat] | null, "radiusM": number | null, "pointCount": number | null, "forbidden", "color" }, validated as POST /zones; result holds zoneId once applied. A name taken or the zone limit reached at confirmation gives status: "failed" with zone_name_taken or zone_limit. Audit: zone.create.

For a device's attributes, summary is { "kind": "set_device_attributes", "device": { "id", "name" }, "before": {…}, "after": {…} } (all attributes, before and after; the change is merged into the existing ones). Attributes changed since the card was shown give failed with device_changed. Audit: device.update [attributes]. Gateways are refused.

POST /orgs/{orgId}/ai/conversations/{id}/actions/{actionId}/confirm — editor, author

Applies the action once, through the same services, checks and audit entries as the Devices, Alerts and Profiles pages (plus assistant.action_applied). The role is checked again at this moment.

{ "action": { "id": "a1b2c3d4-…", "status": "applied", "summary": { … }, "expiresAt": "2026-11-13T10:15:00.000Z", "result": { "ruleId": "6e1f2a90-…" }, "error": null } }

For a device, the answer also has device: { "device": {…}, "token": "…", "ingestUrl": "…", "mqtt": {…} }, as POST /devices. The token is in this answer only.

A business error (device quota, devEUI already taken…) gives 200 with status: "failed" and the code in error; a device whose settings then fail is deleted again (if that deletion fails too, result keeps its id and error ends with ; device_not_deleted). 409 action_not_pending if the action was already confirmed, cancelled or is being applied; 409 action_expired after 15 minutes.

POST /orgs/{orgId}/ai/conversations/{id}/actions/{actionId}/cancel — member, author

pending → cancelled; returns the action. 409 action_not_pending or 409 action_expired otherwise.

Proposals

Assisted configuration for editors and above. The model returns a proposal that the API validates with the schemas of the create routes; nothing is saved: the client shows it, the user reviews it and applies it with the usual endpoints. Each proposal counts as one request and is audited as assistant.propose_decoder, assistant.propose_transform, assistant.propose_rule or assistant.propose_dashboard. A proposal the API cannot validate gives 400 ai_invalid_proposal: <reason>.

POST /orgs/{orgId}/ai/propose/decoder

{ "description": "4 bytes: int16 BE temperature in 0.01 °C, then uint16 battery in mV", "protocol": "lorawan", "examples": ["0929 0E10"], "fPort": 2 }

description: 1 to 8,000 characters; protocol: lorawan, sigfox, mqtt or http; examples: up to 10 frames, hexadecimal or base64 (400 invalid_payload otherwise); fPort: optional, 0 to 255.

{
  "code": "function decode(bytes, fPort) { … }",
  "channels": [{ "key": "temperature", "label": "Temperature", "unit": "°C", "type": "number", "min": null, "max": null }],
  "explanation": "…",
  "samples": [{ "payloadHex": "09290e10", "result": { "ok": true, "output": { "temperature": 23.45, "battery": 3600 }, "points": [ … ] } }],
  "retried": false
}

Each example frame is run in the real decoder sandbox (as POST /decoders/test); a failed sample has { "ok": false, "error": "…" }. retried: the first decoder failed on an example and the model was asked once to fix it.

POST /orgs/{orgId}/ai/propose/transform

{ "description": "compute the dew point", "values": { "temperature": 20, "humidity": 50 }, "deviceId": null }

description: 1 to 2,000 characters; values: sample values (optional); deviceId: a device of the organization whose name and attributes are given to the model and used for the trial run (optional). The model also gets the organization's zone names.

{ "code": "function transform(values, ctx) { … }", "explanation": "…", "result": { "ok": true, "extra": { "dewPoint": 9.3 }, "points": [ … ] }, "retried": false }

The transform is run on the sample values as POST /transforms/test; result is null without sample values. retried: the first transform failed and the model was asked once to fix it.

POST /orgs/{orgId}/ai/propose/rule

{ "description": "Alert me when the Pont temperature is above 30 °C for 10 minutes" }
{ "explanation": "…", "rule": { "name": "Pont too hot", "target": { "type": "device", "deviceId": "0c7f2d1e-…" }, "channelKey": "temperature", "condition": { "kind": "above", "value": 30 }, "durationSeconds": 600, "severity": "warning", "enabled": true, "channelIds": [], "remindEveryHours": null } }

rule follows the alert rule format, with no notification channel; it is null when the rule engine cannot express the request (the explanation says why).

POST /orgs/{orgId}/ai/propose/dashboard

{ "deviceId": "0c7f2d1e-…", "useProfile": false, "description": "A gauge for the battery, temperature over 7 days" }

useProfile: a dashboard for every device of the device's profile, with the $device variable (400 device_has_no_profile if it has none); description: optional, up to 2,000 characters.

{ "explanation": "…", "dashboard": { "name": "Pont", "widgets": [ … ], "variables": [] } }

dashboard is a valid body for POST /dashboards.

Enabling per organization

Instance administrators only: PATCH /admin/orgs/{orgId} with { "aiEnabled": true } (or false), audited in the organization as org.ai_enabled / org.ai_disabled. See Admin.