Skip to content
中文

Create your first canvas

Create a canvas, draw a frame with a title, and save it as PNG. Run the complete program below in an agent’s codemode environment.

Prepare access

You need a codemode environment with Praxis access configured for the target organization and write access to /tmp on the computer running the program. The SDK uses the current caller’s identity; do not put an API key inside the canvas script. For direct HTTP integration, use the organization’s Praxis API key with Bearer authentication as described in API authentication.

Create, draw and save

Run this program once. It performs three actions in order: canvases.create creates a canvas, canvases.execute submits a synchronous canvas script, and canvases.images.list fetches PNG output. The outer program handles networking and file writes. Inside script, use only canvas functions: no module imports, network or file APIs.

import { praxis } from "praxis";
import { writeFile } from "node:fs/promises";
export default async () => {
const canvas = await praxis.canvas.canvases.create({ body: { name: "First canvas" } });
const script = String.raw`hero = Insert(document, {
type: "frame", name: "Hero", width: 480, height: 160,
layout: "vertical", padding: 24, gap: 12, fill: "#FFFFFF"
});
Insert(hero, {
type: "text", name: "Title", content: "Hello, Canvas",
fontFamily: "Geist", fontSize: 32, fontWeight: 600, fill: "#111111"
});
Insert(hero, {
type: "text", name: "Subtitle", content: "Your first design",
fontFamily: "Geist", fontSize: 16, fill: "#666666"
});
Get(hero, (node, ctx) => Print(node.name, ctx.bounds));
TakeScreenshot([hero]);`;
const result = await praxis.canvas.canvases.execute({
path: { canvas_id: canvas.id },
body: { script, expected_version: canvas.version }
});
const images = await praxis.canvas.canvases.images.list({
path: { canvas_id: canvas.id },
query: { ids: result.created.Hero, format: "png", scale: 1 }
});
const image = images.images[result.created.Hero];
if (!image) throw new Error("Hero image is unavailable");
await writeFile("/tmp/first-canvas.png", Buffer.from(image.split(",")[1], "base64"));
return { canvas_id: canvas.id, viewer_url: result.viewer_url, file: "/tmp/first-canvas.png", prints: result.prints, warnings: result.warnings };
};

Verify the result

Open the returned viewer_url to view the same canvas. Hero measures 480 × 160 and contains Title and Subtitle text nodes. prints includes bounds for all three nodes; warnings should be empty. file is a PNG path on the computer running the program: in codemode, that is the agent’s computer, not the viewer’s local computer.

created.Hero is the node ID associated with name: "Hero" at creation; it differs from canvas.id. The ids argument to images.list is a comma-separated node ID string, with at most four IDs per call. images maps each node ID to a data URL; missing nodes return null.

Recover from failure

REVISION_CONFLICT means the canvas changed after you read it. Retrieve it again, inspect the new nodes and version, then adjust and submit your script. If a script fails, this call’s nodes are not retained. Repair it using error.details.edit_id or send corrected new source. Do not rerun the creation program just because image retrieval failed: that creates another canvas.

Continue with the node reference and script functions.