# @canvas-js/core

> This package exports a `Canvas` class that can be used to manually instantiate Canvas applications.

Latest version **0.16.1** (published 2025-07-30) · 0 weekly downloads

## Install

```sh
npm install @canvas-js/core
pnpm add @canvas-js/core
yarn add @canvas-js/core
bun add @canvas-js/core
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.16.1 |
| Published | 2025-07-30 |
| First published | 2022-05-16 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=22.0.0 |
| Dependencies | 43 |
| Unpacked size | 159.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Canvas Technologies, Inc. |
| Maintainers | joelg, raykyri, rjwebb |

## Links

- npm: https://www.npmjs.com/package/@canvas-js/core
- npm.io page: https://npm.io/package/@canvas-js/core

## Dependencies (43)

- [pg](https://npm.io/package/pg.md) ^8.15.6
- [ws](https://npm.io/package/ws.md) ^8.18.2
- [cors](https://npm.io/package/cors.md) ^2.8.5
- [chalk](https://npm.io/package/chalk.md) ^5.4.1
- [ethers](https://npm.io/package/ethers.md) ^6.13.5
- [esbuild](https://npm.io/package/esbuild.md) ^0.25.5
- [express](https://npm.io/package/express.md) ^5.1.0
- [p-defer](https://npm.io/package/p-defer.md) ^4.0.1
- [p-queue](https://npm.io/package/p-queue.md) ^8.1.0
- [@types/ws](https://npm.io/package/@types/ws.md) ^8.5.14
- [any-signal](https://npm.io/package/any-signal.md) ^4.1.1
- [@types/cors](https://npm.io/package/@types/cors.md) ^2.8.17
- [prom-client](https://npm.io/package/prom-client.md) ^15.1.2
- [uint8arrays](https://npm.io/package/uint8arrays.md) ^5.1.0
- [@ipld/schema](https://npm.io/package/@ipld/schema.md) ^6.0.6
- [esbuild-wasm](https://npm.io/package/esbuild-wasm.md) ^0.25.1
- [multiformats](https://npm.io/package/multiformats.md) ^13.3.7
- [prng-xoshiro](https://npm.io/package/prng-xoshiro.md) ^1.0.1
- [@canvas-js/vm](https://npm.io/package/@canvas-js/vm.md) 0.16.1
- [@noble/curves](https://npm.io/package/@noble/curves.md) ^1.9.2
- [@noble/hashes](https://npm.io/package/@noble/hashes.md) ^1.8.0
- [@ipld/dag-cbor](https://npm.io/package/@ipld/dag-cbor.md) ^9.2.4
- [@ipld/dag-json](https://npm.io/package/@ipld/dag-json.md) ^10.2.5
- [@libp2p/logger](https://npm.io/package/@libp2p/logger.md) ^5.1.13
- [@types/express](https://npm.io/package/@types/express.md) ^5.0.3
- [uint8arraylist](https://npm.io/package/uint8arraylist.md) ^2.4.8
- [@libp2p/peer-id](https://npm.io/package/@libp2p/peer-id.md) ^5.1.6
- [aggregate-error](https://npm.io/package/aggregate-error.md) ^5.0.0
- [@canvas-js/utils](https://npm.io/package/@canvas-js/utils.md) 1.0.0
- [@libp2p/interface](https://npm.io/package/@libp2p/interface.md) ^2.10.3
- [http-status-codes](https://npm.io/package/http-status-codes.md) ^2.3.0
- [@canvas-js/modeldb](https://npm.io/package/@canvas-js/modeldb.md) 0.16.1
- [abortable-iterator](https://npm.io/package/abortable-iterator.md) ^5.0.1
- [quickjs-emscripten](https://npm.io/package/quickjs-emscripten.md) ^0.31.0
- [@canvas-js/gossiplog](https://npm.io/package/@canvas-js/gossiplog.md) 0.16.1
- [@canvas-js/interfaces](https://npm.io/package/@canvas-js/interfaces.md) 0.16.1
- [@canvas-js/modeldb-pg](https://npm.io/package/@canvas-js/modeldb-pg.md) 0.16.1
- [@canvas-js/signatures](https://npm.io/package/@canvas-js/signatures.md) 0.16.1
- [@canvas-js/modeldb-idb](https://npm.io/package/@canvas-js/modeldb-idb.md) 0.16.1
- [@multiformats/multiaddr](https://npm.io/package/@multiformats/multiaddr.md) ^12.4.1
- [@canvas-js/modeldb-sqlite](https://npm.io/package/@canvas-js/modeldb-sqlite.md) 0.16.1
- [@canvas-js/signer-ethereum](https://npm.io/package/@canvas-js/signer-ethereum.md) 0.16.1
- [@canvas-js/modeldb-durable-objects](https://npm.io/package/@canvas-js/modeldb-durable-objects.md) 0.16.1

## Recent versions

- 0.16.1 (latest) — 2025-07-30
- 0.16.0-next.9 (next) — 2025-07-13
- 0.10.0-beta.3 (beta) — 2024-06-27
- 0.10.0-alpha.1 (alpha) — 2024-05-21
- 0.16.0 — 2025-07-08
- 0.16.0-next.8 — 2025-06-26
- 0.16.0-next.7 — 2025-06-25
- 0.16.0-next.6 — 2025-06-25
- 0.16.0-next.5 — 2025-06-18
- 0.16.0-next.4 — 2025-06-18
- 0.16.0-next.3 — 2025-06-13
- 0.16.0-next.2 — 2025-06-13
- 0.16.0-next.1 — 2025-06-13
- 0.16.0-next.0 — 2025-06-13
- 0.15.2-next.0 — 2025-06-11
- … 248 more at https://npm.io/package/@canvas-js/core/versions

## README

# @canvas-js/core

This package exports a `Canvas` class that can be used to manually
instantiate Canvas applications.

Use this package directly if you want fine-grained control over when an
application is started/stopped. Otherwise, you can use `useCanvas` in
`@canvas-js/hooks`, which has the same API, but handles initialization
inside React for you.

## Table of Contents

- [Installation](#installation)
- [Usage](#usage)
- [API](#api)

## Installation

```
$ npm i @canvas-js/core
```

## How it works

Under the hood, each `Canvas` application replicates and executes a
log of signed actions, sourced from GossipLog, with read/write access
to a ModelDB database.

Each application accepts several arguments:

- `contract` takes a class that extends `Contract`, or a string containing a JS module which exports a default class that extends `Contract`.
- `topic` takes a string
- `snapshot` (optional) takes a `Snapshot` object which provides initial database contents for the application.
- `signers` (optional) takes an array of signers, which allows different auth methods to be added to the application.

Use `await Canvas.initialize` to start the application. (For synchronous initialization, see CanvasLoadable.ts.)

To connect the application to other sync peers, use `app.connect()` to
start a WebSocket connection, or `app.listen()` to listen for
WebSocket connections from the server.

Or, use `app.startLibp2p()` to start a libp2p node.

After starting the application, you can use `app.actions` to access
each of the actions that you have defined.

Action calls will be signed and proxied to the contract.

```ts
import { Canvas, Contract } from "@canvas-js/core"

class Chat extends Contract<typeof Chat.models> {
  static models = {
    posts: {
      id: "primary",
      user: "string",
      content: "string",
      updated_at: "integer",
    },
  }

  async createPost(content: string) {
    const { id, chain, address, timestamp, db } = this
    const user = [chain, address].join(":")
    await db.set("posts", { id, user, content, updated_at: timestamp })
  }

  async deletePost(postId: string) {
    const { chain, address, db } = this
    const post = await db.get("posts", postId)
    if (post === null) {
      return
    }

    const user = [chain, address].join(":")
    if (post.user !== user) {
      throw new Error("not authorized")
    }

    await db.delete("posts", postId)
  }
}

const app = await Canvas.initialize({
  topic: "example.xyz",
  contract: Chat,
})

await app.actions.createPost("hello world!")
const results = await app.db.query("posts", {})
// [
//   {
//     id: '09p5qn7affkhtbflscr663tet8ddeu41',
//     user: 'did:pkh:eip155:1:0x79c5158f81ebb0c2bcF877E9e1813aed2Eb652B7',
//     content: 'hello world!',
//     updated_at: 1698339861041
//   }
// ]
```

## API

<<< @/../packages/core/src/types.ts

---
_Source: https://npm.io/package/@canvas-js/core · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
