Northflank

A sandbox per Northflank deployment service, driven through @northflank/js-client. Streaming exec, PTYs, container snapshots, GPUs and metrics.

Installation

@northflank/js-client is an optional peer dependency of sbox-sdk, loaded lazily on first use. Node-only (the SDK uses Node streams and HTTP/2).

pnpm add sbox-sdk @northflank/js-client

Authentication

An API token + the project the sandbox services live in.

Authentication variables for the northflank adapter.
OptionEnv varRequiredDescription
tokenNORTHFLANK_TOKENrequiredNorthflank API token.
projectId—requiredProject the sandbox services are created in.
deploymentPlan—optionalCompute plan for new sandboxes (default "nf-compute-200").
image—optionalDefault base image (default "ubuntu:22.04").
ephemeralStorageMB—optionalEphemeral storage in MB for new sandboxes (default 2048).

Create an API token in the Northflank team settings and an existing project. The live tests read NORTHFLANK_API_TOKEN, NORTHFLANK_PROJECT_ID and optionally NORTHFLANK_TEAM_ID, NORTHFLANK_PLAN.

Quickstart

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

const client = createSandboxClient({
  provider: northflank({
    token: process.env.NORTHFLANK_API_TOKEN!,
    projectId: process.env.NORTHFLANK_PROJECT_ID!,
  }),
});

const sandbox = await client.create({
  template: "ubuntu:24.04",
  resources: { vcpus: 2, memoryMB: 4096 },
  lifecycle: { maxAgeMs: 2 * 60 * 60_000 }, // service expires after 2 h
});
for await (const ev of sandbox.commands.run("apt-get update")) {
  if (ev.type === "stdout") process.stdout.write(ev.data);
}
await sandbox.destroy();

A sandbox is a deployment service running the image (deployment.external.imagePath) with a sleep infinity command. create() waits until the deployment is COMPLETED (or fails) before returning.

How it maps

Spec fieldNorthflank
templatedeployment.external.imagePath (registryCredentials for private images)
env, inline secretsruntimeEnvironment
metadatathe service description (sbox:{json}), so list() can filter it
resources.vcpus/memoryMBsmallest fitting billing.deploymentPlan (override with deploymentPlan)
resources.diskMB/shmMBstorage.ephemeralStorage.storageSize, storage.shmSize
resources.gpu/gpuCountbilling.gpu.configuration
lifecycle.maxAgeMscreateOptions.expiryTime (5 min – 7 days; the service is paused, then deleted)
portspublic HTTP ports
volumescreateOptions.volumesToAttach (the volume's mount is set to mountPath)
fromSnapshotdeployment.containerSnapshot.restoreFrom
name / idempotencyKeythe service name; without a name it is derived from the idempotency key, so a retried create reuses the service

Exec, PTY and files

  • commands.run() streams over exec.execServiceSession with separate stdout/stderr and the real exit code; writeStdin feeds the session's stdin.
  • Each command records its shell pid in /tmp/.sbox-<id>.pid; kill() and timeoutMs SIGKILL that process tree.
  • sandbox.pty.create() is a tty exec session (sendInputToCurrentCommand, resizeTerminal). pty.connect(id) works within the same process.
  • Files use the SDK's streaming copy (upload/download.serviceStream.files), which needs tar and cat in the image.
  • No per-command cwd/env: the core wraps the command (perCommandEnvCwd: false).

Previews

ports.expose(port) adds a public HTTP port without touching the others (the update endpoint replaces the whole list, so the adapter merges) and returns https://<port-name>--<service>.code.run. visibility: "private" protects the port with HTTP basic auth:

const p = await sandbox.ports.expose(3000, { visibility: "private" });
// p.auth = { type: "header", headers: { authorization: "Basic …" } }

Basic auth is a header, so a private preview can't be opened from a browser WebSocket (previewBrowserAuth: false).

Snapshots and fork

snapshots.create() takes a Northflank container snapshot (filesystem only, includesMemory: false) and waits for it to succeed. snapshots.restore() redeploys the service from it, client.create({ fromSnapshot }) boots a new service from it, and sandbox.fork() snapshots and restores into new services.

Container snapshots need a cluster with the Kata runtime (sandboxed workloads). gVisor/Kata are a cluster setting on Northflank (sandboxing.defaultSandbox.services for BYOC clusters), not a per-service option, so the adapter has no runtime-class option.

Templates

Northflank builds images from a git repository or a bundle URL, so a raw Dockerfile + local context can't be uploaded. templates.build() accepts { image } only: the template id is the image path plus any env/startCmd/workdir, encoded into the id so it works across processes.

const tpl = await client.templates.build({
  source: { image: "node:22" },
  env: { NODE_ENV: "production" },
});
const sb = await client.create({ template: tpl });

Options

Prop

Type

Capabilities

CapabilityLevelNotes
listnativecursor pagination, metadata filter
pausenativescale to zero; memory and disk are lost
streaming / stdin / ptynativeexec sessions
exposePort / privatePreviewnativepublic port; basic auth when private
snapshot / forknativecontainer snapshots (Kata clusters)
gpu / metricsnativebilling.gpu; metrics.getServiceMetrics
templateBuildnativeimage sources only
stop / setTimeoutunsupportedmaxAgeMs is set once at create
sshunsupportedthe API does not expose an SSH endpoint
volumes / secretsVaultunsupportedattach existing volumes via spec.volumes
regionunsupportedfixed by the project's cluster

Flags: perCommandEnvCwd: false, preservesMemoryOnPause: false, previewModel: "subdomain", templateSources: ["image"].

Limitations

  • pause() scales to zero: the container and its ephemeral disk go away. Mount a volume for data that must survive.
  • deployment.ssh.enabled exists, but the API gives no host/port to connect to, so ssh stays unsupported.
  • Plan selection from resources uses a built-in table of Northflank plan ids; pass deploymentPlan to pin one.

On this page