cloudflare-workers-sdk

IndexedCommit: 253a85d1 pullsUpdated Jan 30, 2026

A comprehensive toolkit for developing, testing, and deploying serverless applications on Cloudflare's global edge network. The SDK includes command-line tools, local development simulators, build plu

Install this reference
Reference

Cloudflare Workers SDK

A comprehensive toolkit for developing, testing, and deploying serverless applications on Cloudflare's global edge network. The SDK includes command-line tools, local development simulators, build plugins, and testing integrations to help you build reliable Workers applications with modern JavaScript frameworks.

Quick References

FilePurpose
packages/wrangler/src/index.tsMain Wrangler CLI entry point
packages/miniflare/src/index.tsMiniflare simulator main API
packages/vite-plugin-cloudflare/src/index.tsVite plugin exports
README.mdProject overview and getting started
packages/wrangler/README.mdWrangler CLI documentation

Packages

Packagenpm nameDescription
packages/wranglerwranglerCLI tool for building and deploying Cloudflare Workers
packages/create-cloudflarecreate-cloudflareProject scaffolding CLI (C3) for new applications
packages/miniflareminiflareLocal development simulator powered by workerd
packages/vite-plugin-cloudflare@cloudflare/vite-pluginVite plugin for Workers runtime integration
packages/vitest-pool-workers@cloudflare/vitest-pool-workersVitest testing integration for Workers
packages/kv-asset-handler@cloudflare/kv-asset-handlerKV-based static asset handling for Workers Sites
packages/workers-utils@cloudflare/workers-utilsUtility functions for common Worker operations

When to Use

  • Developing serverless applications that run at the edge with sub-millisecond latency
  • Building APIs, web applications, or static sites that need global distribution
  • Testing Workers locally with production-like runtime behavior
  • Integrating Workers into modern build pipelines (Vite, Vitest)
  • Deploying applications with zero configuration complexity
  • Creating multi-worker architectures with service bindings and queues
  • Implementing edge caching, KV storage, R2 object storage, and D1 databases

Installation

# Install Wrangler CLI
npm install -g wrangler

# Or create a new project with create-cloudflare
npm create cloudflare@latest my-app

# Install packages programmatically
npm install wrangler @cloudflare/vite-plugin
npm install -D miniflare @cloudflare/vitest-pool-workers

Best Practices

  1. Always use a configuration file - Define your Worker settings in wrangler.json, wrangler.jsonc, or wrangler.toml for reproducible deployments
  2. Test locally before deploying - Use wrangler dev or miniflare to verify functionality locally
  3. Use environment-specific configs - Leverage the env sections in your configuration to separate development, staging, and production settings
  4. Maintain compatibility dates - Set compatibility_date to control which runtime features are available
  5. Secure your secrets - Never commit secrets to version control; use wrangler secret put or environment variables
  6. Leverage bindings correctly - Use type-safe bindings for KV, R2, D1, Durable Objects, and services to access resources in your Worker

Common Patterns

Creating a new Worker project:

npx create cloudflare@latest my-app
cd my-app
npx wrangler dev

Local development with Wrangler:

# Start local development server
npx wrangler dev

# Deploy to production
npx wrangler deploy

# Deploy to a specific environment
npx wrangler deploy --env production

Using Wrangler programmatically in Node.js:

import { Miniflare } from "miniflare";

const mf = new Miniflare({
  script: `
    export default {
      async fetch(request) {
        return new Response("Hello from Workers!");
      }
    }
  `,
});

const response = await mf.dispatchFetch("http://localhost/");
console.log(await response.text());

Vite integration for modern frameworks:

// vite.config.ts
import { defineConfig } from "vite";
import { cloudflare } from "@cloudflare/vite-plugin";

export default defineConfig({
  plugins: [cloudflare()],
});

KV asset handling for static sites:

import { getAssetFromKV } from "@cloudflare/kv-asset-handler";
import manifestJSON from "__STATIC_CONTENT_MANIFEST";

const assetManifest = JSON.parse(manifestJSON);

export default {
  async fetch(request, env, ctx) {
    try {
      return await getAssetFromKV(
        { request, waitUntil: (p) => ctx.waitUntil(p) },
        {
          ASSET_NAMESPACE: env.__STATIC_CONTENT,
          ASSET_MANIFEST: assetManifest,
        }
      );
    } catch (e) {
      return new Response("Not found", { status: 404 });
    }
  },
};

Testing Workers with Vitest:

// vitest.config.ts
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers";

export default defineWorkersConfig({
  workers: [
    {
      name: "worker",
      serviceBindings: {
        external: { external: { url: "https://example.com" } },
      },
    },
  ],
  test: {
    poolOptions: {
      workers: {
        useWebWorkers: false,
      },
    },
  },
});

Configuring bindings in wrangler.json:

{
  "$schema": "node_modules/wrangler/config-schema.json",
  "name": "my-worker",
  "main": "./src/index.ts",
  "compatibility_date": "2024-01-01",
  "vars": {
    "API_KEY": "your-api-key"
  },
  "kv_namespaces": [
    {
      "binding": "YOUR_KV_NAMESPACE",
      "id": "your-kv-id"
    }
  ],
  "r2_buckets": [
    {
      "binding": "YOUR_R2_BUCKET",
      "bucket_name": "your-bucket-name"
    }
  ],
  "d1_databases": [
    {
      "binding": "DB",
      "database_name": "your-database-name"
    }
  ]
}

Miniflare for advanced testing scenarios:

import { Miniflare } from "miniflare";

const mf = new Miniflare({
  modules: true,
  script: "./src/index.js",
  kvNamespaces: ["TEST_NAMESPACE"],
  r2Buckets: ["TEST_BUCKET"],
  kvPersist: true, // Persist KV data to disk
  r2Persist: true, // Persist R2 data to disk
});

// Get KV namespace to manipulate in tests
const kv = await mf.getKVNamespace("TEST_NAMESPACE");
await kv.put("key", "value");

API Quick Reference

ExportTypeDescription
wrangler (CLI)CommandDeploy, develop, and manage Workers from the command line
create-cloudflareCLIScaffold new Cloudflare projects with templates
MiniflareClassLocal Workers simulator for testing and development
getAssetFromKVFunctionRetrieve static assets from Workers KV
serveSinglePageAppFunctionRoute all requests to index.html for SPAs
cloudflare()FunctionVite plugin for Workers runtime integration
defineWorkersConfigFunctionConfigure Vitest test pool for Workers
readConfig()FunctionLoad and validate Wrangler configuration files
mapRequestToAssetFunctionMap request URLs to KV asset keys

Key Wrangler Commands

CommandPurpose
wrangler devStart local development server with live reload
wrangler deployDeploy Worker to Cloudflare's network
wrangler pages dev <dir>Serve static assets locally with Functions
wrangler secret put <key>securely store environment variables
wrangler tailStream real-time logs from deployed Worker
wrangler init [name]Initialize a new Worker project

Configuration Core Options

OptionTypeDescription
namestringUnique identifier for your Worker
mainstringPath to Worker entry point
compatibility_datestringRuntime feature compatibility (e.g., "2024-01-01")
varsobjectEnvironment variables for your Worker
envobjectEnvironment-specific configurations
routesstring[]URL patterns for routing requests

“Explore distant worlds.”

© 2026 Oscar Gabriel