---
name: azion-create-an-application-rule
description: >-
  Add a rule to an application from Azion Console, the Azion CLI, or the API, and apply a cache setting to every path that starts with /static/.
---

# Create an application rule

You can create a rule in [Rules Engine for Applications](/en/documentation/platform/applications/rules-engine/) from Azion Console, the Azion CLI, or the API. A rule runs its behaviors on every request that matches its criteria. The application changes what it does, and your code stays as it is. Rules are also how the application uses its cache settings, function instances, and connectors. The rule on this page applies a [cache setting](/en/documentation/platform/applications/cache/cache-settings/) to the paths that start with `/static/`.

To rewrite a request path or remove the `Server` header from responses, refer to [Create request and response rules](/en/documentation/guides/application-development/getting-started/rules-engine/). A firewall has rules of its own. For those, refer to [Create a firewall rule](/en/documentation/guides/application-security/firewall-and-waf/work-with-rules-engine/).

---

Choose your interface once. The prerequisites and the procedure on this page follow that choice.

## Prerequisites

- An application that serves your traffic through a workload. To create one, refer to [Applications quickstart](/en/documentation/platform/applications/quickstart/).
- A cache setting on that application. To create one, refer to [Create a cache setting](/en/documentation/guides/application-performance/cache-and-purge/tune-cache-settings/).

**Console**

- Access to Azion Console. To sign in, refer to [Access Azion Console](/en/documentation/guides/platform/account-and-billing/how-to-access-azion-console/).

**CLI**

- The Azion CLI, installed and authorized with your account. The commands on this page match Azion CLI 4.23.0. For more information, refer to [Azion CLI](/en/documentation/devtools/cli/).
- The ID of the application. Azion Console shows it in the address of the application's page, after `/applications/edit/`.
- The ID of the cache setting. The **Cache Settings** tab of the application lists it in the **ID** column.

**API**

- A personal token, sent in the `Authorization` header as `Token [TOKEN VALUE]`. To create one, refer to [Manage a personal token](/en/documentation/guides/platform/account-and-billing/personal-tokens/).
- `curl`, or another HTTP client.
- The ID of the application. Azion Console shows it in the address of the application's page, after `/applications/edit/`.
- The ID of the cache setting. The **Cache Settings** tab of the application lists it in the **ID** column.

---

## Apply a cache setting to one path

The rule in this section runs in the **Request Phase**. It matches the requests whose `${uri}` starts with `/static/`. Its **Set Cache Policy** behavior applies your cache setting to them. Requests to other paths keep the cache behavior they had. The criterion also matches longer paths, such as `/static/css/site.css`.

A rule's phase is fixed when you create it. To run a behavior in the **Response Phase** instead, create a new rule in that phase.

The `${uri}` variable needs no Product on the application. **Set Cache Policy** relies on Cache, which every new application runs. Other variables and behaviors need Application Accelerator, Image Processor, or Functions enabled on the application. In Azion Console, each of those options carries a note such as `- Required Application Accelerator`. To enable a Product, refer to [Enable Products on an application](/en/documentation/guides/application-development/getting-started/build-an-application/).

**Console**

To create the rule in Azion Console:

1. **Open the application**

   Access [Azion Console](https://console.azion.com/) > **Applications**, then select your application.

2. **Select the Rules Engine tab**

3. **Select + Rule**

4. **Name the rule**

   In the **General** section, enter a **Name**, such as `apply-static-assets-cache`.

5. **(Optional) Describe the rule**

   In **Description**, enter a note about what the rule does. The note takes up to 1,000 characters.

6. **Select the phase**

   In the **Phase** section, select **Request Phase**.

7. **Select the variable**

   In the **Criteria** section, select `${uri}` as the variable.

8. **Select the operator**

   Select `starts_with` as the operator.

9. **Enter the argument**

   Enter `/static/` as the argument.

10. **Select the behavior**

    In the **Behaviors** section, select **Set Cache Policy**.

11. **Select the cache setting**

    In the second dropdown of the behavior, select your cache setting.

12. **Keep the rule active**

    Under **Status**, make sure that **Active** is turned on.

13. **Select Save**

The rule appears in the **Rules Engine** tab, under the **Request** heading. To run a second behavior in the same rule, select **+ Add Behavior** before you save it.

**CLI**

To create the rule with the Azion CLI, save its definition as `rule.json`. The file holds the rule's `name`, an optional `description`, whether it is `active`, its `criteria`, and its `behaviors`:

```json
{
  "name": "apply-static-assets-cache",
  "description": "Applies the static-assets cache setting to /static/",
  "active": true,
  "criteria": [
    [
      {
        "variable": "${uri}",
        "operator": "starts_with",
        "conditional": "if",
        "argument": "/static/"
      }
    ]
  ],
  "behaviors": [
    {
      "type": "set_cache_policy",
      "attributes": {
        "value": "<cache-setting-id>"
      }
    }
  ]
}
```

`criteria` is a list of groups, and each group is a list of conditions. The first condition takes `"conditional": "if"`. In the `set_cache_policy` behavior, `attributes.value` holds the ID of your cache setting.

Then create the rule in the Request Phase, with your application ID in place of `<application-id>`:

```bash
azion create rules-engine --application-id <application-id> --phase request --file rule.json
```

The command prints the ID of the new rule:

```text
Created Rules Engine with ID <rule-id>
```

The `--phase` flag sets the phase of the rule. The file holds the same body that the API accepts.

**API**

To create the rule with the API, send a `POST` request to the `request_rules` endpoint of your application. The endpoint sets the phase, and the body carries the rule's `name`, `description`, `active`, `criteria`, and `behaviors`:

```bash
curl --request POST \
  --url https://api.azion.com/v4/workspace/applications/<application-id>/request_rules \
  --header 'Accept: application/json' \
  --header 'Authorization: Token [TOKEN VALUE]' \
  --header 'Content-Type: application/json' \
  --data '{
  "name": "apply-static-assets-cache",
  "description": "Applies the static-assets cache setting to /static/",
  "active": true,
  "criteria": [
    [
      {
        "variable": "${uri}",
        "operator": "starts_with",
        "conditional": "if",
        "argument": "/static/"
      }
    ]
  ],
  "behaviors": [
    {
      "type": "set_cache_policy",
      "attributes": {
        "value": "<cache-setting-id>"
      }
    }
  ]
}'
```

`criteria` is a list of groups, and each group is a list of conditions. The first condition takes `"conditional": "if"`. In the `set_cache_policy` behavior, `attributes.value` holds the ID of your cache setting.

The API answers `202` and returns the rule:

```text
{"state":"pending","data":{"id":<rule-id>,…,"behaviors":[{"type":"set_cache_policy","attributes":{"value":<cache-setting-id>}}],"order":0,…}}
```

The response adds the rule's `id` and its `order`, and returns the cache setting ID as a number. The `order` is the rule's position among the request rules of the application, counted from `0`. The first request rule of an application takes `0`, and the next one takes `1`.

While Application Accelerator is off on the application, the API refuses a criterion on `${request_uri}` with `400`:

```json
{"errors":[{"code":"25047","title":"Missing Required Modules","detail":" It requires any of the following modules to be enabled: ['application_accelerator'].","status":"400","source":{"pointer":"/data/criteria/0/0/variable"},"meta":{"message_prefix":"","owner_modules":"any","missing_required_modules":["application_accelerator"]}}]}
```

Use `${uri}`, which needs no Product, or enable Application Accelerator before you send the rule. For every variable, operator, and behavior the body accepts, refer to [Rules Engine for Applications](/en/documentation/platform/applications/rules-engine/) and the [Azion API reference](https://api.azion.com/).

A new rule can take a few minutes to propagate. To read the cache status of a response under `/static/`, refer to [Check the cache status of a response](/en/documentation/guides/application-performance/cache-and-purge/check-page-cache-time/).

---

## Next steps

- [Rules Engine for Applications](/en/documentation/platform/applications/rules-engine.md): Every variable, operator, and behavior an application rule accepts, by phase.
- [Create request and response rules](/en/documentation/guides/application-development/getting-started/rules-engine.md): Rewrite a request path and remove the Server header from responses, in Azion Console.
- [Check the cache status of a response](/en/documentation/guides/application-performance/cache-and-purge/check-page-cache-time.md): Read whether a response under the rule's path came from the cache.
- [Enable Products on an application](/en/documentation/guides/application-development/getting-started/build-an-application.md): Turn on Application Accelerator, Image Processor, or Functions for the variables and behaviors they add.
- [Create device groups](/en/documentation/guides/application-development/getting-started/create-device-groups.md): Group devices by User-Agent to manage application behaviors and cache policies for each group.
