Skip to content
Cloudflare Docs

API examples

API examples

As a platform, you deploy Workers on behalf of your customers programmatically. The following examples show how to use the REST API and TypeScript SDK.

Prerequisites

Before using these examples, you need:

  • Your Account ID - Found in the Cloudflare dashboard URL or API settings
  • A dispatch namespace - Created via the dashboard
  • An API token with Workers permissions - Create one at API Tokens

For SDK examples, install the Cloudflare SDK:

Terminal window
npm install cloudflare

Deploy a user Worker

Upload a Worker script to your dispatch namespace. This is the primary operation your platform performs when customers deploy code.

Terminal window
# First, create the worker script file
cat > worker.mjs << 'EOF'
export default {
async fetch(request, env, ctx) {
return new Response("Hello from user Worker!");
},
};
EOF
# Deploy using multipart form (required for ES modules)
curl -X PUT "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/dispatch/namespaces/$NAMESPACE_NAME/scripts/$SCRIPT_NAME" \
-H "Authorization: Bearer $API_TOKEN" \
-F 'metadata={"main_module": "worker.mjs"};type=application/json' \
-F 'worker.mjs=@worker.mjs;type=application/javascript+module'

Deploy with bindings and tags

Use bindings to give each user Worker its own resources like a KV store or database. Use tags to organize Workers by customer ID, project ID, or plan type for bulk operations.

The following example shows how to deploy a Worker with its own KV namespace and tags attached:

Terminal window
curl -X PUT "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/dispatch/namespaces/$NAMESPACE_NAME/scripts/$SCRIPT_NAME" \
-H "Authorization: Bearer $API_TOKEN" \
-F 'metadata={"main_module": "worker.mjs", "bindings": [{"type": "kv_namespace", "name": "MY_KV", "namespace_id": "your-kv-namespace-id"}], "tags": ["customer-123", "production", "pro-plan"], "compatibility_date": "2024-01-01"};type=application/json' \
-F 'worker.mjs=@worker.mjs;type=application/javascript+module'

For more information, refer to Bindings and Tags.

Deploy a Worker with static assets

Deploy a Worker that serves static files (HTML, CSS, JavaScript, images). This is a three-step process:

  1. Create an upload session with a manifest of files
  2. Upload the asset files
  3. Deploy the Worker with the assets binding

For more details on static assets configuration and options, refer to Static assets.

Step 1: Create upload session

Terminal window
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/dispatch/namespaces/$NAMESPACE_NAME/scripts/$SCRIPT_NAME/assets-upload-session" \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"manifest": {
"/index.html": {
"hash": "<sha256-hash-first-16-bytes-hex>",
"size": 1234
},
"/styles.css": {
"hash": "<sha256-hash-first-16-bytes-hex>",
"size": 567
}
}
}'

The response includes a jwt token and buckets array indicating which files need uploading.

Step 2: Upload assets

Terminal window
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/assets/upload?base64=true" \
-H "Authorization: Bearer $JWT_FROM_STEP_1" \
-F '<hash1>=<base64-encoded-content>' \
-F '<hash2>=<base64-encoded-content>'

Step 3: Deploy Worker with assets

Terminal window
curl -X PUT "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/dispatch/namespaces/$NAMESPACE_NAME/scripts/$SCRIPT_NAME" \
-H "Authorization: Bearer $API_TOKEN" \
-F 'metadata={"main_module": "worker.mjs", "assets": {"jwt": "<completion-token>"}, "bindings": [{"type": "assets", "name": "ASSETS"}]};type=application/json' \
-F 'worker.mjs=export default { async fetch(request, env) { return env.ASSETS.fetch(request); } };type=application/javascript+module'

List Workers in a namespace

Retrieve all user Workers deployed to a namespace.

Terminal window
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/dispatch/namespaces/$NAMESPACE_NAME/scripts" \
-H "Authorization: Bearer $API_TOKEN"

Delete Workers by tag

Delete all Workers matching a tag filter. This is useful when a customer deletes their account and you need to remove all their Workers at once.

Delete all Workers tagged with customer-123:

Terminal window
curl -X DELETE "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/dispatch/namespaces/$NAMESPACE_NAME/scripts?tags=customer-123:yes" \
-H "Authorization: Bearer $API_TOKEN"

Delete a single Worker

Delete a specific Worker by name.

Terminal window
curl -X DELETE "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/dispatch/namespaces/$NAMESPACE_NAME/scripts/$SCRIPT_NAME" \
-H "Authorization: Bearer $API_TOKEN"