Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/wrangler/api.md
1# Wrangler Programmatic API23Node.js APIs for testing and development.45## startWorker (Testing)67Starts Worker with real local bindings for integration tests. Stable API (replaces `unstable_startWorker`).89```typescript10import { startWorker } from "wrangler";11import { describe, it, before, after } from "node:test";12import assert from "node:assert";1314describe("worker", () => {15let worker;1617before(async () => {18worker = await startWorker({19config: "wrangler.jsonc",20environment: "development"21});22});2324after(async () => {25await worker.dispose();26});2728it("responds with 200", async () => {29const response = await worker.fetch("http://example.com");30assert.strictEqual(response.status, 200);31});32});33```3435### Options3637| Option | Type | Description |38|--------|------|-------------|39| `config` | `string` | Path to wrangler.jsonc |40| `environment` | `string` | Environment name from config |41| `persist` | `boolean \| { path: string }` | Enable persistent state |42| `bundle` | `boolean` | Enable bundling (default: true) |43| `remote` | `false \| true \| "minimal"` | Remote mode: `false` (local), `true` (full remote), `"minimal"` (remote bindings only) |4445### Remote Mode4647```typescript48// Local mode (default) - fast, simulated49const worker = await startWorker({ config: "wrangler.jsonc" });5051// Full remote mode - production-like, slower52const worker = await startWorker({53config: "wrangler.jsonc",54remote: true55});5657// Minimal remote mode - remote bindings, local Worker58const worker = await startWorker({59config: "wrangler.jsonc",60remote: "minimal"61});62```6364## getPlatformProxy6566Emulate bindings in Node.js without starting Worker.6768```typescript69import { getPlatformProxy } from "wrangler";7071const { env, dispose, caches } = await getPlatformProxy<Env>({72configPath: "wrangler.jsonc",73environment: "production",74persist: { path: ".wrangler/state" }75});7677// Use bindings78const value = await env.MY_KV.get("key");79await env.DB.prepare("SELECT * FROM users").all();80await env.ASSETS.put("file.txt", "content");8182// Platform APIs83await caches.default.put("https://example.com", new Response("cached"));8485await dispose();86```8788Use for unit tests (test functions, not full Worker) or scripts that need bindings.8990## Type Generation9192Generate types from config: `wrangler types` → creates `worker-configuration.d.ts`9394## Event System9596Listen to Worker lifecycle events for advanced workflows.9798```typescript99import { startWorker } from "wrangler";100101const worker = await startWorker({102config: "wrangler.jsonc",103bundle: true104});105106// Bundle events107worker.on("bundleStart", (details) => {108console.log("Bundling started:", details.config);109});110111worker.on("bundleComplete", (details) => {112console.log("Bundle ready:", details.duration);113});114115// Reconfiguration events116worker.on("reloadStart", () => {117console.log("Worker reloading...");118});119120worker.on("reloadComplete", () => {121console.log("Worker reloaded");122});123124await worker.dispose();125```126127### Dynamic Reconfiguration128129```typescript130import { startWorker } from "wrangler";131132const worker = await startWorker({ config: "wrangler.jsonc" });133134// Replace entire config135await worker.setConfig({136config: "wrangler.staging.jsonc",137environment: "staging"138});139140// Patch specific fields141await worker.patchConfig({142vars: { DEBUG: "true" }143});144145await worker.dispose();146```147148## unstable_dev (Deprecated)149150Use `startWorker` instead.151152## Multi-Worker Registry153154Test multiple Workers with service bindings.155156```typescript157import { startWorker } from "wrangler";158159const auth = await startWorker({ config: "./auth/wrangler.jsonc" });160const api = await startWorker({161config: "./api/wrangler.jsonc",162bindings: { AUTH: auth } // Service binding163});164165const response = await api.fetch("http://example.com/api/login");166// API Worker calls AUTH Worker via env.AUTH.fetch()167168await api.dispose();169await auth.dispose();170```171172## Best Practices173174- Use `startWorker` for integration tests (tests full Worker)175- Use `getPlatformProxy` for unit tests (tests individual functions)176- Use `remote: true` when debugging production-specific issues177- Use `remote: "minimal"` for faster tests with real bindings178- Enable `persist: true` for debugging (state survives runs)179- Run `wrangler types` after config changes180- Always `dispose()` to prevent resource leaks181- Listen to bundle events for build monitoring182- Use multi-worker registry for testing service bindings183184## See Also185186- [README.md](./README.md) - CLI commands187- [configuration.md](./configuration.md) - Config188- [patterns.md](./patterns.md) - Testing patterns189