Skip to main content

DurableObjectNamespace

Creates a Durable Object namespace binding for stateful objects in Cloudflare Workers.

Props

className
string
required
Name of the class that implements the Durable Object.
const rooms = DurableObjectNamespace("chat-rooms", {
  className: "ChatRoom"
});
scriptName
string
Name of the script containing the Durable Object implementation.
environment
string
Environment name for the Durable Object.
sqlite
boolean
Whether to enable SQLite storage for the Durable Object.
const users = DurableObjectNamespace("user-store", {
  className: "User",
  sqlite: true
});

Output

id
string
The ID of the Durable Object namespace.
className
string
Name of the class that implements the Durable Object.
scriptName
string
Name of the script containing the Durable Object implementation.
type
durable_object_namespace
Type identifier for the binding.

Examples

Basic Durable Object Namespace

const rooms = DurableObjectNamespace("chat-rooms", {
  className: "ChatRoom"
});

const worker = await Worker("chat", {
  entrypoint: "./src/chat.ts",
  bindings: {
    ROOMS: rooms
  }
});

Durable Object with SQLite

const users = DurableObjectNamespace("user-store", {
  className: "User",
  sqlite: true
});

const worker = await Worker("api", {
  entrypoint: "./src/api.ts",
  bindings: {
    USERS: users
  }
});

Cross-Script Binding

const dataWorker = await Worker("data-worker", {
  entrypoint: "./src/data.ts",
  bindings: {
    STORAGE: DurableObjectNamespace("storage", {
      className: "DataStorage"
    })
  }
});

const apiWorker = await Worker("api-worker", {
  entrypoint: "./src/api.ts",
  bindings: {
    SHARED_STORAGE: dataWorker.bindings.STORAGE
  }
});

Build docs developers (and LLMs) love