HTTP Snippets: Rules, Execution, Limits, and Deployment
How Cloudflare Snippets run edge JavaScript on matched HTTP traffic, plan limits, deployment via API or Terraform, and when to use Workers instead.
This page summarizes Cloudflare documentation on Cloudflare Snippets: short JavaScript that runs on Cloudflare’s network to customize how HTTP requests and responses are handled. Snippets are part of the Cloudflare Rules platform and are distinct from Chrome DevTools “Snippets,” which are local debug scripts in the browser.
Cloudflare Snippets: what edge HTTP snippets do
Section titled “Cloudflare Snippets: what edge HTTP snippets do”Cloudflare Snippets let you run small pieces of JavaScript at the edge to change request and response handling without serving a different user-facing URL for every case.
Cloudflare Snippets allow you to run short pieces of JavaScript code on Cloudflare’s network to customize how requests and responses are handled for your website or application. With Snippets, you can modify HTTP response headers, implement JWT validation, perform complex redirects, and more.
— Cloudflare, Cloudflare Snippets
They are described as self-contained fetch handlers suitable for programmatic cache control:
Snippets are self-contained JavaScript fetch handlers that run at the edge on your requests through Cloudflare. They allow you to programmatically interact with the cache, providing full control over the cache key and response behavior without changing the user-facing URL.
— Cloudflare, Serving tailored content with Cloudflare
Snippet rule and code: what you must define
Section titled “Snippet rule and code: what you must define”A deployed Snippet needs two linked pieces:
To create and deploy a snippet, you need to define the following elements:
Snippet rule: A filter expression that determines which requests the Snippet will be applied to. Each snippet can only be associated with one snippet rule. Code snippet: JavaScript code that runs on the Cloudflare network when a request matches the associated snippet rule.
— Cloudflare, Cloudflare Snippets
Uploading code alone is not enough for production traffic: after a PUT to create or update snippet code, you must attach a snippet rule whose expression defines when the code runs (Configure Snippets via API). Code created via the API without a rule may appear under Unused Snippets in the dashboard until you associate or delete it (Cloudflare Snippets).
Snippet names are fixed identifiers: only a-z, 0-9, and _, unique per zone, and cannot be changed after creation (Configure Snippets via API, Troubleshoot Snippets).
How Snippets work: rule matching and sequential execution
Section titled “How Snippets work: rule matching and sequential execution”For each request, Cloudflare evaluates snippet rules, builds a list of matching snippet IDs, then runs their code in order.
For each incoming request, Cloudflare evaluates the expression of every snippet rule defined in the zone. The evaluation checks for a match based on various request properties (such as bot score, WAF attack score, country of origin, and cookies).
— Cloudflare, How Snippets work
Once all the rules have been evaluated and the full table has been compiled, Cloudflare starts processing all the snippet IDs in the table.
The snippets are executed sequentially. Each snippet receives the modified request from the previous snippet and applies new modifications to it.
— Cloudflare, How Snippets work
After the last Snippet runs, the modified request continues through the rest of the Rules execution pipeline (How Snippets work).
At GA, Cloudflare emphasized Rules integration and debugging:
Part of the Cloudflare Rules platform – Snippets inherit request modifications from other Cloudflare products and support sequential execution, allowing you to run multiple Snippets on the same request and apply custom modifications step by step.
Trace integration – Use Cloudflare Trace to see which Snippets were triggered on a request — helping you understand traffic flow and debug more effectively.
— Cloudflare, Cloudflare Snippets are now Generally Available
Bot Score, WAF Attack Score, and Custom Lists in snippet rules
Section titled “Bot Score, WAF Attack Score, and Custom Lists in snippet rules”Matching can use traffic intelligence and shared lists:
In Snippets, you can now also match on Bot Score and WAF Attack Score, unlocking smarter edge logic for everything from request filtering and mitigation to tarpitting and logging.
— Cloudflare, More ways to match — Snippets now support Custom Lists, Bot Score, and WAF Attack Score
Custom lists matching – Snippets and Cloud Connector now support user-created IP, AS, and Hostname lists via dashboard or Lists API. Great for shared logic across zones.
— Cloudflare, More ways to match — Snippets now support Custom Lists, Bot Score, and WAF Attack Score
Subrequests from fetch() inside a Snippet
Section titled “Subrequests from fetch() inside a Snippet”When a Snippet calls fetch() to a proxied hostname in the same zone, that outbound call is a subrequest and counts toward plan subrequest limits. Redirect chains count multiply (Cloudflare Snippets).
Each Snippet should call fetch(request) at most once when continuing to origin; multiple calls trigger error 1201 (Troubleshoot Snippets).
When to use Snippets vs Workers vs Page Rules
Section titled “When to use Snippets vs Workers vs Page Rules”Cloudflare positions Snippets as lightweight Rules extensions included on paid plans:
Cloudflare Snippets provide a fast, declarative way to modify HTTP requests and responses at the edge — without requiring a full-stack compute platform.
— Cloudflare, When to use Snippets vs Workers
Snippets are ideal for fast, cost-free request and response modifications at the edge. They extend Cloudflare Rules without requiring additional infrastructure or external solutions.
— Cloudflare, When to use Snippets vs Workers
For legacy Page Rules, the docs steer users toward Snippets and other Rules products:
For more flexibility and customization, consider using Snippets.
— Cloudflare, Additional reference for Page Rules
| Capability | Snippets | Workers |
|---|---|---|
| Match on request attributes (headers, geo, cookies, Bot Score, etc.) | Yes | No |
| Modify HTTP requests/responses at the edge | Yes | Yes |
| Edge caching control | Yes | Yes |
| Persistent storage (KV, D1, Durable Objects) | No | Yes |
| Heavy compute (AI, image transforms) | No | Yes |
| CLI deploy (Wrangler), gradual rollouts | No | Yes |
(Table based on When to use Snippets vs Workers.)
Move to Workers when Snippets hit CPU, memory, or functionality limits—errors 1203 and 1204 explicitly suggest Workers (Troubleshoot Snippets). Snippets and Workers can cooperate on different URL paths, using subrequests to avoid conflicts (When to use Snippets vs Workers).
Availability, GA status, and resource limits
Section titled “Availability, GA status, and resource limits”Cloudflare Snippets are now generally available at no extra cost across all paid plans — giving you a fast, flexible way to programmatically control HTTP traffic using lightweight JavaScript.
— Cloudflare, Cloudflare Snippets are now Generally Available
| Plan | Snippets available | Snippet count | Subrequests per request |
|---|---|---|---|
| Free | No | 0 | 0 |
| Pro | Yes | 25 | 2 |
| Business | Yes | 50 | 3 |
| Enterprise | Yes | 300 | 5 |
Counts reflect upgraded Rules limits (Pro 10→25, Business 25→50, Enterprise 50→300) per Increased Cloudflare Rules limits and the availability table in Cloudflare Snippets. Rollout of new limits may be gradual; some accounts may still see older caps until rollout completes in the first half of 2025 (Increased Cloudflare Rules limits).
Runtime limits (all plans that support Snippets):
| Limit | Value |
|---|---|
| Maximum execution time | 5 ms |
| Maximum memory | 2 MB |
| Maximum total package size | 32 KB |
(Cloudflare Snippets — Limits)
Create and deploy Snippets: dashboard, API, and Terraform
Section titled “Create and deploy Snippets: dashboard, API, and Terraform”Dashboard: create a snippet with rule and preview
Section titled “Dashboard: create a snippet with rule and preview”In the dashboard, open Snippets, name the snippet (name is permanent), edit JavaScript, configure Snippet rule match (all requests or a filter expression), then Deploy or Save as Draft. The editor supports HTTP preview and, in the newer UI, single-page editing of code and rule with autocomplete (Create a snippet in the dashboard, New Snippets Code Editor).
API: tokens, endpoints, and module shape
Section titled “API: tokens, endpoints, and module shape”API management requires Zone > Snippets > Edit on the token (Configure Snippets via API). Base path: https://api.cloudflare.com/client/v4 plus zone-scoped routes such as GET /zones/{zone_id}/snippets and PUT /zones/{zone_id}/snippets/snippet_rules (Configure Snippets via API).
Uploaded files must be ES modules with a default export object exposing a fetch handler; non-ESM uploads are rejected (Configure Snippets via API).
Terraform: cloudflare_snippet and cloudflare_snippet_rules
Section titled “Terraform: cloudflare_snippet and cloudflare_snippet_rules”Terraform can deploy snippet files and rules (Configure Snippets using Terraform, Terraform Support for Snippets). Example shape from the docs:
resource "cloudflare_snippet" "my_snippet" { zone_id = "<ZONE_ID>" name = "my_test_snippet_1" main_module = "file1.js" files { name = "file1.js" content = file("file1.js") }}
resource "cloudflare_snippet_rules" "cookie_snippet_rule" { zone_id = "<ZONE_ID>" rules { enabled = true expression = "http.cookie eq \"a=b\"" description = "Trigger snippet on specific cookie" snippet_name = "my_test_snippet_1" } depends_on = [cloudflare_snippet.my_snippet]}(Configure Snippets using Terraform)
Common Snippet errors and fixes
Section titled “Common Snippet errors and fixes”| Error / issue | Meaning | What to do |
|---|---|---|
| 1201 | Multiple fetch(request) to origin |
Call fetch(request) only once per Snippet (Troubleshoot Snippets) |
| 1203 | CPU time exceeded (5 ms) | Simplify logic or use Workers (Troubleshoot Snippets) |
| 1204 | Memory exceeded (2 MB) | Reduce memory use or use Workers (Troubleshoot Snippets) |
| 1205 | Deployment propagating | Retry after a few seconds (Troubleshoot Snippets) |
| Cannot rename | Name is Snippet ID | Create a new Snippet and delete the old one (Troubleshoot Snippets) |
Watch subrequest budgets: each fetch() in-chain, including redirects, consumes quota (Cloudflare Snippets, Troubleshoot Snippets).
How to check this on your site
Section titled “How to check this on your site”-
In the Cloudflare dashboard, open Create a snippet in the dashboard (Rules > Snippets), confirm the Snippet is deployed (not only saved as draft) and the Snippet rule expression matches the URLs you are testing.
-
Send a test HTTP request that should match the rule (including Bot Score or list conditions if used). Use the snippet editor’s preview where possible (Create a snippet in the dashboard).
-
Use Cloudflare Trace on a live request and confirm which Snippets fired, as described in Cloudflare Snippets are now Generally Available.
-
Optionally list snippet code and rules via the API (
GET /zones/{zone_id}/snippetsandGET /zones/{zone_id}/snippets/snippet_rules) per Configure Snippets via API. -
You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about.