Execute canvas script
POST /canvas/v1/canvases/{canvas_id}:execute
Draw or edit a canvas atomically with a synchronous Pencil execute script. First create a canvas with praxis.canvas.canvases.create and use its id as canvas_id; inspect an existing canvas with retrieve before modifying it. Send exactly one of script (new source) or edit_id (a failed script to patch using edits). Unlike Figma REST, :execute writes design content; nodes use Pencil .pen schema 2.21 (frame/group/rectangle/ellipse/polygon/path/text/icon/ref/note), not Figma node fields.
The root variable is document. Synchronous functions are Insert / Copy / Update / Replace / Move / Delete / SetVariables / Get / GetVariables / FindEmptySpace / Print / TakeScreenshot; globals persist for the next call. A first frame needs only this body.script:
const f = Insert(document, { type: "frame", name: "Hero", width: 1200, layout: "vertical", padding: 48, fill: "#FFFFFF" }); Insert(f, { type: "text", content: "Hello", fontSize: 32, fill: "#111111" }); Print(f)Insert/Copy return the new node id; read the copy with Get(id) — copied descendants get new ids, so change them through Copy’s third argument descendants keyed by the source node ids. To copy a page and change its title in the same script:
const page = Insert(document, {type:"frame",name:"Original",width:320,height:160,layout:"vertical",padding:16});const title = Insert(page, {type:"text",name:"Title",content:"Original",fontSize:24});const copy = Copy(page, document, {name:"Copy",x:360,descendants:{[title]:{content:"Updated"}}});Print(copy); Print(Get(copy).children[0].content);Use viewer_url to watch the result in Console. TakeScreenshot records node ids in screenshots; call images.list with those ids to fetch PNG data URLs. See the Canvas execute reference for request fields and results, and the Canvas node reference for node content.
A successful write increments version by one. Optionally set expected_version to the version from retrieve or nodes.list; on 409 REVISION_CONFLICT read again, merge your intent and retry with the current version. Every script failure rolls back all writes. On 400 SCRIPT_ERROR, SCRIPT_LIMIT or OPS_REJECTED, use error.details.edit_id plus edits to repair and rerun the stored source without resending the entire script; details may include line, column and rejection. On 400 EDIT_EXPIRED the saved script is older than ten minutes: submit a corrected new script. On 429 CANVAS_BUSY, more than four calls are queued for this canvas: wait for pending calls before retrying.
Execution errors include error.details.prints containing output printed before failure, with the same string-array shape and output limit as successful prints; rolled-back writes still create no version.
Support: supported
Parameters
| Name | Location | Type | Required | Description |
|---|---|---|---|---|
| canvas_id | path | string | true | Canvas id returned by create or list, in the form cvs_ followed by 16 lowercase letters or digits; a display name is not accepted. |
Request body (application/json)
Required: true
| Name | Type | Description |
|---|---|---|
| script | string | New synchronous Pencil execute source using document as the root; provide this or edit_id, never both. See the operation’s first-frame example. |
| edit_id | string | Saved failed-script identifier from error.details.edit_id; use with edits instead of script within ten minutes of the failure. |
| edits | array<object> | Text replacements applied to the source identified by edit_id before rerunning; use error line, column or rejection to choose the correction. |
| edits[].find | string | Literal text to locate in the failed source saved under edit_id. |
| edits[].replace | string | Corrected text to substitute for the matched find text before execution. |
| edits[].all | boolean | Set true to replace every occurrence of find; omitted or false requires find to occur exactly once, and a missing or repeated match fails the retry without applying anything. |
| expected_version | string | Revision from retrieve, nodes.list or the previous execute response; omit for an unguarded write. A mismatch returns 409 REVISION_CONFLICT with details.version. |
{ "type": "object", "description": "Exactly one of script or edit_id must be supplied. Use edits with edit_id to repair a failed script stored for ten minutes.", "oneOf": [ { "required": [ "script" ], "not": { "required": [ "edit_id" ] } }, { "required": [ "edit_id" ], "not": { "required": [ "script" ] } } ], "properties": { "script": { "type": "string", "description": "New synchronous Pencil execute source using document as the root; provide this or edit_id, never both. See the operation's first-frame example." }, "edit_id": { "type": "string", "description": "Saved failed-script identifier from error.details.edit_id; use with edits instead of script within ten minutes of the failure." }, "edits": { "type": "array", "description": "Text replacements applied to the source identified by edit_id before rerunning; use error line, column or rejection to choose the correction.", "items": { "type": "object", "required": [ "find", "replace" ], "properties": { "find": { "type": "string", "description": "Literal text to locate in the failed source saved under edit_id." }, "replace": { "type": "string", "description": "Corrected text to substitute for the matched find text before execution." }, "all": { "type": "boolean", "default": false, "description": "Set true to replace every occurrence of find; omitted or false requires find to occur exactly once, and a missing or repeated match fails the retry without applying anything." } } } }, "expected_version": { "type": "string", "pattern": "^[0-9]+$", "example": "3", "description": "Revision from retrieve, nodes.list or the previous execute response; omit for an unguarded write. A mismatch returns 409 REVISION_CONFLICT with details.version." } }}Response
HTTP 200
Committed revision, operation results and viewer URL; screenshots contains node ids to pass to images.list.
application/json
| Name | Type | Description |
|---|---|---|
| version | string | Current revision as a decimal string, incremented by one after each successful write; pass it as expected_version to guard a subsequent execute call. |
| operation_count | integer | Number of design operations performed by the committed script. |
| created | object | Map from created result names to node identifiers; reuse the ids in nodes.list, images.list or subsequent scripts. |
| created_all | object | Every node created under each result name, in creation order. created keeps only the first id for a repeated name; use created_all when a script inserts several nodes with the same name. |
| prints | array<string> | String outputs produced by Print calls in script execution order. |
| warnings | array<object> | Non-fatal design warnings from this execution; writes have committed, so inspect the affected nodes before making another edit. |
| warnings[].code | string | Machine-readable warning category identifying what to inspect in the design. |
| warnings[].node_id | string | Identifier of the node to inspect with nodes.list or images.list. |
| warnings[].message | string | Human-readable explanation of the design issue and its context. |
| screenshots | array<string> | Node ids requested through TakeScreenshot; call images.list with up to four ids to obtain the actual PNG images. |
| vectorized | array<object> | One entry per Vectorize call the script made, in call order; empty when the script asked for no trace. Each entry is independent — an entry carrying error means that one image could not be traced and no group was written for it, while the script's other edits, and its other traces, still committed. Inspect the node named in group_id to review a traced picture. |
| vectorized[].request_id | string | Identifier the Vectorize call returned to the script, matching this answer to that call. |
| vectorized[].frame_id | string | Identifier of the frame the trace was aimed at. |
| vectorized[].group_id | string | Identifier of the group holding the traced picture, present only on success; fetch it with nodes.list to inspect or restyle the paths. |
| vectorized[].mode | string | How the image was traced; mono for line art, flat for a picture reduced to colour regions. |
| vectorized[].paths | integer | Number of path nodes written, one per colour region; present only on success. |
| vectorized[].error | object | Why this image was not traced; the rest of the call is unaffected. Take another image, crop it, or retry with different options according to the code. |
| touched_frames | array<string> | Identifiers of top-level frames affected by the script; render these frames to review the edit. |
| globals | array<string> | Global variable names still available for reuse in the next execute call on this canvas. |
| viewer_url | string | Console URL where a person can watch the canvas and inspect the committed visual result live. |