---
description: Create Snippets using the Cloudflare API.
title: Configure Snippets via API
image: https://developers.cloudflare.com/og-docs.png
---

[Skip to content](#main-content)

> Documentation Index  
> Fetch the complete documentation index at: https://developers.cloudflare.com/rules/llms.txt  
> Use this file to discover all available pages before exploring further.

# Configure Snippets via API

Last updated Aug 25, 2026|Copy as Markdown|[View as Markdown](https://66f4ea29.previews.developers.cloudflare.com/rules/snippets/create-api/index.md)|[Agent setup](https://66f4ea29.previews.developers.cloudflare.com/agent-setup/)

You can create Snippets using the [Cloudflare API](https://66f4ea29.previews.developers.cloudflare.com/fundamentals/api/).

## Required permissions

The [API token](https://66f4ea29.previews.developers.cloudflare.com/fundamentals/api/get-started/create-token/) used in API requests to manage Snippets must have at least the following permission:

* _Zone_ \> _Snippets_ \> _Edit_

Note

A token with this permission is only valid for the Snippets endpoints described in this page. You cannot use it to interact with the `http_request_snippets` phase via [Rulesets API](https://66f4ea29.previews.developers.cloudflare.com/ruleset-engine/rulesets-api/).

## Endpoints

To obtain the complete endpoint, append the Snippets endpoints listed below to the Cloudflare API base URL:

```txt
https://api.cloudflare.com/client/v4
```

The `{zone_id}` argument is the [zone ID](https://66f4ea29.previews.developers.cloudflare.com/fundamentals/account/find-account-and-zone-ids/) (a hexadecimal string). You can find this value in the Cloudflare dashboard.

The following table summarizes the available operations.

| Operation                          | Verb + Endpoint                                        |
| ---------------------------------- | ------------------------------------------------------ |
| List all code snippets             | GET /zones/{zone\_id}/snippets                         |
| Create/update code snippet         | PUT /zones/{zone\_id}/snippets/{snippet\_name}         |
| Get code snippet details           | GET /zones/{zone\_id}/snippets/{snippet\_name}         |
| Get code snippet content           | GET /zones/{zone\_id}/snippets/{snippet\_name}/content |
| Delete code snippet                | DELETE /zones/{zone\_id}/snippets/{snippet\_name}      |
| List snippet rules                 | GET /zones/{zone\_id}/snippets/snippet\_rules          |
| Create/update/delete snippet rules | PUT /zones/{zone\_id}/snippets/snippet\_rules          |
| Delete all snippet rules           | DELETE /zones/{zone\_id}/snippets/snippet\_rules       |

## Example API calls

### Create/update code snippet

To create or update a Snippet, use the following `PUT` request. The snippet is named `$SNIPPET_NAME` and the body contains the JavaScript code.

Required API token permissions

At least one of the following [token permissions](https://66f4ea29.previews.developers.cloudflare.com/fundamentals/api/reference/permissions/) is required:
* `Snippets Write`

```bash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/snippets/$SNIPPET_NAME" \
	--request PUT \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--form "files=@example.js" \
	--form "metadata={\"main_module\": \"example.js\"}"
```

The name of a snippet can only contain the characters `a-z`, `0-9`, and `_` (underscore). The name must be unique in the context of the zone. You cannot change the snippet name after creating the snippet.

The required body parameters are:

* `files`: The file with your JavaScript code.
* `metadata`: Object containing `main_module`, which must match the filename of the uploaded file.

To make this example work, save your JavaScript code in a file named `example.js`, and then execute `curl` command with a `PUT` request from the folder where `example.js` is located.

```json
{
	"errors": [],
	"messages": [],
	"success": true,
	"result": {
		"created_on": "2023-07-24-00:00:00",
		"modified_on": "2023-07-24-00:00:00",
		"snippet_name": "snippet_name_01"
	}
}
```

To deploy a new snippet you must [create a snippet rule](#createupdatedelete-snippet-rules). The expression of the snippet rule defines when the snippet code will run.

### Create/update/delete snippet rules

Caution

When using this endpoint to create a new rule and keep existing rules, you must include all rules in the request body. Omitting an existing rule will delete the corresponding rule.

Once you have created a code snippet, you can link it to rules. This is done via the following `PUT` request to the `snippet_rules` endpoint.

Required API token permissions

At least one of the following [token permissions](https://66f4ea29.previews.developers.cloudflare.com/fundamentals/api/reference/permissions/) is required:
* `Snippets Write`

```bash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/snippets/snippet_rules" \
	--request PUT \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"rules": [
				{
						"description": "Trigger snippet on specific cookie",
						"enabled": true,
						"expression": "http.cookie eq \"a=b\"",
						"snippet_name": "snippet_name_01"
				}
		]
	}'
```

Was this helpful?

YesNo

## On this page

[![](https://66f4ea29.previews.developers.cloudflare.com/_astro/logo.te5VL_aD.svg)Docs](https://66f4ea29.previews.developers.cloudflare.com/)

```json
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/rules/snippets/create-api/#page","headline":"Configure Snippets via API · Cloudflare Rules docs","description":"Create Snippets using the Cloudflare API.","url":"https://developers.cloudflare.com/rules/snippets/create-api/","inLanguage":"en","image":"https://developers.cloudflare.com/og-docs.png","dateModified":"2026-08-25","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
```
