Skip to content
GitHub Discord

Cloudflare Durable Object

This example creates a Durable Object and links it to a worker.

Send a GET request to the url output. The worker calls the Durable Object, and the Durable Object logs the current count.

sst.config.ts
const counter = new sst.cloudflare.DurableObject("Counter", {
className: "Counter",
});
const api = new sst.cloudflare.Worker("Api", {
migrations: [
{
tag: "v1",
newSqliteClasses: [counter.className],
},
],
handler: "worker.ts",
link: [counter],
url: true,
});
worker.ts
import { Resource } from "sst";
import { DurableObject } from "cloudflare:workers";
export default {
async fetch(request: Request) {
const url = new URL(request.url);
if (url.pathname === "/favicon.ico") {
return new Response(null, { status: 204 });
}
const stub = Resource.Counter.getByName("global");
return stub.fetch("https://counter/");
},
};
export class Counter extends DurableObject {
async fetch() {
const current = (await this.ctx.storage.get<number>("count")) ?? 0;
const count = current + 1;
await this.ctx.storage.put("count", count);
console.log("durable object hit", { count });
return Response.json({ count });
}
}

View the full example.