Skip to content
Quickstart

Quickstart

Media generation, durable output retention and provenance with host-owned authority and billing

Save this as worker.ts. TypeScript hosts use lib: ["ES2023", "ESNext.Disposable"] and types: ["@cloudflare/workers-types/experimental"] in their compiler options. This local example uses a fixed provider that returns a one-pixel GIF, so you can try the save and retry path without a model account. Replace provider with your own MediaProvider for real generation.

quickstart.ts
/// <reference types="@cloudflare/workers-types/experimental" resolution-mode="require" />
import { listMediaModels } from "@fungi.computer/darkroom";
import { createMediaService } from "@fungi.computer/darkroom/server";
import { MediaOperationDO } from "@fungi.computer/darkroom/cloudflare";
export { MediaOperationDO };
type Bindings = {
MEDIA_OPERATION: DurableObjectNamespace<MediaOperationDO>;
APP_ARTIFACTS: R2Bucket;
OUTPUTS: R2Bucket;
};
export default {
async fetch(request: Request, env: Bindings): Promise<Response> {
const service = createMediaService({
scope: {
actorId: "local-owner",
namespace: "studio",
owner: "illustrator",
destinationId: "outputs",
},
operations: env.MEDIA_OPERATION,
provider: async () => ({
bytes: Uint8Array.from(
atob("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"),
(character) => character.charCodeAt(0),
),
format: "gif",
}),
metering: "unmetered",
destination: {
authorize: async () => {},
write: async (path, bytes) => {
await env.OUTPUTS.put(path, bytes);
},
},
});
if (new URL(request.url).pathname === "/catalog")
return Response.json(listMediaModels());
return service.fetch(request);
},
};

Save this as wrangler.json:

JSON
{
"name": "darkroom-example",
"main": "worker.ts",
"compatibility_date": "2026-09-02",
"compatibility_flags": ["nodejs_compat"],
"durable_objects": {
"bindings": [
{ "name": "MEDIA_OPERATION", "class_name": "MediaOperationDO" }
]
},
"migrations": [{ "tag": "v1", "new_sqlite_classes": ["MediaOperationDO"] }],
"r2_buckets": [
{ "binding": "APP_ARTIFACTS", "bucket_name": "darkroom-retained" },
{ "binding": "OUTPUTS", "bucket_name": "darkroom-output" }
]
}

Run npx wrangler dev --local, then:

TERMINAL
curl http://localhost:8787/models
curl http://localhost:8787/generate \
-H 'Content-Type: application/json' -H 'X-Request-Id: first-picture' \
-d '{"path":"picture.gif","settings":{"model":"@cf/black-forest-labs/flux-1-schnell","prompt":"a quiet greenhouse","width":1024,"height":768}}'

Repeat the same request to replay its saved result. Change the input with the same ID and the service returns 409. This example admits one local owner. Add your host’s authentication and destination authorization before serving others.