Cloudflare Snippets are now generally available on all paid plans. The feature lets users attach lightweight JavaScript "code rules" to HTTP traffic at no additional cost, and GA brings a new code editor, higher limits alongside other Cloudflare Rules products, and visibility into triggered Snippets through Trace.
Where Snippets sit in the request path
Snippets extend Cloudflare Rules with executable logic. The Ruleset Engine evaluates a rule, invokes the associated code, and that code runs on the Workers runtime — before a request reaches the origin or after a response comes back. They are intended for cases where built-in rule actions are not sufficient, without requiring a full Worker.
Capabilities include:
- Execution optimized for speed through the Ruleset Engine and Workers runtime.
- Matching on
URI,user-agent,cookies, headers and other request fields. - Sequential execution: multiple Snippets can run on one request, applying modifications step by step.
- Inheritance of request modifications made by other Cloudflare products through native Rules integration.
- JavaScript and Web APIs, plus Workers runtime features such as the
fetch API,cache API,request.cfobject andHTMLRewriter. - Automated deployment and versioning via Terraform.
From alpha to 2 million requests per second
The feature was announced during Developer Week 2022 as a way to make flexible HTTP traffic modifications without a full Worker. An alpha launch followed in 2023 with hundreds of testers. In 2024 traffic grew 7x, reaching 17,000 requests per second, alongside Terraform support and a production-grade backend. GA in 2025 adds the new editor, increased limits, Trace integration, and a platform handling over 2 million requests per second at peak, with Secrets Store integration rolling out this summer.

Debugging with Trace, secrets via Secrets Store
Cloudflare Trace now reports exactly which Snippets were triggered on a given request, which helps verify that logic ran as intended and shows how Snippets interact with other products in the request pipeline — useful both when tuning header logic and when troubleshooting routing.
In the third quarter, Snippets will be able to read API keys, authentication tokens and other sensitive values from Secrets Store directly, removing plaintext secrets from code. Secrets will be configured via the dashboard or API under a new "Settings" button.


Choosing between Snippets and Workers
Both products allow programmatic traffic control, but target different problems.
|
Feature |
Snippets |
Workers |
|
Execute scripts based on request attributes (headers, geolocation, cookies, etc.) |
✅ |
❌ |
|
Modify HTTP requests/responses or serve a different response |
✅ |
✅ |
|
Add, remove, or rewrite headers dynamically |
✅ |
✅ |
|
Cache assets at the edge |
✅ |
✅ |
|
Route traffic dynamically between origins |
✅ |
✅ |
|
Authenticate requests, pre-sign URLs, run A/B testing |
✅ |
✅ |
|
Perform compute-intensive tasks (e.g., AI inference, image processing) |
❌ |
✅ |
|
Store persistent data (e.g., KV, Durable Objects, D1) |
❌ |
✅ |
|
Deploy via CLI (Wrangler) |
❌ |
✅ |
|
Use TypeScript, Python, Rust or other programming languages |
❌ |
✅ |
Snippets fit when:
- You need ultra-fast conditional traffic modifications directly on Cloudflare's network.
- You want to extend Cloudflare Rules beyond built-in actions.
- You need free, unlimited invocations within the execution limits.
- You are migrating from VCL, Akamai's EdgeWorkers, or on-premise logic.
Workers fit when:
- The application requires state management, Developer Platform product integrations, or high compute limits.
- You are building APIs, full-stack applications, or complex workflows.
- You need logging, debugging tools, CLI support, and gradual rollouts.
A detailed guide covers best practices for the undecided.
Use cases
Each script below can be scoped with the Rules language, so you control precisely which requests trigger it.
Header modification
Inject custom headers, strip unnecessary ones, and adjust values on the fly:
export default {
async fetch(request) {
const timestamp = Date.now().toString(16); // convert timestamp to HEX
const modifiedRequest = new Request(request, { headers: new Headers(request.headers) });
modifiedRequest.headers.set("X-Hex-Timestamp", timestamp); // send HEX timestamp to upstream
const response = await fetch(modifiedRequest);
const newResponse = new Response(response.body, response); // make response from upstream mutable
newResponse.headers.append("x-snippets-hello", "Hello from Cloudflare Snippets"); // add new response header
newResponse.headers.delete("x-header-to-delete"); // delete response header
newResponse.headers.set("x-header-to-change", "NewValue"); // replace the value of existing response header
return newResponse;
},
};
Planned maintenance page
Route traffic to a maintenance page while the origin undergoes scheduled work:
export default {
async fetch(request) { // for all matching requests, return predefined HTML response with 503 status code
return new Response(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>We'll Be Right Back!</title>
<style> body { font-family: Arial, sans-serif; text-align: center; padding: 20px; } </style>
</head>
<body>
<h1>We'll Be Right Back!</h1>
<p>Our site is undergoing maintenance. Check back soon!</p>
</body>
</html>
`, { status: 503, headers: { "Content-Type": "text/html" } });
}
};
Failover to a backup origin
Reroute requests automatically when the primary origin returns an unexpected response:
export default {
async fetch(request) {
const response = await fetch(request); // send original request to the origin
if (!response.ok && !response.redirected) { // if response is not 200 OK or a redirect, send to another origin
const newRequest = new Request(request); // clone the original request to construct a new request
newRequest.headers.set("X-Rerouted", "1"); // add a header to identify a re-routed request at the new origin
const url = new URL(request.url); // clone the original URL
url.hostname = "backup.example.com"; // send request to a different origin / hostname
return await fetch(url, newRequest); // serve response from the backup origin
}
return response; // otherwise, serve response from the primary origin
},
};
Location-based redirects
Send visitors to region-specific sites for better localization:
export default {
async fetch(request) {
const country = request.cf.country; // identify visitor's country using request.cf property
const redirectMap = { US: "https://example.com/us", EU: "https://example.com/eu" }; // define redirects for each country
if (redirectMap[country]) return Response.redirect(redirectMap[country], 301); // redirect on match
return fetch(request); // otherwise, proceed to upstream normally
}
};
Deploying a Snippet
Snippets live in the dashboard under Rules > Snippets. The workflow:
- Go to Rules → Snippets.
- Start from a prebuilt template or write your own JavaScript.
- Configure a flexible rule to trigger the Snippet.
- Test and deploy instantly.
- Automate through the API or Terraform.



