> ## Documentation Index
> Fetch the complete documentation index at: https://docs.widgetic.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Canvases

> Work with widget editing canvases

Canvases are the editing environment where a widget is designed and configured. Each canvas represents an editing session with its own state, and it holds one widget.

## Overview

With the Canvases API, you can:

* Create new canvases
* List existing canvases
* Retrieve canvas details
* Update canvas content
* Delete canvases

## List Canvases

Retrieve all canvases with pagination and filtering:

```typescript theme={"theme":"css-variables"}
import { CanvasesApi } from '@widgetic/api-sdk-ts';

const canvases = new CanvasesApi('YOUR_API_KEY');

const { data: list } = await canvases.getAllCanvases({
  page: 1,
  size: 20,
  sort: 'created_at:desc'
});

console.log(`Found ${list.total} canvases, showing ${list.data?.length ?? 0}`);
```

The canvases are in `data.data` of the answer, next to `page`, `size` and `total`.

## Filtering Canvases

Filter canvases by various criteria:

```typescript theme={"theme":"css-variables"}
const filtered = await canvases.getAllCanvases({
  createdAfter: new Date('2024-01-01T00:00:00Z'),
  createdBefore: new Date('2024-12-31T23:59:59Z'),
  updatedAfter: new Date('2024-06-01T00:00:00Z')
});
```

You can also filter by `updatedBefore`. The list cannot be filtered by persistence key.

## Get Canvas by ID

Retrieve a specific canvas:

```typescript theme={"theme":"css-variables"}
const { data: canvas } = await canvases.getCanvas({
  id: '123e4567-e89b-12d3-a456-426614174000'
});

console.log('Canvas:', canvas.persistenceKey);
console.log('Content:', canvas.canvasContent);
```

## Create Canvas

Create a new canvas:

```typescript theme={"theme":"css-variables"}
const { data: newCanvas } = await canvases.createCanvas({
  createCanvasRequest: {
    persistenceKey: 'my-widget-canvas-001',
    canvasContent: {
      version: 1,
      elements: [],
      settings: {
        background: '#ffffff',
        gridSize: 10
      }
    }
  }
});

console.log('Created canvas:', newCanvas.id);
```

## One Widget to a Canvas

A canvas holds one widget at a time. A new canvas is empty. Its widget is made with [`POST /widgets`](https://docs.widgetic.com/docs/api/widgets#create-a-widget), naming the canvas in `canvasId`; a widget made without a canvas gets a canvas of its own. Naming a canvas that already has a widget is refused with `409` and the code `CANVAS_ALREADY_HAS_WIDGET`, and nothing is created (see [Where a Widget Can Go](https://docs.widgetic.com/docs/api/widgets.md#where-a-widget-can-go)). `primaryWidgetId` on the canvas is the widget it holds, or `null` while it has none.

## Update Canvas

Update an existing canvas:

```typescript theme={"theme":"css-variables"}
const updated = await canvases.updateCanvas({
  id: '123e4567-e89b-12d3-a456-426614174000',
  updateCanvasRequest: {
    canvasContent: {
      version: 2,
      elements: [
        { id: 'elem1', type: 'text', content: 'Hello' }
      ],
      settings: {
        background: '#f0f0f0',
        gridSize: 10
      }
    }
  }
});
```

## Delete Canvas

Delete a canvas. Its widget is deleted with it:

```typescript theme={"theme":"css-variables"}
await canvases.deleteCanvas({
  id: '123e4567-e89b-12d3-a456-426614174000'
});
```

## Canvas Structure

A canvas in an answer carries these fields:

| Field | Type | What it holds |
| - | - | - |
| `id` | string | The canvas's identifier |
| `persistenceKey` | string | Your reference key (see [Persistence Keys](#persistence-keys)) |
| `canvasContent` | object | The canvas state, JSON of your choosing (see [Canvas Content](#canvas-content)) |
| `primaryWidgetId` | string or `null` | The widget on this canvas (a canvas holds one), or `null` while it has none |
| `managedUserId` | string or `null` | The managed user the canvas was made for (partners only), or `null` when it is your own. A canvas follows its widget when you [move the widget](https://docs.widgetic.com/docs/partners/managed-users.md#moving-content-to-another-managed-user) to another customer |
| `createdAt` | string | When the canvas was created (an ISO timestamp) |
| `updatedAt` | string | When the canvas was last changed (an ISO timestamp) |

An answer can carry other fields. They are for Widgetic's own apps and can change without notice, so read only the ones above.

## Canvas Content

The `canvasContent` field stores the canvas state. It holds JSON of your choosing: the API saves what you send and does not check its shape. The examples above use this shape:

```json theme={"theme":"css-variables"}
{
  "version": 1,
  "elements": [
    { "id": "elem1", "type": "text", "content": "Hello" }
  ],
  "settings": { "background": "#ffffff", "gridSize": 10 }
}
```

An update replaces `canvasContent` as a whole, so send all of it.

## Persistence Keys

Use persistence keys to maintain references between your application and Widgetic:

```typescript theme={"theme":"css-variables"}
// Your own ids, and your own storage code (saveProjectRecord stands for it)
const userId = 'user_42';
const projectId = 'project_7';
async function saveProjectRecord(record: { projectId: string; widgeticCanvasId: string }): Promise<void> {
  // Write the record to your own database
}

// Create with a meaningful key
const { data: canvas } = await canvases.createCanvas({
  createCanvasRequest: {
    persistenceKey: `user_${userId}_project_${projectId}_canvas`,
    canvasContent: { version: 1, elements: [] }
  }
});

// Keep the id the API returns next to your own record
await saveProjectRecord({ projectId, widgeticCanvasId: canvas.id });
```

The list cannot be filtered by persistence key, so keep the `id` that creation returns in your own records.

## Related Endpoints

| Endpoint | Description |
| - | - |
| `GET /canvases` | List all canvases |
| `GET /canvases/{id}` | Get canvas by ID |
| `POST /canvases` | Create a new canvas |
| `PUT /canvases/{id}` | Update a canvas |
| `DELETE /canvases/{id}` | Delete a canvas |

## Next Steps

> [card] [Widgets](https://docs.widgetic.com/docs/api/widgets.md)
>
> Make the widget of a canvas

> [card] [Compositions](https://docs.widgetic.com/docs/api/compositions.md)
>
> Set a widget up for one place and embed it


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.