Skip to content

Create a snippet in the dashboard

The snippet creation wizard will guide you through the following steps:

  1. Define snippet code and test your snippet.
  2. Create snippet rule. The rule expression defines for which requests the snippet code will run.
  3. Review your snippet configuration and deploy the snippet.

Procedure

  1. Log in to the Cloudflare dashboard, and select your account and website.

  2. Go to Rules > Snippets, and select Create a Snippet.

  3. Enter a descriptive name for the snippet.

  4. Enter the snippet's JavaScript code. You can test how your snippet will handle incoming requests using the HTTP and Preview tabs.

  5. Select Continue to add Snippet rule.

  6. Enter a descriptive name for the snippet rule. The snippet rule specifies for which requests the snippet code will run.

  7. 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.

  8. (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.

  9. Select Continue to create Snippet.

  10. 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.