Morph

A sandbox per Morph Cloud instance. Snapshot-first VMs with pause/resume, instant branching and cached setup chains, via the morphcloud SDK.

Maintenance-only

Morph's TypeScript SDK (morphcloud) hasn't been updated since October 2025. Only the Python SDK is still maintained. This adapter is kept correct and working on morphcloud@0.0.20, but it won't gain new Morph features. If you are starting fresh on Morph, check the state of the TypeScript SDK first.

Installation

morphcloud is an optional peer dependency of sbox-sdk, loaded lazily on first use.

pnpm add sbox-sdk morphcloud

Authentication

A single API key.

Authentication variables for the morph adapter.
OptionEnv varRequiredDescription
apiKeyMORPH_API_KEYrequiredMorphCloud API key.
baseUrl—optionalOverride the MorphCloud API base URL.
imageId—optionalDefault image to snapshot from when `template` is not a snapshot id (default "morphvm-minimal").
vcpus—optionalDefault vCPUs for a lazily-created snapshot.
memory—optionalDefault memory (MB) for a lazily-created snapshot.
diskSize—optionalDefault disk size for a lazily-created snapshot.

Morph is snapshot-first: pass a snapshot id as `template` to boot it directly, or any other value as an image to snapshot first.

morph() reads MORPH_API_KEY when you don't pass apiKey. Check the key with client.verify(). It makes one authenticated list call and never creates anything.

Usage

import { createSandboxClient } from "sbox-sdk";
import { morph } from "sbox-sdk/morph";

const client = createSandboxClient({
  provider: morph({ apiKey: process.env.MORPH_API_KEY! }),
});

const sandbox = await client.create({
  template: "morphvm-minimal", // a Morph image id, or a snapshot_… id
  resources: { vcpus: 2, memoryMB: 4096, diskMB: 16384 },
  env: { NODE_ENV: "production" },
  lifecycle: { maxAgeMs: 30 * 60_000, onIdle: "pause", autoResume: true },
});

for await (const ev of sandbox.commands.run("npm test")) {
  if (ev.type === "stdout") process.stdout.write(ev.data); // real streaming
}

await sandbox.pause(); // memory preserved
await sandbox.resume(); // same id

const [a, b] = await sandbox.fork({ count: 2 }); // instance.branch()

Morph is snapshot-first:

  • template: "snapshot_…", fromSnapshot, or a built template boots that snapshot directly.
  • Anything else is a Morph image id. The adapter keeps one base snapshot per (image, vcpus, memory, disk), found through a digest and reused on later creates, so creating a sandbox doesn't add a new snapshot each time.
  • resources only apply when booting from an image. A snapshot keeps the resources it was taken with.

How the spec maps

SpecMorph
metadata, nameinstance metadata (name is stored as sbox-name)
lifecycle.maxAgeMsttlSeconds
lifecycle.onIdlettlAction: pause → pause, stop/destroy → stop (Morph deletes the instance on stop)
lifecycle.autoResumesetWakeOn(ssh, http)
env, inline secretsadded to every command (Morph has no per-instance env)
portsexposed as HTTP services at create
idempotencyKeystored as metadata. A retry returns the instance that already exists
volumes, network (anything except allow-all), GPUs, vault secretsrejected with Validation

lifecycle.idleTimeoutMs and region have no Morph equivalent, so they're ignored.

Templates

client.templates.build() chains Snapshot.setup(cmd). Morph caches each step by chain hash, so an unchanged prefix of steps is reused. Three sources are accepted:

// 1. A Morph image id (or a snapshot id) as the base
await client.templates!.build({ source: { image: "morphvm-minimal" } });

// 2. A snapshot you already have
await client.templates!.build({ source: { snapshot: "snapshot_abc" } });

// 3. A restricted Dockerfile: FROM <morph-image-id|snapshot_id> + shell-form RUN only
const tpl = await client.templates!.build({
  source: {
    dockerfile: `FROM morphvm-minimal
RUN apt-get update && apt-get install -y nodejs
RUN npm i -g pnpm`,
  },
});
const sandbox = await client.create({ template: tpl });

Every other Dockerfile instruction (COPY, ENV, WORKDIR, CMD and so on) is rejected with Validation. The same goes for registry images, template env, workdir, user, startCmd and readyCmd. The result is a snapshot tagged with the template cache key, so templates.get(cacheKey) finds it again. Setup output goes to the SDK's own stdout and isn't forwarded to onEvent. You only get one log event per step.

Previews

Ports are Morph HTTP services named sbox-<port>.

const pub = await sandbox.ports!.expose(3000); // auth: { type: "public" }
const priv = await sandbox.ports!.expose(8080, { visibility: "private" });
// priv.auth = { type: "header", headers: { Authorization: "Bearer <MORPH_API_KEY>" } }

Morph's private mode (auth_mode: "api_key") checks for your account API key in an Authorization header. It has no query-token or signed-URL mode, so private previews don't work from a browser (previewBrowserAuth: false), and anyone you give the header to holds your full API key. Use public previews for anything that faces a browser.

Capabilities

CapabilityLevelNotes
listnativefiltered on the server by metadata
streamingnativeSSE exec with stdout and stderr kept separate
pausenativeinstance.pause() / resume(). Memory preserved, id unchanged
setTimeoutnativeinstance.setTTL()
snapshotnativeinstance.snapshot(). The name is stored in metadata. List and delete work
forknativeinstance.branch(). The intermediate snapshot is deleted afterwards
sshnativeinstance.sshKey() via ssh.cloud.morph.so
exposePortnativeexposeHttpService
privatePreviewnativeheader auth with the account API key (see above)
templateBuildnativeSnapshot.setup() chains
filesUploademulatedexec + base64, in 48 KB chunks so large files round-trip
stopunsupportedMorph's stop deletes the instance. Use pause() or destroy()
background, pty, stdin, egressControl, volumes, metrics, codeInterpreterunsupported

Flags:

  • perCommandEnvCwd: false: the core adds cwd/env to the command itself.
  • preservesMemoryOnPause: true and previewModel: "subdomain".
  • No in-place snapshots.restore(). To restore, boot a new sandbox with client.create({ fromSnapshot }).

Known limitations

  • connect() drops spec.env. Env from create() is held by the handle that create()/fork() return. A sandbox you get back with client.connect(id) doesn't have it.
  • kill() ends the stream right away (exit code 137). It then kills the remote process tree with a second exec that finds processes by a per-command marker env var. A command that clears its own environment can survive this.
  • timeoutMs is passed to Morph in whole seconds, rounded up. On timeout the adapter also kills the remote process.
  • File I/O is exec + base64, so a 5 MB write takes about 100 exec round-trips.
  • ssh.create() returns the instance's SSH key with the instance id as the user. The exact gateway username format hasn't been checked against a live account.

On this page