Part 2: Start Sandboxes from It
Bind CreateSandbox to the snapshot
Section titled “Bind CreateSandbox to the snapshot”import * as Cloudflare from "@alchemy.run/cloudflare";import * as Daytona from "@alchemy.run/daytona";import * as Effect from "effect/Effect";import { Warm } from "./Images.ts";
export default class Api extends Cloudflare.Worker<Api>()( "Api", { main: import.meta.url }, Effect.gen(function* () { const sandboxes = yield* Daytona.CreateSandbox(Warm); return {}; }).pipe(Effect.provide(Daytona.CreateSandboxHttp)),) {}Every sandbox this client creates boots from Warm. The Worker gets its own API key, with write:sandboxes and delete:sandboxes.
Run a command in a fresh sandbox
Section titled “Run a command in a fresh sandbox” const sandboxes = yield* Daytona.CreateSandbox(Warm);
const run = Effect.fn("run")(function* (command: string) { const sandbox = yield* Effect.acquireRelease(sandboxes.create(), (s) => sandboxes.delete(s.sandboxId).pipe(Effect.ignore), ); return yield* sandbox.exec(command, { cwd: "/home/daytona/repo" }); });The repository is already there: the sandbox boots from the snapshot, so nothing clones at request time. create waits until the sandbox has started, and acquireRelease deletes it when the request ends. exec returns exitCode and result.
Serve it
Section titled “Serve it” return {}; return { fetch: Effect.gen(function* () { const request = yield* HttpServerRequest; const url = new URL(request.url, "http://api"); if (request.method === "POST" && url.pathname === "/run") { return yield* HttpServerResponse.json(yield* run(yield* request.text)); } return HttpServerResponse.text("not found", { status: 404 }); }).pipe(Effect.scoped, Effect.orDie), };Deploy it
Section titled “Deploy it” Effect.gen(function* () { const warm = yield* Warm; return { snapshot: warm.name }; const api = yield* Api; return { url: api.url.as<string>(), snapshot: warm.name }; }),pnpm alchemy deploycurl -X POST "$URL/run" -d 'git log -1 --format=%s'bun alchemy deploycurl -X POST "$URL/run" -d 'git log -1 --format=%s'npx alchemy deploycurl -X POST "$URL/run" -d 'git log -1 --format=%s'yarn alchemy deploycurl -X POST "$URL/run" -d 'git log -1 --format=%s'The snapshots are unchanged, so this deploy only adds the Worker.
Part 3 gives each user a sandbox of their own.