---
title: Create a cache rule via API · Cloudflare Cache (CDN) docs
description: Use the Rulesets API to create a cache rule via API. To configure Cloudflare’s API refer to the API documentation.
lastUpdated: 2024-12-16T22:33:26.000Z
source_url:
  html: https://dependabot-npm-and-yarn-astro-5-7-9.preview.developers.cloudflare.com/cache/how-to/cache-rules/create-api/index.md
  md: https://dependabot-npm-and-yarn-astro-5-7-9.preview.developers.cloudflare.com/cache/how-to/cache-rules/create-api/
---

Use the [Rulesets API](https://developers.cloudflare.com/ruleset-engine/rulesets-api/) to create a cache rule via API. To configure Cloudflare’s API refer to the [API documentation](https://developers.cloudflare.com/fundamentals/api/get-started/).

## Basic rule settings

When creating a cache rule via API, make sure you:

* Set the rule action to `set_cache_settings`.
* Define the parameters in the `action_parameters` field according to the [settings](https://developers.cloudflare.com/cache/how-to/cache-rules/settings/) you wish to override for matching requests.
* Deploy the rule to the `http_request_cache_settings` phase entry point ruleset.

## Procedure

1. Use the [List zone rulesets](https://developers.cloudflare.com/api/resources/rulesets/methods/list/) method to obtain the list of rules already present in the `http_request_cache_settings` phase entry point ruleset.

2. If the phase ruleset does not exist, create it using the [Create a zone ruleset](https://developers.cloudflare.com/api/resources/rulesets/methods/create/) operation. In the new ruleset properties, set the following values:

   * kind: `zone`
   * phase: `http_request_cache_settings`

3. Use the [Update a zone ruleset](https://developers.cloudflare.com/api/resources/rulesets/methods/update/) operation to add a cache rule to the list of ruleset rules. Alternatively, include the rule in the [Create a zone ruleset](https://developers.cloudflare.com/api/resources/rulesets/methods/create/) request mentioned in the previous step.

4. (Optional) To update an existing cache rule, use the [Update a zone ruleset rule](https://developers.cloudflare.com/api/resources/rulesets/methods/update/) operation. For an example, refer to the section below.

## Example requests

These examples are setting all the Cache Rules of a zone to a single rule, since using these examples directly will cause any existing rules to be deleted.

## Required API token permissions

The API token used in API requests to manage Cache Rules must have the following permissions:

* *Zone* > *Cache Rules* > *Edit*
* *Account Rulesets* > *Edit*
* *Account Filter Lists* > *Edit*
