Skip to main content
Wrangler exports a programmatic API that allows you to use it as a library in your Node.js applications. This is useful for building custom development tools, testing frameworks, and deployment pipelines.

Installation

npm install wrangler

Core Functions

startWorker()

Starts a local development server for a Worker.
import { startWorker } from "wrangler";

const worker = await startWorker({
  name: "my-worker",
  entrypoint: "./src/index.ts",
  compatibilityDate: "2024-01-01",
});

const response = await worker.fetch("/");
console.log(await response.text());

await worker.dispose();
options
StartDevWorkerInput
required
Configuration options for the dev server
Worker
object
The worker instance

Example Usage

import { startWorker } from "wrangler";

const worker = await startWorker({
  name: "my-api",
  entrypoint: "./src/index.ts",
  compatibilityDate: "2024-01-01",
  bindings: {
    DB: {
      type: "d1",
      id: "my-database",
    },
    MY_KV: {
      type: "kv_namespace",
      id: "my-kv-namespace",
    },
  },
  dev: {
    persist: ".wrangler/state",
    server: {
      port: 8787,
    },
  },
});

// Wait for worker to be ready
await worker.ready;

// Make a request
const response = await worker.fetch("/api/users");
console.log(await response.json());

// Trigger a scheduled event
await worker.scheduled();

// Clean up
await worker.dispose();

unstable_dev()

This API is experimental and may change in future releases.
Starts a Wrangler dev server for testing purposes.
import { unstable_dev } from "wrangler";

const worker = await unstable_dev("src/index.ts", {
  config: "wrangler.toml",
  env: "staging",
  local: true,
});

const response = await worker.fetch("/");
console.log(response.status);

await worker.stop();
script
string
required
Path to the worker script
options
Unstable_DevOptions
Unstable_DevWorker
object

getPlatformProxy()

Get proxy objects for Cloudflare platform bindings in Node.js.
import { getPlatformProxy } from "wrangler";

const { env, dispose } = await getPlatformProxy({
  configPath: "wrangler.toml",
});

// Use bindings
const value = await env.MY_KV.get("key");

await dispose();
options
GetPlatformProxyOptions
PlatformProxy
object
// Example: Use in a test framework
import { getPlatformProxy } from "wrangler";
import { describe, it, expect, beforeAll, afterAll } from "vitest";

let env, dispose;

beforeAll(async () => {
  ({ env, dispose } = await getPlatformProxy());
});

afterAll(async () => {
  await dispose();
});

it("should store and retrieve from KV", async () => {
  await env.MY_KV.put("test-key", "test-value");
  const value = await env.MY_KV.get("test-key");
  expect(value).toBe("test-value");
});

Configuration Utilities

unstable_readConfig()

Read and parse a Wrangler configuration file.
import { unstable_readConfig } from "wrangler";

const config = unstable_readConfig({
  config: "wrangler.toml",
  env: "production",
});

console.log(config.name); // Worker name
console.log(config.compatibility_date); // Compatibility date
options
object
config
string
Path to config file
env
string
Environment to use
config
Config
Parsed configuration object

unstable_getWorkerNameFromProject()

Get the worker name from a project directory.
import { unstable_getWorkerNameFromProject } from "wrangler";

const name = unstable_getWorkerNameFromProject("/path/to/project");
console.log(name); // "my-worker"

Binding Utilities

convertConfigBindingsToStartWorkerBindings()

Convert Wrangler config bindings to the format expected by startWorker().
import { 
  unstable_readConfig, 
  convertConfigBindingsToStartWorkerBindings 
} from "wrangler";

const config = unstable_readConfig({ config: "wrangler.toml" });
const bindings = convertConfigBindingsToStartWorkerBindings(config);

Event Types

The Worker object emits events during its lifecycle.

ErrorEvent

Emitted when an error occurs.
type ErrorEvent = {
  type: "error";
  error: SerializedError;
};

ConfigUpdateEvent

Emitted when configuration changes.
type ConfigUpdateEvent = {
  type: "config-update";
  config: StartDevWorkerOptions;
};

BundleStartEvent / BundleCompleteEvent

Emitted during bundling.
type BundleStartEvent = {
  type: "bundle-start";
};

type BundleCompleteEvent = {
  type: "bundle-complete";
  bundle: Bundle;
};

ReadyEvent

Emitted when the worker is ready.
type ReadyEvent = {
  type: "ready";
  url: URL;
  inspectorUrl?: URL;
};

Pages API

unstable_pages.deploy()

Deploy a Pages project programmatically.
import { unstable_pages } from "wrangler";

const result = await unstable_pages.deploy({
  directory: "./dist",
  projectName: "my-site",
  accountId: "your-account-id",
});

console.log(`Deployed to: ${result.url}`);

mTLS Certificate Management

uploadMTlsCertificate()

Upload an mTLS certificate from a buffer.
import { uploadMTlsCertificate } from "wrangler";
import fs from "fs";

const cert = fs.readFileSync("cert.pem");
const key = fs.readFileSync("key.pem");

const result = await uploadMTlsCertificate(
  "my-cert",
  cert,
  key,
  "account-id"
);

listMTlsCertificates()

List all mTLS certificates.
import { listMTlsCertificates } from "wrangler";

const certs = await listMTlsCertificates("account-id");

deleteMTlsCertificate()

Delete an mTLS certificate.
import { deleteMTlsCertificate } from "wrangler";

await deleteMTlsCertificate("cert-id", "account-id");

Type Definitions

All types are exported from the main package:
import type {
  Worker,
  StartDevWorkerInput,
  StartDevWorkerOptions,
  Binding,
  Trigger,
  GetPlatformProxyOptions,
  PlatformProxy,
} from "wrangler";

Build docs developers (and LLMs) love