---
name: azion-reorder-application-and-firewall-rules
description: >-
  Change the position of the rules of an Application or a Firewall with one PUT request per rule list to the Azion API v4.
---

# Reorder Application and Firewall rules

You can reorder the rules of an [Application](/en/documentation/platform/applications/) or a [Firewall](/en/documentation/platform/firewall/) with one `PUT` request to the [Azion API](/en/documentation/devtools/api/) v4. The request sends an `order` array with the ID of every rule in the list, in the new order. A list holds the rules of one Application phase, or every rule of a Firewall.

To reorder Firewall rules from Azion Console or the Azion CLI, refer to [Change the order of Firewall rules](/en/documentation/guides/application-security/bots-and-network/change-the-order-of-firewall-rules/). To reorder Application rules in Azion Console, move them in the list of the **Rules Engine** tab. For the Azion CLI command, refer to the [CLI section of Rules Engine for Applications](/en/documentation/platform/applications/rules-engine/#cli).

---

## Prerequisites

- A personal token. To create one, refer to [Manage personal tokens](/en/documentation/guides/platform/account-and-billing/personal-tokens/).
- `curl`, to call the API.
- An Application with two or more rules in the phase you want to reorder, or a Firewall with two or more rules.

---

## Reorder the request rules of an Application

The `order` field of a rule is its position in its phase, from `0`. The reorder request sets every position of the phase at once.

To reorder the request rules:

1. Store your token and the ID of the Application in shell variables. Replace the two values with your own:

   ```bash
   export AZION_API_TOKEN="[TOKEN VALUE]"
   APPLICATION_ID=<your-application-id>
   ```

2. List the request rules in their current order. The `fields` parameter keeps the ID, the name, and the position of each rule:

   ```bash
   curl --silent "https://api.azion.com/v4/workspace/applications/$APPLICATION_ID/request_rules?fields=id,name,order&ordering=order&page_size=100" \
     --header "Authorization: Token $AZION_API_TOKEN"
   ```

   The response lists the rules by position:

   ```json
   {
     "count": 3,
     ...
     "results": [
       {
         "id": 1550922,
         "name": "Redirect old blog",
         "order": 0
       },
       {
         "id": 1550924,
         "name": "Deny admin",
         "order": 1
       },
       {
         "id": 1550927,
         "name": "Bypass cache for API",
         "order": 2
       }
     ]
   }
   ```

   A page holds at most 100 rules. If `next` is not `null`, read the next pages too. To read every page, refer to [Page through a list with filters and ordering](/en/documentation/guides/application-development/automation/page-through-a-list-with-filters-and-ordering/).

3. Send a `PUT` request with every rule ID of the phase in the new order. Replace the IDs with your own. This example moves `Deny admin` to the first position:

   ```bash
   curl --silent --request PUT "https://api.azion.com/v4/workspace/applications/$APPLICATION_ID/request_rules/order" \
     --header "Authorization: Token $AZION_API_TOKEN" \
     --header 'Content-Type: application/json' \
     --data '{"order": [1550924, 1550922, 1550927]}'
   ```

   The API answers `202`. The response holds the first page of the rules in their new order, with a `page_size` of 10. This excerpt shows the ID, the name, and the position of each rule:

   ```json
   {
     "count": 3,
     ...
     "page_size": 10,
     ...
     "results": [
       {
         "id": 1550924,
         "name": "Deny admin",
         ...
         "order": 0,
         ...
       },
       {
         "id": 1550922,
         "name": "Redirect old blog",
         ...
         "order": 1,
         ...
       },
       {
         "id": 1550927,
         "name": "Bypass cache for API",
         ...
         "order": 2,
         ...
       }
     ]
   }
   ```

   With more than 10 rules, the response holds the first 10, and `count` and `total_pages` report the rest. The new order still applies to every rule you sent: list the rules again to read all of them.

`Deny admin` is the first request rule of the Application. The new order reaches traffic only after it propagates, and Azion guarantees no propagation time. To check it, repeat a request that a moved rule matches until every answer is the one you expect. For the order in which an Application runs its rules, and for propagation, refer to [How Applications works](/en/documentation/platform/applications/how-it-works/).

---

## Reorder the response rules of an Application

The response phase has its own list of positions, and its own reorder path, `response_rules/order`.

To reorder the response rules:

1. Store your token and the ID of the Application in shell variables. Replace the two values with your own:

   ```bash
   export AZION_API_TOKEN="[TOKEN VALUE]"
   APPLICATION_ID=<your-application-id>
   ```

2. List the response rules in their current order:

   ```bash
   curl --silent "https://api.azion.com/v4/workspace/applications/$APPLICATION_ID/response_rules?fields=id,name,order&ordering=order&page_size=100" \
     --header "Authorization: Token $AZION_API_TOKEN"
   ```

   The response lists the rules by position:

   ```json
   {
     "count": 2,
     ...
     "results": [
       {
         "id": 1550944,
         "name": "Add frame header",
         "order": 0
       },
       {
         "id": 1550945,
         "name": "Remove server header",
         "order": 1
       }
     ]
   }
   ```

   A page holds at most 100 rules. If `next` is not `null`, read the next pages too.

3. Send a `PUT` request with every response rule ID in the new order. Replace the IDs with your own:

   ```bash
   curl --silent --request PUT "https://api.azion.com/v4/workspace/applications/$APPLICATION_ID/response_rules/order" \
     --header "Authorization: Token $AZION_API_TOKEN" \
     --header 'Content-Type: application/json' \
     --data '{"order": [1550945, 1550944]}'
   ```

   The API answers `202`. The response holds the first page of the response rules in their new order:

   ```json
   {
     "count": 2,
     ...
     "results": [
       {
         "id": 1550945,
         "name": "Remove server header",
         ...
         "order": 0,
         ...
       },
       {
         "id": 1550944,
         "name": "Add frame header",
         ...
         "order": 1,
         ...
       }
     ]
   }
   ```

`Remove server header` is the first response rule of the Application, and the request rules keep their positions. The new order reaches traffic only after it propagates, and Azion guarantees no propagation time. To check it, repeat a request that a moved rule matches until every answer is the one you expect. For more information, refer to [Propagation](/en/documentation/platform/applications/how-it-works/#propagation).

---

## Reorder the rules of a Firewall

A Firewall has one list of [rules](/en/documentation/platform/firewall/rules-engine/), and its reorder path is `request_rules/order` under the Firewall.

To reorder the rules of a Firewall:

1. Store your token and the ID of the Firewall in shell variables. Replace the two values with your own:

   ```bash
   export AZION_API_TOKEN="[TOKEN VALUE]"
   FIREWALL_ID=<your-firewall-id>
   ```

2. List the rules in their current order:

   ```bash
   curl --silent "https://api.azion.com/v4/workspace/firewalls/$FIREWALL_ID/request_rules?fields=id,name,order&ordering=order&page_size=100" \
     --header "Authorization: Token $AZION_API_TOKEN"
   ```

   The response lists the rules by position:

   ```json
   {
     "count": 2,
     ...
     "results": [
       {
         "id": 170225,
         "name": "Deny wp-login",
         "order": 0
       },
       {
         "id": 170226,
         "name": "Drop xmlrpc",
         "order": 1
       }
     ]
   }
   ```

   A page holds at most 100 rules. If `next` is not `null`, read the next pages too.

3. Send a `PUT` request with every rule ID of the Firewall in the new order. Replace the IDs with your own:

   ```bash
   curl --silent --request PUT "https://api.azion.com/v4/workspace/firewalls/$FIREWALL_ID/request_rules/order" \
     --header "Authorization: Token $AZION_API_TOKEN" \
     --header 'Content-Type: application/json' \
     --data '{"order": [170226, 170225]}'
   ```

   The API answers `202`. The response holds the first page of the rules of the Firewall in their new order:

   ```json
   {
     "count": 2,
     ...
     "results": [
       {
         "id": 170226,
         "name": "Drop xmlrpc",
         ...
         "order": 0
       },
       {
         "id": 170225,
         "name": "Deny wp-login",
         ...
         "order": 1
       }
     ]
   }
   ```

`Drop xmlrpc` is the first rule of the Firewall. The new order reaches traffic only after it propagates, and Azion guarantees no propagation time. To check it, repeat a request that a moved rule matches until every answer is the one you expect. For the order in which a Firewall runs its rules, and for propagation, refer to [How Firewall works](/en/documentation/platform/firewall/how-it-works/).

---

## Fix a rejected order

The API rejects an `order` array that does not list every rule of the phase, or of the Firewall, exactly once. On the three reorder paths, it answers `400`, keeps the current positions, and names the problem in `errors[0].code`:

| Code    | Title                  | Cause                                                  | Fix                                               |
| ------- | ---------------------- | ------------------------------------------------------ | ------------------------------------------------- |
| `25002` | Missing Rules In Order | The array leaves out a rule, or it is empty.           | List every page of rules again and send every ID. |
| `25001` | Invalid Rules          | The array holds an ID that is not a rule of this list. | Remove the ID.                                    |
| `25000` | Duplicated Rules       | The array holds an ID twice.                           | Keep one copy of the ID.                          |

With `25001`, `meta.not_found_ids` lists the IDs that are not rules of the list. A request rule ID in a response order is one of them. With `25000`, `meta.duplicated_ids` lists the repeated IDs.

For example, an array that leaves out one of the request rules returns this body:

```json
{
  "errors": [
    {
      "code": "25002",
      "title": "Missing Rules In Order",
      "detail": "When ordering you should provide the order for all rules.",
      "status": "400",
      "source": {
        "pointer": "/data/order"
      }
    }
  ]
}
```

After the fix, the `PUT` request returns the first page of the rules in their new order.

---

## Next steps

- [Rules Engine for Applications](/en/documentation/platform/applications/rules-engine.md): Every criterion, behavior, and API path of an Application rule, by phase.
- [Troubleshoot Azion API](/en/documentation/devtools/api/troubleshooting.md): Causes and fixes for the 400, 401, 404, and 405 responses of the API v4.
