Backend · Skill

Build a Cloudflare Durable Object

build-cloudflare-durable-objects

Builds or reviews Cloudflare Durable Objects for per-entity state: one object per room or user, SQLite storage, RPC methods, and alarms. Use when the user wants a Durable Object, a chat-room or per-entity lock on Workers, or a wrangler binding and migration for that class. Listing objects is not a D1 query, and a static-site deploy is a different task.

Works with: Any AI Not yet model-tested; testing Jan 2027

Paste it into ChatGPT, Claude, Gemini or any AI chat, then tell it about your project. How it works

Install in Claude Code, Cursor or Codex (optional)

Only for AI coding tools that read skill folders. Run one line in a terminal, in your project folder. It downloads this skill's text files from TheSkillVault and nothing else; you can read them first with “View raw SKILL.md”.

Claude Code (all your projects)

curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/build-cloudflare-durable-objects/raw' -o ~/'.claude/skills/build-cloudflare-durable-objects/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/build-cloudflare-durable-objects/files/LICENSE.txt' -o ~/'.claude/skills/build-cloudflare-durable-objects/LICENSE.txt' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/build-cloudflare-durable-objects/files/NOTICE.txt' -o ~/'.claude/skills/build-cloudflare-durable-objects/NOTICE.txt'

Cursor (this project)

curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/build-cloudflare-durable-objects/raw' -o '.cursor/skills/build-cloudflare-durable-objects/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/build-cloudflare-durable-objects/files/LICENSE.txt' -o '.cursor/skills/build-cloudflare-durable-objects/LICENSE.txt' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/build-cloudflare-durable-objects/files/NOTICE.txt' -o '.cursor/skills/build-cloudflare-durable-objects/NOTICE.txt'

Codex and other Agent Skills tools (this project)

curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/build-cloudflare-durable-objects/raw' -o '.agents/skills/build-cloudflare-durable-objects/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/build-cloudflare-durable-objects/files/LICENSE.txt' -o '.agents/skills/build-cloudflare-durable-objects/LICENSE.txt' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/build-cloudflare-durable-objects/files/NOTICE.txt' -o '.agents/skills/build-cloudflare-durable-objects/NOTICE.txt'

Or connect the whole library over MCP

Add TheSkillVault to Cursor or add this to your app's MCP settings (read-only; help):

{
  "mcpServers": {
    "theskillvault": {
      "url": "https://theskillvault.ai/mcp"
    }
  }
}

A Durable Object is one strongly consistent actor for a single entity, such as a room, a game, a document, or a user. It is not a global cache and it is not a D1 database.

Read the current docs before you write an API call. Start at https://developers.cloudflare.com/durable-objects/ and the best-practices pages there. If those pages disagree with this skill, follow the pages and name the one you used.

When to use

  • One room, game, document, or user needs its own consistent state.
  • That entity needs a WebSocket or its own alarm.
  • You are adding a binding, a migration, or a review of an existing class.

When not to use

  • The handler is stateless. Use a normal Worker and apply-cloudflare-workers-practices.
  • You only need to host a site. Use deploy-site-to-cloudflare.
  • You need many independent reads spread worldwide, with no coordination.

Steps

  1. Open the docs above and the project's wrangler config. Treat generated types and the compatibility date as the baseline.
  2. Model one object per coordination atom. One room. Not one object for every request in the account.
  3. Bind the class in wrangler.jsonc. Add a migration tag that lists it under new_sqlite_classes. New namespaces use the SQLite backend.
  4. Route with getByName so the same name always reaches the same object. If you need a random id, store the newUniqueId mapping outside the object.
  5. Extend DurableObject from cloudflare:workers. Create tables in the constructor, inside blockConcurrencyWhile, and only for schema setup.
  6. Expose RPC methods on the class. Confirm on the docs whether your compatibility date still needs a fetch handler. The pinned guide treated RPC as available from 2024-04-03 onward. Check that date before you set it.
  7. Write storage before you update memory. Group related writes in one storage transaction. Do not await an outside call between those writes.
  8. Keep a single alarm. setAlarm replaces the previous one. deleteAlarm cancels it.
  9. From the Worker, await every RPC call. Inside try, return await the async handler so a rejection still reaches catch.
  10. Access follows the Worker. There is no separate object role. Read https://developers.cloudflare.com/workers/authorization/durable-objects/ before you grant observability or Data Studio access. Scope that role to the Worker you mean.

Fixed examples

The pinned guide's room handler returned the promise of an async function from inside try, without await. The catch then never saw a rejection. Await it:

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const url = new URL(request.url);
    try {
      if (url.pathname.startsWith("/api/rooms")) {
        return await handleRooms(request, env);
      }
      return new Response("Not Found", { status: 404 });
    } catch (error) {
      console.error("Request failed:", error);
      return new Response("Internal Server Error", { status: 500 });
    }
  },
};

handleRooms stays async and awaits stub.sendMessage or stub.getMessages. Do not return that promise bare.

Listing objects is not a D1 command. The pinned guide's "List DOs" example ran wrangler d1 execute against a table name. D1 is a different product. Wrangler's command index does not include a Durable Object list command. List namespaces with the account API:

GET /accounts/{account_id}/workers/durable_objects/namespaces

List objects in one namespace with:

GET /accounts/{account_id}/workers/durable_objects/namespaces/{namespace_id}/objects

Send the token as a header. Do not put it in the repo, the chat, or the command line of a committed script.

Gotchas

  • blockConcurrencyWhile on every request serializes the object. Use it for schema setup only. Do not hold it across fetch or other outside I/O.
  • State that lives only in memory is gone after eviction or a crash.
  • A global object becomes a bottleneck. Split by entity name.
  • Secrets do not belong in wrangler.jsonc. Use wrangler secret.

Portability

The class runs only on Cloudflare Workers. If you are using a regional database for the lock instead, keep that and do not add an object you do not need. Callers can be any language that can reach the Worker.

Example prompt

"Add a Durable Object per chat room with SQLite storage and RPC. Await the room handler inside try. Show me how to list objects without wrangler d1 execute."

Done when

  • The migration uses new_sqlite_classes, and routing uses getByName for a stable name.
  • Schema setup is the only blockConcurrencyWhile, and related writes share one transaction.
  • The Worker awaits async handlers inside try.
  • Nothing in the change tells the user to query Durable Objects through wrangler d1 execute.
  • The API token is not in git.

---

Modified from "durable-objects" in cloudflare/skills (Apache-2.0). Attribute to Cloudflare (cloudflare/skills). The upstream LICENSE keeps the unfilled template line. See LICENSE.txt.

Bundled files

Licence

Adapted from Cloudflare (cloudflare/skills) (Copyright [yyyy] [name of copyright owner]. The cloudflare/skills LICENSE keeps that Apache template line unfilled. Attribute this skill to Cloudflare (cloudflare/skills).), Apache License 2.0. Modified by TheSkillVault on 2026-11-24. Rewritten as a short build and review guide. Docs are retrieved before API claims. Two upstream example bugs are fixed: listing objects no longer uses wrangler d1 execute, and the room handler returns await handleRooms() so a rejection still hits catch. The long reference pack was not copied. Not the original file.

Licence: Apache License 2.0. Attribution when you reuse it: Based on work by Cloudflare (cloudflare/skills) (Copyright [yyyy] [name of copyright owner]. The cloudflare/skills LICENSE keeps that Apache template line unfilled. Attribute this skill to Cloudflare (cloudflare/skills)., Apache License 2.0), https://github.com/cloudflare/skills/tree/ff191de7d903788eceaf516a514ae7d3b56b924a/skills/durable-objects; adapted by TheSkillVault, https://theskillvault.ai