Commands
Commands let you invoke an operation on a connected device in real time. The cloud sends a request, the device executes it, and returns a response. The platform waits 10 seconds for the response.
How It Works
Commands use a request-response pattern over MQTT with correlation IDs to match responses to requests:
Invoking a Command (Cloud Side)
Commands are invoked through the Beacon Tower asset API. The request body is the command
payload: it is forwarded to the device unchanged as the payload field of the invocation.
curl -X POST https://api.beacontower.ai/assets/temp-sensor-001/commands/reboot \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"force": true
}'
Send a JSON object, or no body. A scalar or array body reaches the device as "payload": null.
There is no timeout parameter. The platform waits 10 seconds for the device to respond. A field
such as "timeout" in the body is not interpreted; it is passed to the device as part of the
payload.
Asset API result
The call returns 200 with the command's status once the device has answered or the wait has
ended. The status is derived from the device's statusCode (see
Publishing the Response):
| Outcome | status |
|---|---|
Device answered 200 | Completed |
Device answered 202 | Pending |
Device answered any other code (including 201 and 204) | Failed |
| Device not connected, did not answer within 10 seconds, or the provider failed | Failed |
responsePayload carries the payload field of the device's response, or null when the response has none.
While a command is Pending or Running, a new call for the same command on the same asset is
not sent to the device: it returns 200 with the status of the command already in progress. A
command left Pending by a 202 answer stays Pending, so later calls for that command keep
returning it.
Other responses:
| Status | When | Body |
|---|---|---|
400 | Invalid asset id or payload | Plain text |
404 | Asset not found, or its command is not initialised | Empty |
503 | The asset processor is unavailable | Plain text Asset processor unavailable |
500 | Any other failure, including an internal timeout | Empty |
A Failed status does not tell you whether the device received the command. Before you retry,
check whether running the command twice is safe.
Provider route
Below the asset API, the provider-facing endpoint is spelled
POST /providers/{providerId}/clients/{providerClientId}/directMethod, taking a
RunDirectMethodRequest and returning a RunDirectMethodResult. Those names are unchanged — use
them verbatim if you call that surface directly.
{ "method": "reboot", "componentId": null, "payload": { "force": true } }
method is required; componentId and payload are optional.
For providers built on the Provider SDK (including the MQTT provider):
| Status | When | Body |
|---|---|---|
200 | The device answered, whatever its statusCode | { "status": <device statusCode>, "payload": <device response>, "errorMessage": null } |
400 | method missing | Validation problem details |
403 | No permission on the provider client | Plain text |
404 | Provider or provider client not found | Plain text |
409 | Provider client not provisioned | Plain text |
500 | Device not connected, no answer within 10 seconds, or any provider failure | Plain text error message |
A 200 means the device answered; read status for the device's own result. Device offline,
timeout and transport failures are not distinguished: all three return 500.
Handling Commands (Device Side)
Subscribe to Command Invocations
The device subscribes to its command invocation topic using a wildcard:
device/{deviceId}/command/+/invoke (QoS 1)
The + wildcard matches any command name, so the device receives all command invocations on a single subscription.
Invocation Payload
When a command is invoked, the device receives a message on device/{deviceId}/command/{commandName}/invoke (QoS 1, not retained):
{
"correlationId": "a1b2c3d4e5f67890abcdef1234567890",
"payload": {
"force": true
}
}
| Field | Description |
|---|---|
correlationId | Identifies this invocation. 32 lowercase hexadecimal characters (a GUID without dashes), new for every invocation. Use it in the response topic. |
payload | The command payload from the caller: a JSON object, or null |
There is no responseTopic field. The device builds the response topic itself, as below.
Publishing the Response
After executing the command, the device publishes the result to:
device/{deviceId}/command/{commandName}/response/{correlationId} (QoS 1)
{commandName} is the name from the invoke topic and {correlationId} is the value from the
invocation. The platform matches the response by the last topic segment; a response whose
correlationId is unknown or no longer awaited (for example, after the wait has ended) is
discarded.
The response is a JSON object with two fields, both optional:
| Field | Description |
|---|---|
statusCode | The command result, an integer from 200 to 599. 200 when absent. |
payload | The data returned to the caller: any JSON value (object, array, string, number, boolean or null). |
{
"statusCode": 200,
"payload": { "uptime_before_reboot": 86400 }
}
Only payload reaches the caller. Any other field in the response is dropped, so put everything the
caller needs inside payload.
| Response | Result |
|---|---|
JSON object with statusCode 200–299, or without statusCode | Success with that status; the caller receives payload |
JSON object with statusCode 300–599 | Device error: the status and payload are returned to the caller |
| Empty message | Status 200, no payload |
JSON object with a statusCode that is not an integer from 200 to 599 | The command fails (PayloadTypeMismatch) |
| A JSON scalar or array, or text that is not JSON | The command fails (PayloadTypeMismatch) |
A failed command reaches the caller as described in Timeouts and Errors.
Complete Device-Side Example
import paho.mqtt.client as mqtt
import json
DEVICE_ID = "temp-sensor-001"
def on_message(client, userdata, msg):
# Parse command name from topic
# Topic format: device/{id}/command/{name}/invoke
parts = msg.topic.split("/")
method_name = parts[3]
invocation = json.loads(msg.payload)
correlation_id = invocation["correlationId"]
payload = invocation.get("payload")
response_topic = f"device/{DEVICE_ID}/command/{method_name}/response/{correlation_id}"
# Execute the command
if method_name == "reboot":
result = {"statusCode": 200, "payload": {"message": "Rebooting in 5 seconds"}}
elif method_name == "get_diagnostics":
result = {"statusCode": 200, "payload": {"cpu_usage": 45.2, "memory_free": 1024}}
else:
result = {"statusCode": 404, "payload": {"message": f"Unknown method: {method_name}"}}
# Publish response
client.publish(response_topic, json.dumps(result), qos=1)
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id=DEVICE_ID)
client.username_pw_set(DEVICE_ID, "device-primary-key")
client.on_message = on_message
client.tls_set()
client.connect("mqtt.example.com", 8883, keepalive=120)
# Subscribe to all command invocations
client.subscribe(f"device/{DEVICE_ID}/command/+/invoke", qos=1)
client.loop_forever()
Timeouts and Errors
Timeout
The platform waits 10 seconds for the response. If the device does not answer in time, the asset
API returns 200 with status Failed, and the provider route returns 500 with a plain-text
message. A response that arrives later is discarded.
Device Offline
If the device is not connected to the broker when the command is invoked, the platform does not publish the invocation at all, so nothing is queued for the device. The caller sees the same result as a timeout, after the same 10 seconds.
Error Handling
Provider-side failures reach the caller as Failed (asset API) or 500 with a plain-text message
(provider route). There is no JSON error body and no machine-readable error code on either route.
Supported Commands
Commands are not predefined — any command name is valid. The command name is simply the string in the topic path. It is up to the device firmware to recognize and handle the commands it supports.
Common patterns:
| Command | Typical Purpose |
|---|---|
reboot | Restart the device |
get_diagnostics | Return diagnostic information |
set_config | Apply configuration changes |
update_firmware | Trigger firmware update |
ping | Connectivity check |
Next Steps
- MQTT Protocol Reference — Full topic and payload reference
- Device Twins — Manage device configuration via desired/reported properties
- Assets and Bindings — Invoking commands through the asset API