What it is
Cloudflare Wrangler is a command-line interface (CLI) tool for developing, deploying, and managing applications on Cloudflare’s Workers platform. You reach for it when you want to build serverless applications that run on Cloudflare’s edge network.
Installation
Linux/macOS
npm install -g wrangler
or
yarn global add wrangler
Windows
npm install -g wrangler
or
yarn global add wrangler
Core Concepts
- Workers: Serverless JavaScript, TypeScript, or WebAssembly functions that run on Cloudflare’s global network.
wrangler.toml: The configuration file for your Worker project, defining name, account ID, routes, and other settings.wrangler.dev: A local development server that simulates the Cloudflare Workers environment, allowing for rapid iteration.wrangler publish: Deploys your Worker to Cloudflare’s production environment.- Routes: URLs that are configured to trigger your Worker.
Commands / Usage
Project Initialization
-
Create a new Worker project:
wrangler generate my-workerCreates a new Worker project directory named
my-workerwith a basic template. -
Create a new Worker project from a template:
wrangler generate my-worker https://github.com/cloudflare/workers-templates/tree/main/typescriptCreates a new Worker project from a specified Git repository template.
Development
-
Start a local development server:
cd my-worker wrangler devRuns your Worker locally and watches for file changes. Access your Worker at
http://localhost:8787. -
Start a local development server with a specific IP/port:
wrangler dev --host 127.0.0.1 --port 9000Runs your Worker locally on
http://127.0.0.1:9000. -
Start a local development server with a specific upstream Worker:
wrangler dev --upstream my-other-workerWhen developing a Worker that might interact with another Worker, this command allows you to specify an upstream Worker to proxy requests to.
-
Start a local development server with a specific upstream Worker and route:
wrangler dev --upstream my-other-worker --route "https://api.example.com/*"Proxies requests matching the route to the specified upstream Worker.
Deployment
-
Publish your Worker to production:
cd my-worker wrangler publishDeploys the current state of your Worker to Cloudflare.
-
Publish with a specific version message:
wrangler publish --message "Added new feature X"Deploys your Worker and associates a descriptive message with the deployment.
-
Publish to a specific environment:
wrangler publish --env stagingDeploys your Worker to a configured staging environment. Requires environments to be set up in
wrangler.toml.
Configuration and Account Management
-
Configure your Cloudflare account:
wrangler configPrompts you to log in to your Cloudflare account and sets up authentication.
-
Configure your Worker project:
wrangler initInitializes a
wrangler.tomlfile in the current directory if one doesn’t exist. -
View your
wrangler.tomlconfiguration:wrangler config --showDisplays your current Wrangler configuration, including your account ID and API token.
-
Login to Cloudflare:
wrangler loginAuthenticates your Wrangler CLI with your Cloudflare account.
Environment Management
-
List configured environments:
wrangler env listShows the environments defined in your
wrangler.tomlfile. -
Create a new environment:
wrangler env create productionAdds a new environment named
productionto yourwrangler.toml. -
Delete an environment:
wrangler env delete stagingRemoves the
stagingenvironment from yourwrangler.toml. -
Set environment variables for an environment:
wrangler env vars set MY_API_KEY --env productionSets an environment variable named
MY_API_KEYfor theproductionenvironment. You will be prompted for the value. -
List environment variables for an environment:
wrangler env vars list --env productionDisplays environment variables set for the
productionenvironment. -
Delete environment variables for an environment:
wrangler env vars delete MY_API_KEY --env productionRemoves the
MY_API_KEYenvironment variable from theproductionenvironment.
KV (Key-Value) Store Management
-
Create a new KV namespace:
wrangler kv:namespace create my-kv-storeCreates a new KV namespace named
my-kv-storeand adds it to yourwrangler.toml. -
List KV namespaces:
wrangler kv:namespace listLists all KV namespaces associated with your account.
-
Put a value into a KV namespace:
wrangler kv:key put "user:123" "John Doe" --binding MY_KV_STOREStores the value "John Doe" with the key "user:123" in the KV namespace bound to
MY_KV_STORE. -
Get a value from a KV namespace:
wrangler kv:key get "user:123" --binding MY_KV_STORERetrieves the value associated with the key "user:123" from the KV namespace bound to
MY_KV_STORE. -
List keys in a KV namespace:
wrangler kv:key list --binding MY_KV_STORELists all keys within the KV namespace bound to
MY_KV_STORE. -
Delete a key from a KV namespace:
wrangler kv:key delete "user:123" --binding MY_KV_STORERemoves the key "user:123" and its associated value from the KV namespace bound to
MY_KV_STORE.
Durable Objects Management
-
Create a new Durable Object:
wrangler do:new <name>Creates a new Durable Object named
<name>. -
List Durable Objects:
wrangler do:listLists all Durable Objects in your account.
Cron Triggers
-
Create a cron trigger:
wrangler cron:create "0 * * * *" --cron-description "Every hour"Creates a cron trigger that runs every hour.
-
List cron triggers:
wrangler cron:listLists all configured cron triggers for your Worker.
-
Delete a cron trigger:
wrangler cron:delete "0 * * * *"Deletes the specified cron trigger.
Tail (Logging)
-
Tail logs from your Worker:
wrangler tailConnects to your Worker in production and streams logs in real-time.
-
Tail logs with a specific number of recent events:
wrangler tail --count 100Streams logs and includes the last 100 events that occurred before connecting.
-
Tail logs from a specific environment:
wrangler tail --env stagingStreams logs from the Worker deployed to the
stagingenvironment.
Other Commands
-
Validate your Worker configuration:
wrangler validateChecks your
wrangler.tomlfor common configuration errors. -
Get information about your Worker:
wrangler whoamiDisplays information about your authenticated Cloudflare account.
-
Generate a type definition file:
wrangler generate types worker-types.tsGenerates a TypeScript file (
worker-types.ts) that includes types for your Worker’s environment variables, KV namespaces, Durable Objects, and more.
Common Patterns
-
Develop a Worker with a KV store and Durable Object:
# In wrangler.toml [vars] MY_KV_NAMESPACE = "MY_KV_STORE" [[durable_objects]] name = "MY_DO" script = "my-do-worker" # if your DO is in a separate file # In your main worker file (e.g., index.ts) export default { async fetch(request, env, ctx) { const kvValue = await env.MY_KV_STORE.get("some_key"); const doStub = env.MY_DO.get(env.MY_DO.idFromName("some_id")); await doStub.fetch("http://example.com"); return new Response(kvValue); }, }; # Deploy and run wrangler kv:namespace create MY_KV_STORE wrangler do:new MY_DO wrangler publish -
Deploy a Worker to a staging environment and test it:
# Ensure staging environment is configured in wrangler.toml # wrangler env create staging # wrangler env vars set API_URL --env staging http://localhost:3000 wrangler publish --env staging curl https://staging.your-worker-name.your-account.workers.dev -
Watch for local changes and automatically rebuild/redeploy (requires
nodemonor similar):# Using nodemon for automatic restarts npm install -g nodemon nodemon --watch src --ext ts --exec "wrangler dev"This setup automatically restarts
wrangler devwhenever TypeScript files in thesrcdirectory change. -
Get a list of all deployed Worker versions:
wrangler deployments listUseful for auditing or rolling back to previous versions.
-
Rollback to a previous deployment:
wrangler deployments rollback <deployment-id>Reverts your Worker to a specific deployment ID.
Gotchas
wrangler.tomlis essential: Many commands require awrangler.tomlfile to be present and correctly configured (especiallyaccount_id). If it’s missing, Wrangler will often prompt you to create it.account_idis mandatory: You must set your Cloudflareaccount_idinwrangler.tomlfor most commands to work. You can find this on your Cloudflare dashboard.- Environment variables are bound, not global: Environment variables, KV namespaces, and Durable Objects are accessed via the
envobject passed to your Worker’sfetchhandler. They are not global variables. - Local
wrangler devvs. Production: Whilewrangler devis a great simulator, there can be subtle differences in behavior compared to the live production environment. Always test thoroughly in production. - KV namespace binding names: When using KV or Durable Objects, ensure the binding name in
wrangler.toml(e.g.,MY_KV_STORE) matches how you reference it in your Worker code (e.g.,env.MY_KV_STORE). wrangler.devport conflicts: If port 8787 is already in use,wrangler devwill try to find another available port. Check the output for the correct URL.- API Token Permissions: When setting up
wrangler configorwrangler login, ensure the API token you use has the necessary permissions for Workers management, KV, Durable Objects, etc. Insufficient permissions are a common source of deployment errors. - Module Workers vs. Script Workers: Wrangler supports both.
wrangler.tomlhas atypefield ("module"or"javascript"). Default is"javascript". Module Workers are generally preferred for modern development.