Create in the dashboard
The snippet creation wizard will guide you through the following steps:
- Define snippet code and test your snippet.
- Create snippet rule. The rule expression defines for which requests the snippet code will run.
- Review your snippet configuration and deploy the snippet.
-
Log in to the Cloudflare dashboard, and select your account and website.
-
Go to Rules > Snippets, and select Create a Snippet.
-
Enter a descriptive name for the snippet.
-
Enter the snippet’s JavaScript code. You can test how your snippet will handle incoming requests using the HTTP and Preview tabs.
-
Select Continue to add Snippet rule.
-
Enter a descriptive name for the snippet rule. The snippet rule specifies for which requests the snippet code will run.
-
Under When incoming requests match, select if you wish to run the snippet for all incoming requests or only for requests that match a custom filter expression.
-
(Optional) To define a custom expression, use the Expression Builder (specifying one or more values for Field, Operator, and Value) or manually enter an expression using the Expression Editor. For more information, refer to Edit expressions in the dashboard.
-
Select Continue to create Snippet.
-
Review the snippet code and the snippet rule that defines when the snippet will run. To save your snippet and deploy a rule that enables it, select Save and deploy Snippet. If you are not ready to deploy your snippet, select Save and enable later.