Snippets examples
Refer to the following examples to get started creating your snippet code.
Refer to How it works and Create a snippet in the dashboard for overall guidance.
22 examples
- Set up an A/B test by controlling what response is served based on cookies.
- Dynamically set a cookie expiration and test group.
- Allow or deny a request based on a known pre-shared key in a header. This is not meant to replace the WebCrypto API.
- Redirect requests to certain URLs based on a mapped object to the request’s URL.
- Redirect a response based on the country code in the header of a visitor.
- Send debugging information in an errored response to a logging service.
- Adjust Cross-Origin Resource Sharing (CORS) headers and handle preflight requests.
- Get a specific
Set-Cookie
header and update it with a certain value. - If origin responded with
403 Forbidden
error code, redirect to different page. - Redirect all requests from one domain to another domain.
- If origin responds with
JSON
, parse the response and delete fields to return a modified response. - Remove certain query strings from a request before passing to the origin.
- Remove from response all headers that start with a certain name.
- Respond with information about the incoming request provided by Cloudflare’s global network.
- If response to the original request is not
200 OK
or a redirect, send to another origin. - Send Bots information to your origin. Refer to Bot Managenent variables for a full list of available fields.
- Use the bot score field to send bots to a honeypot.
- Convert timestamp to hexadecimal format and send it as a custom header to the origin.
- Set common security headers such as X-XSS-Protection, X-Frame-Options, and X-Content-Type-Options.
- Verify a signed request using the HMAC and SHA-256 algorithms or return a 403.
- Define a delay to be used when incoming requests match a rule you consider suspicious.
- Extract the JWT token from a header, decode it, and implement validation checks to verify it.