Skip to content

Secrets

Code in a sandbox, or an agent driving it, shouldn’t be able to read your tokens. A Daytona secret solves that: the sandbox sees a placeholder in its environment, and Daytona’s egress proxy swaps in the real value only on requests to the hosts the secret names. The value never enters the sandbox.

requestreal valueplaceholdersandboxnever sees the valueDaytonanpm install$NPM_TOKEN = placeholderegress proxyswaps the value inregistry.npmjs.orggets the real tokenany other hostgets the placeholderrequestreal valueplaceholdersandboxnever sees the valueDaytonanpm install$NPM_TOKEN = placeholderegress proxyswaps the value inregistry.npmjs.orggets the real tokenany other hostgets the placeholder
The sandbox holds only a placeholder. On its way out, a request to registry.npmjs.org gets the real token; a request anywhere else keeps the placeholder.
src/Secrets.ts
import * as Daytona from "@alchemy.run/daytona";
import * as Config from "effect/Config";
export const NpmToken = Daytona.Secret("NpmToken", {
value: Config.Redacted("NPM_TOKEN"),
hosts: ["registry.npmjs.org"],
});

Leave out hosts and Daytona sends the value to any host.

src/DevBox.ts
import * as Daytona from "@alchemy.run/daytona";
import { Base } from "./Images.ts";
import { NpmToken } from "./Secrets.ts";
export const DevBox = Daytona.Sandbox("DevBox", {
snapshot: Base,
secrets: { NPM_TOKEN: NpmToken },
});

$NPM_TOKEN in the sandbox holds the placeholder, and npm install from a private registry works. Changing the secrets updates the sandbox in place.

A Worker can create a secret per user and hand it to that user’s sandbox:

const secrets = yield* Daytona.CreateSecret();
const github = yield* secrets.put({ name: `github-${user}`, value: token, hosts: ["api.github.com"] });
const sandbox = yield* sandboxes.create({ secrets: [{ GITHUB_TOKEN: github.name }] });

The Worker’s key has manage:secrets. Putting the same name again replaces the value. Each secret the binding creates carries stack/stage/id in its description, and is not deleted when the stack is destroyed: secrets.list() and match the description to clean them up.

.env is only the default. A Stack’s secrets option reads the same Config values from a secrets manager instead, like Doppler or Infisical, so production tokens never sit in a file on anyone’s machine. Pick the source per stage:

alchemy.run.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Daytona from "@alchemy.run/daytona";
import * as Doppler from "@alchemy.run/doppler";
import * as Alchemy from "alchemy";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { DevBox } from "./src/DevBox.ts";
export default Alchemy.Stack(
"Sandboxes",
{
providers: Layer.mergeAll(Cloudflare.providers(), Daytona.providers()),
secrets: ({ stage }) =>
stage === "prod"
? Doppler.Secrets({ project: "sandboxes", config: "prd" })
: Alchemy.Secrets.DotEnv(),
},
Effect.gen(function* () {
const devBox = yield* DevBox;
return { devBox: devBox.sandboxId };
}),
);

Your code doesn’t change: Config.Redacted("NPM_TOKEN") reads from whichever source the stage uses. See Secret providers for Doppler, Infisical, ordering several sources, and writing your own.

What a sandbox can reach at all is in Network and previews.