Worker
Learn how to deploy, configure, and manage Cloudflare Workers using Alchemy for serverless functions at the edge.
This guide walks you through creating, testing, and deploying a Cloudflare Worker, then evolving it to use Durable Objects for session storage.
-
Create a Worker
Start by defining a Worker in your
alchemy.run.ts
file.import alchemy from "alchemy";import { Worker } from "alchemy/cloudflare";const app = await alchemy("my-app");export const worker = await Worker("api", {entrypoint: "./src/worker.ts",url: true, // Get a public URL});console.log({ url: worker.url });await app.finalize(); -
Implement the Worker
Create your Worker script with a basic fetch handler.
src/worker.ts import type { worker } from "../alchemy.run";export default {async fetch(request: Request, env: typeof worker.Env): Promise<Response> {const url = new URL(request.url);return Response.json({message: "Hello from Alchemy!",path: url.pathname,});},}; -
Test Locally
Run the Worker in development mode.
Terminal window bun alchemy devYour Worker is now running locally and will auto-reload on code changes.
-
Deploy to Production
Deploy your Worker to Cloudflare’s edge network.
Terminal window bun alchemy deployYour Worker is now live at the URL shown in the console.
-
Add Session Storage
Create a Durable Object namespace to store session data.
alchemy.run.ts import alchemy from "alchemy";import { Worker } from "alchemy/cloudflare";import { Worker, DurableObjectNamespace } from "alchemy/cloudflare";const app = await alchemy("my-app");const sessions = DurableObjectNamespace("sessions", {className: "Session",sqlite: true,});export const worker = await Worker("api", {entrypoint: "./src/worker.ts",url: true,bindings: {SESSIONS: sessions,},});console.log({ url: worker.url });await app.finalize(); -
Implement the Session Class
Create a Durable Object to handle session state.
src/worker.ts import { DurableObject } from "cloudflare:workers";import type { worker } from "../alchemy.run";export class Session extends DurableObject {async fetch(request: Request) {const url = new URL(request.url);if (url.pathname === "/set") {const data = await request.json();await this.ctx.storage.put("data", data);return Response.json({ success: true });}const data = await this.ctx.storage.get("data");return Response.json({ data });}} -
Use Session from Worker
Update your Worker to use sessions based on a request header.
src/worker.ts import { env } from "cloudflare:workers";import type { worker } from "../alchemy.run";export default {async fetch(request: Request, env: typeof worker.Env): Promise<Response> {const url = new URL(request.url);return Response.json({message: "Hello from Alchemy!",path: url.pathname,});const sessionId = request.headers.get("x-session-id") || "default";// Get the session Durable Objectconst id = env.SESSIONS.idFromName(sessionId);const session = env.SESSIONS.get(id);// Forward request to sessionreturn session.fetch(request);},}; -
Test the Session
Test your session-enabled Worker locally.
Terminal window # Set session datacurl -X POST http://localhost:8787/set \-H "x-session-id: user-123" \-H "Content-Type: application/json" \-d '{"name": "Alice", "theme": "dark"}'# Get session datacurl http://localhost:8787 \-H "x-session-id: user-123" -
Deploy Updated Worker
Deploy your session-enabled Worker to production.
Terminal window bun alchemy deployYour Worker now persists session data across requests using Durable Objects.