Create a snippet in the dashboard
-
Log in to the Cloudflare dashboard ↗, and select your account and website.
-
Go to Rules > Snippets, and select Create a Snippet.
-
In Snippet name, enter a descriptive name for the snippet. You cannot change the name after creating the snippet.
-
Enter the snippet's JavaScript code in the code editor. You can test how your snippet will handle incoming requests using the HTTP and Preview tabs.
-
Select Snippet rule to configure when the snippet will run.
-
Under Run this Snippet if incoming requests match, select if you wish to run the snippet only for requests that match a custom filter expression or for all incoming requests.
-
(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 Done.
-
To deploy your snippet, select Deploy. If you are not ready to deploy your snippet, open the dropdown next to Deploy and select Save as Draft.