Skip to content

Sandboxes

A sandbox is a MicroVM: a small virtual machine with its own kernel, disk and network, that boots in seconds. Your program creates sandboxes while it runs, runs code it doesn’t trust inside them, and deletes them when it’s done.

A template is a sandbox prepared ahead of time from a container image, with your setup commands already run. Every sandbox created from it boots from its saved state, so packages you installed are already there.

src/sandbox.ts
import * as DigitalOcean from "@alchemy.run/digitalocean";
export const MyTemplate = DigitalOcean.Sandboxes.Template("MyTemplate", {
image: "node:22-bookworm",
setup: ["npm install -g typescript"],
});

Deploy boots the image, runs setup, and saves the result.

CreateSandbox gives a runtime a client that creates sandboxes from the template. DigitalOcean has no API to create a narrower token, so pass a token you created with only the microvm scopes; it reaches the runtime as a secret.

src/api.ts
import * as DigitalOcean from "@alchemy.run/digitalocean";
import * as Config from "effect/Config";
import * as Effect from "effect/Effect";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import { MyTemplate } from "./sandbox.ts";
export default class Api extends DigitalOcean.App<Api>()(
"Api",
{ main: import.meta.url },
Effect.gen(function* () {
const token = yield* Config.Redacted("SANDBOX_TOKEN");
const sandboxes = yield* DigitalOcean.Sandboxes.CreateSandbox(MyTemplate, { token });
return {
fetch: Effect.gen(function* () {
const sandbox = yield* Effect.acquireRelease(sandboxes.create(), (s) =>
Effect.ignore(s.delete),
);
const result = yield* sandbox.exec("tsc --version");
return HttpServerResponse.text(result.stdout);
}).pipe(Effect.orDie),
};
}).pipe(Effect.provide(DigitalOcean.Sandboxes.CreateSandboxHttp)),
) {}

Effect.acquireRelease deletes the sandbox when the request ends, even if the handler fails. exec returns a non-zero exit code as a result, not a failure.

const sandbox = yield* sandboxes.create({ httpPort: 3000, autoPauseSeconds: 300 });
yield* sandbox.exec("npx serve -l 3000 &");
sandbox.url; // public URL of port 3000

With autoPauseSeconds, an idle sandbox pauses, keeping its memory and disk, and the next request to url resumes it.

yield* sandboxes.create({ size: { cpu: 2, memory: 4096 } });

Sizes come in fixed pairs: 1 vCPU and 2 GiB, 2 and 4, 8 and 16, 16 and 32.

A Sandbox resource is one sandbox declared in the Stack, created at deploy and deleted with it:

yield* DigitalOcean.Sandboxes.Sandbox("Preview", { base: yield* MyTemplate, httpPort: 3000 });
  • A sandbox you create at run time isn’t in the Stack: delete it yourself, or alchemy destroy won’t find it.
  • Every sandbox from the same template starts from the same saved state; per-sandbox values go in env.