import { DurableObject } from 'cloudflare:workers';
YOUR_KV_NAMESPACE: KVNamespace;
YOUR_DO_CLASS: DurableObjectNamespace;
async fetch(req: Request, env: Env): Promise<Response> {
// Assume each Durable Object is mapped to a roomId in a query parameter
// In a production application, this will likely be a roomId defined by your application
// that you validate (and/or authenticate) first.
let url = new URL(req.url);
let roomIdParam = url.searchParams.get("roomId");
// Create (or get) a Durable Object based on that roomId.
let durableObjectId = env.YOUR_DO_CLASS.idFromName(roomIdParam);
// Get a "stub" that allows you to call that Durable Object
let durableObjectStub = env.YOUR_DO_CLASS.get(durableObjectId);
// Pass the request to that Durable Object and await the response
// This invokes the constructor once on your Durable Object class (defined further down)
// on the first initialization, and the fetch method on each request.
// You could pass the original Request to the Durable Object's fetch method
// or a simpler URL with just the roomId.
let response = await durableObjectStub.fetch(`http://do/${roomId}`);
// This would return the value you read from KV *within* the Durable Object.
export class YourDurableObject extends DurableObject {
constructor(public state: DurableObjectState, env: Env) {
// Ensure you pass your bindings and environmental variables into
// each Durable Object when it is initialized
async fetch(request: Request) {
// Error handling elided for brevity.
await this.env.YOUR_KV_NAMESPACE.put("some-key");
let val = await this.env.YOUR_KV_NAMESPACE.get("some-other-key");
return Response.json(val);