Action widgets API
An action widget is a canvas widget that carries triggers. A trigger is a touch or an event on the widget, and each trigger holds a list of steps that run other actions on other widgets or on the canvas. Through the REST API you can read and write the triggers of a widget and fire a trigger without touching the widget.
For what action widgets are and how people build them on a canvas, see Action widgets in the Canvus manual.
Canvus Server 26.11.5 and later
Triggers need Canvus Server 26.11.5 or later. On an older server the triggers field is left out of responses, and a request that sets a non-empty list is refused with 501 Not Implemented.
Authentication
All endpoints require authentication via the Private-Token header or a CanvusSession cookie. Writing triggers and firing a trigger both need edit access to the canvas. View-only users receive 403 Forbidden.
The triggers field
Every widget except connectors and the canvas background has a triggers field: notes, images, browsers, videos, PDFs, anchors, tables, IP videos, RDP connections and video input widgets. It is a list, empty when the widget has no triggers. You can set it on create (POST) and change it with PATCH. It is returned by GET on the type-specific endpoints and on Widgets (generic). Annotations, uploads, connectors and the background do not have it; a request that sets it on one of them is refused with 400 Bad Request.
"triggers": [
{
"type": "tap",
"steps": [
{ "action": "pin", "params": {}, "targets": ["0d5d2a8c-7a3a-4f6e-9a3c-2f1b7d1f6c11"] }
]
},
{ "type": "reached-timecode", "value": 12.5, "steps": [] }
]
Trigger
type-- string, required. One of the trigger types below.value-- number. Required forreached-timecode,loopedandpage-shown, and not allowed on any other type.steps-- list of steps, possibly empty.
A widget cannot have two triggers with the same type and value. A trigger has no other keys.
type |
Fires when | value |
|---|---|---|
tap |
A finger taps the widget | |
hold |
A finger presses and holds on the widget | |
double-tap |
A finger taps the widget twice in quick succession | |
annotated |
A pen stroke is completed on the widget | |
maximised |
The widget enters the single-widget full-screen view | |
minimised |
The widget leaves the single-widget full-screen view | |
played |
A video starts playing | |
paused |
A video is paused | |
ended |
A video reaches its end (does not fire while looping is on) | |
reached-timecode |
A video playback reaches a time | Seconds, 0 or more |
looped |
A video has looped a number of times | Whole number from 1 to 2147483647 |
page-turned-forward |
A PDF turns to a later page | |
page-turned-back |
A PDF turns to an earlier page | |
page-shown |
A PDF shows a page | Page number, whole number from 1 to 2147483647 |
The server accepts and fires every type. Clients raise them as follows. A touch trigger (tap, hold, double-tap) is seen by the client that was touched. annotated, maximised and minimised are seen by the client where they happen. The video and PDF triggers are seen by every client with the canvas open. Nothing fires for the state a widget is in when it is first watched. For PDFs, the server counts page changes so that clients that joined at different times agree on one firing.
Step
action-- string, required, not empty. The action name.params-- object, optional, default{}. The settings of the action.targets-- list of widget IDs, optional, default[]. A widget ID is the widget'sidin the REST API.
A step has no other keys. Actions that need targets must name at least one.
Actions
A step's action is one of the names below. The server runs a canvas action once when the trigger fires. A view action is left to the clients, which each run it on their own screen.
action |
Kind | Targets | params |
|---|---|---|---|
pin |
canvas | one or more | none |
unpin |
canvas | one or more | none |
bring-to-front |
canvas | one or more | none. Each target goes above the other children of its parent, with the depth a touch gives it (highest sibling depth + 0.1). Several targets keep their current order; a target already on top is not written. |
focus |
view | one or more | none |
zoom |
view | one or more | margin, optional: number from 0 to 0.45. The part of the view left free on each side of the targets. Default 0, zoom until the targets meet an edge of the view. |
maximise |
view | one or more | none |
exit-maximised |
view | none | none |
zoom-to-anchor |
view | none | anchor: the ID of the anchor widget |
zoom-to-all |
view | none | none |
open-canvas |
view | none | canvas: the canvas ID. Optional: server, name, confirm (boolean, default false) |
note-background-color |
canvas | notes | color: #rrggbb or #rrggbbaa |
note-text-color |
canvas | notes | color. Also turns auto-text-color off. |
note-font |
canvas | notes | family. An empty string is the Default font. |
video-play |
canvas | videos | none |
video-pause |
canvas | videos | none |
video-loop |
canvas | videos | on, boolean, optional |
video-mute |
canvas | videos | on, boolean, optional |
video-seek |
canvas | videos | time: seconds |
browser-bookmark |
canvas | browsers | url: an http or https address. Optional: title. |
browser-transparent |
canvas | browsers | on, boolean, optional |
model-background-color |
canvas | 3D models | color |
pdf-page |
canvas | PDFs | page: from 1 |
background-color |
canvas | none | color. Also makes the background a plain colour. |
background-haze |
canvas | none | none |
background-grid |
canvas | none | on, boolean, optional |
reset-demo |
canvas | none | none. Only on a demo canvas; otherwise the server logs a warning and does nothing. |
save-copy |
canvas | none | none. The copy is named after the canvas with the server's local date and time. |
broadcast |
view | one or more, any type | output: the name of a video output |
stop-broadcast |
view | none | output |
browser-refresh |
view | browsers | none |
model-auto-rotate |
view | 3D models | on, boolean, optional |
table-zoom-to-cell |
view | tables | cell, such as B3: column B, row 3 |
pin-canvas |
view | none | on, boolean, optional |
start-anchor-presentation |
view | none | none |
start-presentation |
view | none | presentation: the presentation's ID. Optional: name. |
stop-presentation |
view | none | none. Leaves presentation mode on the client's screen, as the presentation's own exit does. |
hide-interface |
view | none | parts: a list with at least one of title, sidebar, mainmenu, finger, resizer, each at most once |
show-interface |
view | none | none |
A switch with on set to true turns on, false turns off, and without on it toggles what the target has now. The server checks every step before it runs it. A step with params the action does not understand, or a target of a type the action does not apply to, is skipped with a warning that names the widget and the action. The other steps run. A part name in hide-interface that this version does not know refuses the whole step in a client.
open-canvas settings:
server-- the address of another server, such ashttps://host,ssl://host:portortcp://host:port. Without it, the canvas is on the server of the canvas that holds the action widget.name-- the canvas's name when the step was defined. A client shows it in the confirmation dialog only when it cannot look the name up itself.confirm-- whentrue, the client shows a confirmation dialog before it loads the canvas.
Widget IDs, canvas IDs and name can be up to 200 characters. server can be up to 300.
A client that does not understand a step's action or params skips the step and writes one warning to its log. The server accepts and keeps action names it does not know, so a definition from a newer version survives an older server or editor.
Limits and validation
- At most 32 triggers per widget, 32 steps per trigger and 64 targets per step.
- At most 64 KiB for the whole
triggersvalue. - Targets are widget IDs. When a widget is cloned through the API, a target that names the source widget names the clone, and other targets are kept.
The server validates the whole definition before it writes anything. A request with an invalid triggers, in a POST or a PATCH, changes nothing: no other field in the request is applied and no widget is created. The msg of the 400 Bad Request names the problem and its place:
| Problem | msg |
|---|---|
Unknown type |
triggers[0].type: unknown trigger type 'swipe' |
| Missing value | triggers[0].value: required for 'looped' |
| Value on a type that takes none | triggers[0].value: not allowed for 'tap' |
| Same type and value twice | triggers[2]: same type and value as triggers[0] (tap) |
| Too many triggers | triggers: more than 32 triggers |
| Too many targets | triggers[0].steps[0].targets: more than 64 targets |
| A canvas action without a target | triggers[0].steps[0].targets: 'pin' needs at least one target |
| Unknown key | triggers[0]: unknown key 'enabled' |
| Not a list | triggers: must be a list |
A trigger with a type that this server does not know is refused in a request. The exception is sending back what a GET returned: a trigger that a newer version wrote and that is unchanged is accepted and kept. You can also move it or leave it out.
Write triggers
Send triggers in a create request or in a PATCH.
PATCH /api/v1/canvases/:canvas_id/images/:image_id
curl -X PATCH -H "Private-Token: YOUR_TOKEN" -H "Content-Type: application/json" \
-d '{"triggers": [{"type": "tap", "steps": [{"action": "zoom", "params": {"margin": 0.1}, "targets": ["TARGET_ID"]}]}]}' \
https://canvus.example.com/api/v1/canvases/CANVAS_ID/images/IMAGE_ID
- A PATCH that includes
triggersreplaces the whole list. Nothing is merged. Send an empty list to clear the triggers. - A PATCH without
triggersleaves them as they are. - The response shows the widget with its
triggersin the canonical form: every step hasaction,paramsandtargets, and numbers are written in their shortest form (12.50is returned as12.5). - A PATCH never fires a trigger. A PATCH that says
"action": "fire_trigger"in its body or its query string is refused with400 Bad Request, and nothing in it is applied. Use the fire call. - A clone (
source_widget_idandsource_canvas_idin a create request) takes the triggers of its source widget. A clone request that also hastriggersis refused with400 Bad Request.
Triggers are saved with the widget. They survive a restart and travel with a copy of the canvas.
Fire a trigger
Fires one trigger of an action widget, as a touch or an event would.
POST /api/v1/canvases/:canvas_id/:widget_type/:widget_id/fire
Path parameters:
canvas_id(uuid, required) -- ID of the canvaswidget_type(string, required) -- The widget's collection name:notes,images,browsers,videos,pdfs,anchors,video-inputs,ip-videos,rdp-connectionsortables. It must match the widget's type.widget_id(uuid, required) -- ID of the action widget
Body parameters:
trigger(string, required) -- The trigger key: the trigger'stype, ortype:valuefor a trigger with a value, such astaporreached-timecode:12.5. A value may be written in any form that means the same number:reached-timecode:12.50is accepted and answered asreached-timecode:12.5.
Example request:
curl -X POST -H "Private-Token: YOUR_TOKEN" -H "Content-Type: application/json" \
-d '{"trigger": "tap"}' \
https://canvus.example.com/api/v1/canvases/CANVAS_ID/images/IMAGE_ID/fire
Example response:
{
"widget": "0d5d2a8c-7a3a-4f6e-9a3c-2f1b7d1f6c11",
"trigger": "tap",
"occurrence": "5b2f7c0e-8d1a-4a53-b6c2-0e9a3f1d7a44"
}
occurrence identifies this firing. Each firing has its own.
What a firing does
The call obeys the same rules as a touch:
- The widget must be pinned. An unpinned widget is not armed, and nothing fires.
- The widget must have the trigger. The call fires the stored definition. The request carries no actions.
- The same trigger on the same widget fires at most once a second. The interval is a server setting,
[action-widgets] minimum-fire-intervalinmt-canvus-server.ini, one second by default (0turns the limit off). A trigger that fired less than the interval ago is refused. The limit is per trigger, so another trigger of the same widget can fire at once. A refused call does not count against the limit. - The server runs the trigger's canvas actions once. Every client that has the canvas open runs the trigger's view actions on its own screen, unless that client has turned off follow action widgets.
- The
200 OKmeans the server accepted the firing. It does not wait for the actions to finish and says nothing about whether a client ran its view actions.
Firing needs edit access to the canvas, as writing the widget does. Anyone who can see the canvas can fire a trigger by touch, but through the API a view-only user is refused.
Response codes
| Status | Meaning |
|---|---|
200 OK |
The server accepted the firing. The body has widget, trigger and occurrence. |
400 Bad Request |
trigger is missing or is not a string (trigger is mandatory: ...). trigger is not a trigger key (trigger: 'swipe' is not a trigger key: unknown trigger type 'swipe'), for example a type with a value and no value ('looped' needs a value, as in 'looped:1'). The widget_type in the path is not a widget type that can carry triggers, such as connectors or widgets. |
403 Forbidden |
The caller can only view the canvas: View-only users cannot fire triggers. |
404 Not Found |
The canvas or widget does not exist, or the widget is not of the type in the path (Widget ... type is Note not Image). Or the widget has no such trigger (Widget '...' has no trigger 'hold'). |
409 Conflict |
The widget is not pinned: Widget '...' is not pinned, so its triggers do not fire. |
429 Too Many Requests |
The trigger fired less than the minimum interval ago: Trigger 'tap' on widget '...' fired less than 1 s ago. Try again after the interval. |
501 Not Implemented |
Returned by a create or PATCH request, not by the fire call: the request sets a non-empty triggers and the server is older than 26.11.5 and has nowhere to store it. |
503 Service Unavailable |
The server cannot fire the trigger now: the canvas is not loaded on the server, or the server does not run action widgets, or the firing failed inside the server. |
All errors have the usual body, {"msg": "..."}.
Example: define and fire a trigger from a script
This script arms a widget, gives it a tap trigger that pins a target, and fires the trigger. It suits a test or an installation check.
# 1. Pin the action widget, which arms it
curl -X PATCH -H "Private-Token: $TOKEN" -d '{"pinned": true}' \
"$BASE/canvases/$CANVAS/images/$ACTION_WIDGET"
# 2. Define the trigger
curl -X PATCH -H "Private-Token: $TOKEN" -H "Content-Type: application/json" \
-d "{\"triggers\": [{\"type\": \"tap\", \"steps\": [{\"action\": \"pin\", \"targets\": [\"$TARGET\"]}]}]}" \
"$BASE/canvases/$CANVAS/images/$ACTION_WIDGET"
# 3. Fire it. The target is pinned once, on the server.
curl -X POST -H "Private-Token: $TOKEN" -H "Content-Type: application/json" \
-d '{"trigger": "tap"}' \
"$BASE/canvases/$CANVAS/images/$ACTION_WIDGET/fire"
Local canvases
The REST API does not serve local canvases, so neither triggers nor the fire call are available for them.