@computesdk/northflank

中文文档
1.1.21发布于 2026/11/06MITTypeScriptESM + CJS近 30 天下载 1万

Northflank provider for ComputeSDK - Deploy and manage compute workloads on Northflank's container platform

原始使用文档
文档目录14 节
  1. Installation
  2. Setup
  3. Quick Start
  4. Configuration
  5. Internal deployments (recommended for production)
  6. Per-sandbox options
  7. Running code
  8. Commands
  9. Filesystem
  10. Sandbox management
  11. Notes
  12. Limitations
  13. Support
  14. License

@computesdk/northflank

Northflank provider for ComputeSDK.

Each sandbox is a deployment service in your Northflank project. Commands run in the container via Northflank's exec API; ports are exposed through the service's public DNS.

Installation

npm install @computesdk/northflank

Setup

  1. Get your API token: Team settings → API → Tokens → Create API token
  2. Create (or pick) a Northflank project
  3. Set environment variables:
export NORTHFLANK_TOKEN=your_api_token_here
export NORTHFLANK_PROJECT_ID=your_project_id

Quick Start

import { northflank } from '@computesdk/northflank';

const compute = northflank({
  token: process.env.NORTHFLANK_TOKEN!,
  projectId: process.env.NORTHFLANK_PROJECT_ID!,
  runtime: 'node',
});

const sandbox = await compute.instances.create();

const result = await sandbox.runCommand(`node -e "console.log('hello from northflank')"`);
console.log(result.stdout);

await sandbox.destroy();

Configuration

interface NorthflankConfig {
  /** Northflank API token */
  token: string;
  /** Project ID (slug, e.g. "my-project") */
  projectId: string;
  /** Team ID — required for organization API tokens */
  teamId?: string;
  /** API host — defaults to https://api.northflank.com */
  host?: string;
  /** Service name prefix used to identify managed sandboxes — defaults to "computesdk-" */
  servicePrefix?: string;
  /** Container image — overrides the default for the runtime */
  image?: string;
  /** Default runtime label when none is passed to create() — any string; defaults to "node". "node" and "python" map to built-in image defaults; anything else needs `image` or `internalDeployment` */
  runtime?: string;
  /** Northflank deployment plan — defaults to "nf-compute-50" */
  deploymentPlan?: string;
  /** Default ports to expose on every sandbox */
  ports?: { name: string; internalPort: number; public?: boolean; protocol?: 'HTTP' | 'HTTP/2' | 'TCP' | 'UDP' }[];
  /** Budget in ms for the first exec to retry while the container starts (default: 120000) */
  timeout?: number;
  /** Deploy from a Northflank build service instead of an external image */
  internalDeployment?: { id: string; branch?: string; buildSHA?: string };
  /** Ephemeral storage per container in MB (e.g. 5120 for 5 GiB) */
  ephemeralStorageSize?: number;
}

By default, sandboxes pull node:20-slim / python:3.11-slim (or your image) from an external registry. For production workloads we recommend deploying from a Northflank build service in the same project — internal deployments are optimized for faster cold starts and don't depend on external registries.

const compute = northflank({
  token: process.env.NORTHFLANK_TOKEN!,
  projectId: process.env.NORTHFLANK_PROJECT_ID!,
  internalDeployment: {
    id: 'my-build-service',   // build service ID in the same project
    branch: 'main',           // optional — defaults to "main"
    buildSHA: 'latest',       // optional — defaults to "latest"
  },
});

You can also pass internalDeployment on a per-sandbox basis to create(); it overrides the config-level value. External images keep working — internalDeployment is just the recommended path.

Per-sandbox options

create() accepts these provider-specific options on top of the standard ones:

await compute.instances.create({
  runtime: 'python',                    // overrides config.runtime
  image: 'my-registry/custom:tag',      // overrides config.image and the runtime default
  ports: [{ name: 'app', internalPort: 3000 }], // overrides config.ports
  deploymentPlan: 'nf-compute-200',     // overrides config.deploymentPlan
  timeout: 180000,                      // overrides config.timeout
  envs: { LOG_LEVEL: 'debug' },         // merged into runtimeEnvironment
  name: 'my-sandbox',                   // service name (auto-prefixed)
  ephemeralStorageSize: 5120,           // 5 GiB ephemeral storage per container
});

Built-in image defaults: node → node:20-slim, python → python:3.11-slim. Any other runtime label is accepted, but you must supply image or internalDeployment — there's no default to fall back on.

Running code

There is no runCode — invoke the runtime directly through runCommand:

// Python
const py = await sandbox.runCommand(`python -c "import json; print(json.dumps({'hello': 'world'}))"`);

// Node.js
const js = await sandbox.runCommand(`node -e "console.log(JSON.stringify({hello: 'world'}))"`);

For multi-line scripts, write the file first and then invoke it:

await sandbox.filesystem.writeFile('/tmp/script.py', `
import pandas as pd
print(pd.__version__)
`);
const result = await sandbox.runCommand('python /tmp/script.py');

Commands

// Simple command
await sandbox.runCommand('ls -la');

// Working directory
await sandbox.runCommand('npm install', { cwd: '/app' });

// Environment variables
await sandbox.runCommand('echo $MY_VAR', { env: { MY_VAR: 'hello' } });

// Background execution
await sandbox.runCommand('python server.py', { background: true });

Filesystem

await sandbox.filesystem.writeFile('/tmp/hello.txt', 'hello world');
const content = await sandbox.filesystem.readFile('/tmp/hello.txt');

await sandbox.filesystem.mkdir('/tmp/data');
const entries = await sandbox.filesystem.readdir('/tmp');

const exists = await sandbox.filesystem.exists('/tmp/hello.txt');
await sandbox.filesystem.remove('/tmp/hello.txt');

Filesystem operations send paths as plain argv elements to the exec proxy (e.g. ['mkdir', '-p', '--', path]) or through Northflank's fileCopy API — no shell parsing is involved, so paths don't need escaping. Only runCommand (which actually invokes a shell) escapes the env / cwd values it interpolates.

Sandbox management

// List managed sandboxes (filtered by servicePrefix)
const sandboxes = await compute.instances.list();

// Look up by service ID (slug of the service name)
const sandbox = await compute.instances.getById('computesdk-1700000000-abc123');

// Inspect — refetches the live deployment status
const info = await sandbox.getInfo();
console.log(info.id, info.status, info.createdAt);

// Get the public URL for a port — exposes it dynamically if not already public
const url = await sandbox.getUrl({ port: 3000 });

await sandbox.destroy();

Notes

  • Services are named <servicePrefix><timestamp>-<random> (or <servicePrefix><name> if you pass name). list, getById and destroy only operate on services whose name starts with the prefix.
  • create returns immediately after the Northflank API call that creates the service — it does not wait for the container to be ready. The first runCommand (or filesystem call) may therefore take longer: it retries transient exec failures (up to timeout ms) while the pod finishes starting. Once one exec succeeds, subsequent calls run directly with no retry.
  • list returns every prefixed service regardless of deployment status. Use getInfo to check each one's state and filter client-side if needed.
  • getInfo re-reads the service from the API every call — running means the deployment status is COMPLETED and the service isn't paused; stopped means paused, PENDING, or IN_PROGRESS; error means FAILED.
  • getUrl will patch the service's port config to make the requested port public if it isn't already, then return its dns.

Limitations

  • Startup time — Northflank pulls and starts the image; expect tens of seconds.
  • Memory & CPU — bounded by the chosen deploymentPlan.
  • File persistence — files live in the container and are lost when the service is destroyed.

Support

License

MIT

版本与发布记录

@computesdk/northflank 共发布 23 个版本,首次发布于 2026/05/29;过去 12 个月发布了 23 个版本。

版本发布日期距今
1.1.21latest2026/11/064 天前
1.1.202026/11/046 天前
1.1.192026/10/2812 天前
1.1.182026/10/2317 天前
1.1.172026/10/2318 天前
1.1.162026/10/2218 天前

关于 @computesdk/northflank 的常见问题

@computesdk/northflank 是什么?

Northflank provider for ComputeSDK - Deploy and manage compute workloads on Northflank's container platform

如何安装 @computesdk/northflank?

在项目目录运行 npm install @computesdk/northflank,或使用 pnpm add / yarn add / bun add。

@computesdk/northflank 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@computesdk/northflank 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@computesdk/northflank 可以商用吗?

@computesdk/northflank 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@computesdk/northflank 的下载量有多少?

根据 npm 官方下载统计,@computesdk/northflank 在 2026/10/11 – 2026/11/09 这 30 天内被下载 10,005 次,日均约 334 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@computesdk/northflank 还在维护吗?

最新版本 1.1.21 发布于 2026/11/06(4 天前);至今共发布 23 个版本,其中过去 12 个月发布了 23 个;首次发布于 2026/05/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。