Creates isolated Linux MicroVMs using Vercel Sandbox SDK. Use when building code execution environments, running untrusted code, spinning up dev servers, testing in isolation, or when the user mentions "sandbox", "microvm", "isolated execution", or "@vercel/sandbox".
Creates isolated Linux MicroVMs using Vercel Sandbox SDK. Use when building code execution environments, running untrusted code, spinning up dev servers, testing in isolation, or when the user mentions "sandbox", "microvm", "isolated execution", or "@vercel/sandbox".
metadata
{"author":"Vercel Inc.","version":"1.0"}
CRITICAL: Always Use Correct @vercel/sandbox Documentation
Your knowledge of @vercel/sandbox may be outdated.
Follow these instructions before starting on any sandbox-related tasks:
Snapshots save the entire sandbox filesystem to be reused later on, for any number of sandboxes.
Create a Snapshot
const sandbox = awaitSandbox.create({ runtime: 'node24' });
// Install dependenciesawait sandbox.runCommand('npm', ['install']);
// Create snapshot (stops the sandbox)const snapshot = await sandbox.snapshot({
expiration: ms('14d') // Default: 30 days, use 0 for no expiration
});
console.log('Snapshot ID:', snapshot.snapshotId);
List and Manage Snapshots
// List snapshotsconst { snapshots, pagination } = awaitSnapshot.list();
// Get a specific snapshotconst snapshot = awaitSnapshot.get({ snapshotId: 'snap_abc123' });
// Delete snapshotawait snapshot.delete();
Exposed Ports
const sandbox = awaitSandbox.create({
ports: [3000, 8080]
});
// Get public URL for a portconst url = sandbox.domain(3000);
// Returns: https://subdomain.vercel.run// Open in browserspawn('open', [url]);
Timeout Management
const sandbox = awaitSandbox.create({
timeout: ms('10m') // Initial timeout, default of 5 minutes
});
// Extend timeout by 5 more minutesawait sandbox.extendTimeout(ms('5m'));
// New total: 15 minutes
Authentication
Vercel OIDC Token (Recommended)
# Pull development credentials
vercel link
vercel env pull
The SDK automatically uses VERCEL_OIDC_TOKEN from environment.
Beta: Persistent Sandboxes (@vercel/sandbox@beta and sandbox@beta)
The beta introduces persistent, long-lived sandboxes with a new Session layer. Install with:
pnpm i @vercel/sandbox@beta # SDK 2.0.0-beta.x
pnpm i -g sandbox@beta # CLI 3.0.0-beta.x
IMPORTANT:
This is a beta, not a stable version. Do not use for production.
If the user had installed a previous major version (@vercel/sandbox@1, sandbox@1, sandbox@2), make it clear that sandboxes are by default persistent: they will automatically create snapshots to preserve the state.
Key Concepts
Sandbox = a persistent, named entity that survives across multiple VM boots.
Session = a running VM instance within a sandbox. Sessions are created/resumed automatically and are identified by ID.
Sandboxes are identified by name (not ID). Names are unique per project.
When a sandbox stops, it will automatically snapshot and restore the state on the next resume (with persistent: true, the default).
Migration: Old V1 sandboxes are backfilled with sandboxId as their name (e.g., sbx_123), so the only change needed is using name instead of sandboxId.
New Exports
import { Session } from'@vercel/sandbox';
Migration from Stable (1.x) to Beta (2.x)
Creating sandboxes — new name and persistent params
// Stable (1.x): anonymous, ephemeral sandboxes identified by sandboxIdconst sandbox = awaitSandbox.create({ runtime: 'node24' });
console.log(sandbox.sandboxId);
// Beta (2.x): persistent sandboxes identified by nameconst sandbox = awaitSandbox.create({
name: 'my-dev-env', // Optional, random if omitted. Unique per project.runtime: 'node24',
persistent: true// Default: true. Auto-snapshots on shutdown and restores on resume.
});
console.log(sandbox.name);
Retrieving sandboxes — name replaces sandboxId
// Stable (1.x)const sandbox = awaitSandbox.get({ sandboxId: 'sbx_abc123' });
// Beta (2.x) — retrieves by name.const sandbox = awaitSandbox.get({ name: 'my-dev-env' });
// Pass `resume: true` to to automatically resume the sandbox. Otherwise, it will// be resumed when the next command is run.const sandbox = awaitSandbox.get({ name: 'my-dev-env', resume: false });
Listing sandboxes — pagination and filtering changes
// Stable (1.x): used since/until for paginationconst {
json: { sandboxes }
} = awaitSandbox.list({ since, until });
// Beta (2.x): cursor-based pagination, new filtering paramsconst { sandboxes, pagination } = awaitSandbox.list({
cursor: pagination.next, // string token (replaces since/until)namePrefix: 'my-app-', // Filter by name prefixsortBy: 'name'// "createdAt" (default) or "name"
});
Listing snapshots — new name filter
// Beta (2.x): filter snapshots by sandbox nameconst { snapshots } = awaitSnapshot.list({
name: 'my-dev-env'// Only snapshots belonging to this sandbox
});
Auto-resume for persistent sandboxes
If a sandbox created with persistent: true is stopped, and you call
runCommand, writeFiles, or similar SDK methods with the same sandbox name, the SDK automatically
starts a new session and retries the operation. You do not need to resume
manually.
New Session class
// Access the current running VM sessionconst session = sandbox.currentSession();
console.log(session.sessionId);
console.log(session.status); // "pending" | "running" | "stopping" | "stopped" | ...
New sandbox.update() method (replaces updateNetworkPolicy)
// Stable (1.x)await sandbox.updateNetworkPolicy('deny-all');
// Beta (2.x) — updateNetworkPolicy still works but is deprecatedawait sandbox.update({
networkPolicy: 'deny-all',
persistent: false,
resources: { vcpus: 4 },
timeout: ms('30m')
});
New sandbox.delete() method
// Permanently remove a sandbox and all its snapshotsawait sandbox.delete();
New sandbox.listSessions() and sandbox.listSnapshots()
// List all VM sessions for this sandboxconst sessions = await sandbox.listSessions();
// List snapshots belonging to this sandboxconst snapshots = await sandbox.listSnapshots();
CLI Changes (3.0.0-beta)
Key differences from the stable CLI:
All commands now use sandbox name instead of sandbox ID.
sandbox rm / sandbox removepermanently deletes the sandbox.
New: sandbox sessions command to manage sessions.
New: sandbox create --name <name> to set a sandbox name.
New: sandbox create --non-persistent to disable state persistence.
New: sandbox run --stop to stop the session when the command exits.
New: sandbox run --name <name> resumes from an existing sandbox if it exists.
Breaking: sandbox run --rm now deletes the sandbox (previously just stopped it).
New: sandbox snapshots list --name <name> to filter snapshots by sandbox name.
New: sandbox config list <name> to view sandbox configuration.
New: sandbox config vcpus <name> <count> to update vCPUs.
New: sandbox config timeout <name> <duration> to update timeout.
New: sandbox config persistent <name> <true|false> to toggle persistence.
sandbox cp now uses <sandbox_name>:path instead of <sandbox_id>:path.
sandbox ls supports --name-prefix and --sort-by filtering.