Skip to content

Daytona.Sandbox reference

Source: packages/daytona/src/ConnectSandbox.ts

Work with one Sandbox declared at deploy, from any host: run commands, read and write files, use git, open previews, fork it or snapshot it. The handle has the same methods as the DaytonaSandbox that CreateSandbox returns; its sandboxId, name and toolboxProxyUrl are Effects, resolved at runtime.

The host gets its own Daytona API key with write:sandboxes and delete:sandboxes; Daytona checks permissions per kind of object, so the key can reach other sandboxes in the organization too.

ConnectSandbox: Running Commands in a Dev Box

Section titled “ConnectSandbox: Running Commands in a Dev Box”
const devBox = yield* Daytona.ConnectSandbox(DevBox);
return {
fetch: Effect.gen(function* () {
yield* devBox.startSandbox();
const test = yield* devBox.exec("git pull && pnpm test", { cwd: "/home/daytona/repo" });
return yield* HttpServerResponse.json(test);
}),
};

ConnectSandbox: Putting a Worker in Front of a Port

Section titled “ConnectSandbox: Putting a Worker in Front of a Port”
const devBox = yield* Daytona.ConnectSandbox(DevBox);
return {
fetch: Effect.gen(function* () {
const port = yield* devBox.getTcpPort(3000);
return yield* port.fetch(yield* HttpServerRequest);
}),
};

Terminals, code interpreter runs and followed logs stream over a WebSocket, so they work from a Worker too.

const output = yield* Effect.scoped(
Effect.gen(function* () {
const pty = yield* devBox.process.createPty({ id: "build" });
yield* pty.write("pnpm build; exit\n");
return yield* pty.output.pipe(Stream.decodeText(), Stream.mkString);
}),
);

Daytona forks linux-vm sandboxes; a container’s fork fails with ForkNotSupported.

const fork = yield* devBox.forkSandbox({ name: `pr-${number}` });

Source: packages/daytona/src/CreateSandbox.ts

Create, find and delete Daytona sandboxes at runtime, from a Worker, Lambda or any other host: a sandbox per request, per user or per task. Pass a Snapshot to create every sandbox from it (a warm base), or nothing to pick the snapshot per call.

The host gets its own Daytona API key, with only write:sandboxes and delete:sandboxes. Every sandbox it creates is labeled with the stack, stage and host as Daytona labels (alchemy.stack, alchemy.stage, alchemy.id; Daytona doesn’t allow :: in a label key), on top of your own labels. They are not deleted when the stack is destroyed: list them by those labels to clean them up, e.g. sandboxes.list({ labels: { "alchemy.id": "Api" } }). Each sandbox comes back as a DaytonaSandbox handle with the whole toolbox: processes, files, git, LSP, previews, forks and snapshots.

const sandboxes = yield* Daytona.CreateSandbox(Base);
return {
fetch: Effect.gen(function* () {
const sandbox = yield* Effect.acquireRelease(
sandboxes.create({ autoDeleteInterval: 0 }),
(s) => sandboxes.delete(s.sandboxId).pipe(Effect.ignore),
);
const ls = yield* sandbox.exec("ls -la");
return yield* HttpServerResponse.json(ls);
}),
};
const sandbox =
(yield* sandboxes.get(`dev-${user}`)) ??
(yield* sandboxes.create({ name: `dev-${user}`, labels: { user } }));
yield* sandbox.startSandbox();
yield* sandbox.git.cloneRepository({ url: repoUrl, path: "/home/daytona/repo" });

Source: packages/daytona/src/Sandbox.ts

A long-lived Daytona sandbox: an isolated machine with its own filesystem, processes and network, for a dev box, a preview environment or a persistent agent workspace. Its disk survives stops; Daytona stops it when idle and archives it later, and deploy starts it again.

The class form runs an Effect program in the sandbox: give it main and an implementation, and Alchemy bundles the program, uploads it and runs it with Node, served on the sandbox’s preview URL (url). A code change uploads the new bundle and restarts the process; the sandbox and its URL stay the same. The process lives as long as the sandbox runs, so a sandbox with main never auto-stops unless autoStopInterval says so; if it stopped anyway, the next deploy starts it and the program.

For sandboxes created per request or per task, use CreateSandbox at runtime instead; for one declared here, use ConnectSandbox to run commands in it from a Worker.

export const DevBox = Daytona.Sandbox("DevBox", {
snapshot: Base,
autoStopInterval: "30 minutes",
});
export const Locked = Daytona.Sandbox("Locked", {
snapshot: Base,
domainAllowList: ["github.com", "registry.npmjs.org"],
});
export const Builder = Daytona.Sandbox("Builder", {
snapshot: Base,
mounts: [{ volume: Cache, path: "/cache" }],
secrets: { NPM_TOKEN: NpmToken },
});
export default class Agent extends Daytona.Sandbox<Agent>()(
"Agent",
{ snapshot: Base, main: import.meta.url, public: true },
Effect.gen(function* () {
const data = yield* Daytona.MountVolume(Data, { path: "/data" });
const fs = yield* FileSystem.FileSystem;
return {
fetch: Effect.gen(function* () {
return yield* HttpServerResponse.json(yield* fs.readDirectory(data.path));
}),
};
}).pipe(Effect.provide(Daytona.MountVolumeLive)),
) {}

A mount binding that needs a client in the image (an alchemy/Mount capability) makes the sandbox boot from a derived snapshot: its snapshot plus the client’s install lines, built on Daytona, named from a hash and deleted with the sandbox. Sandboxes provide FUSE; a client that needs NFS fails with MountUnsupported.

The program reaches the internet as its sandbox does. Below Daytona’s Tier 3, sandboxes reach only an allowlist of essential services (package registries, GitHub, AI APIs, Daytona’s API), so bindings that manage sandboxes work but toolbox calls are refused.

A GPU sandbox boots from a GPU snapshot, such as Daytona’s daytona-gpu (one H100) in its earth region, and must be ephemeral. The organization needs GPU credits in its wallet.

export const Trainer = Daytona.Sandbox("Trainer", {
snapshot: { name: "daytona-gpu" },
target: "earth",
autoDeleteInterval: 0,
spot: true,
});
export const Scratch = Daytona.Sandbox("Scratch", { ttl: "2 hours" });
export const Db = Daytona.Sandbox("Db", { autoDeleteInterval: 0 });
export const App = Daytona.Sandbox("App", {
autoDeleteInterval: 0,
linkedSandbox: Db,
});

Sandbox.lookup references a sandbox Alchemy doesn’t manage (created in the Daytona dashboard or by another stack). It is read during plan/deploy, never created, updated or deleted. url and program are undefined: they come from the deploy that started the sandbox’s program.

const box = Daytona.Sandbox.lookup({ name: "research" });