# How to create device groups

import Tabs from '~/components/tabs/Tabs'


Group end-users based on their devices, operating systems, or browsers with information provided by the `User-Agent` HTTP request header. By using [Device Groups](/en/documentation/products/build/applications/device-groups/) to categorize requests, you can make your application more responsive.

With this guide, you'll create and activate a new device group to identify mobile devices that access your application.

---

<Tabs client:visible>
    <Fragment slot="tab.console">Console</Fragment>
    <Fragment slot="tab.api">API</Fragment>

<Fragment slot="panel.console">
1. Access [Azion Console](/en/documentation/products/guides/how-to-access-azion-console/) > **Applications**.
2. Click the application to which you want to create a new device group.
3. Select the **Device Groups** tab.
4. Click the **+ Device Group** button.
5. Name your device group. The name of your device group can't contain spaces and must have only alphanumeric characters. For example: `Mobile`.
6. In the **Match to User-Agent** field, specify a regular expression that will meet the desired criteria. For example: `(Mobile|iPhone|Android)`.
7. Click the **Save** button.

:::caution[warning]
The procedure described below is a temporary solution to check the validity of your device group. Once tests have been successfully made, you should deactivate the rule.
:::

Now that your device group is set, you can use **Rules Engine** to determine what action must be taken if the expression is matched to a `User-Agent` header. In this case, a custom `Device-Is` header will be received in the response:

1. Navigate to the **Rules Engine** tab.
2. Click **+ Rule**.
3. Give your rule a name. For example: `Device Group: Mobile`.
4. Select **Response Phase**.
5. In the **Criteria** section, select the variable `${device_group}`.
6. Select the operator **is equal**.
7. Add the name of the device group as configured in the previous instructions. For example: `Mobile`.
8. In the **Behavior** section, select the behavior **Add Response Header**.
9. As an argument, add `Device-Is: Mobile device`.
10. Click the **Save** button.
11. Wait a few minutes for the changes to propagate through the edge.

If your current browser sends a `User-Agent` header similar to the example, you can test whether the request went through by [accessing your application](/en/documentation/products/guides/configure-a-domain/) from a mobile device or artificially adding the header and its value to the request using the following command:

```bash
curl -I https://xxxxxxxxxx.map.azionedge.net/ -H "User-Agent: iPhone Android"
```

You should receive a list of headers including the custom header added by the rule: `device-is: Mobile device`. Now that you've verified that the devices are being correctly categorized, you can [customize the desired behavior](/en/documentation/products/guides/build/work-with-rules-engine/) using the same rule.
</Fragment>

<Fragment slot="panel.api">
1. Run the following `POST` request in your terminal, replacing `[TOKEN VALUE]` with your [personal token](/en/documentation/products/guides/personal-tokens/) and the `<application_id>` variable with [your application ID](/en/documentation/products/guides/build/configure-main-settings/):

```bash
curl --location 'https://api.azionapi.net/edge_applications/<application_id>/device_groups' \
--header 'Accept: application/json; version=3' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token [TOKEN VALUE]' \
--data '{
    "name": "Mobile",
    "user_agent": "(Mobile|iPhone|Android)"
}'
```

  | Key | Description |
  | --- | --- |
  | `name` | The name of the device group. Must be alphanumeric. |
  | `user_agent` | The regular expression that should be matched to the contents of the `User-Agent` header from the request. |

2. You'll receive a response similar to this:

```json
{
    "results": {
        "id": <device_group_id>,
        "name": "Mobile",
        "user_agent": "(Mobile|iPhone|Android)"
    }
}
```

:::caution[warning]
The procedure described below is a temporary solution to check the validity of your device group. Once tests have been successfully made, you should deactivate or modify the rule.
:::

3. Run the following `POST` request in your terminal, replacing `[TOKEN VALUE]` with your [personal token](/en/documentation/products/guides/personal-tokens/), the `<application_id>` variable with [your application ID](/en/documentation/products/guides/build/configure-main-settings/), and the `input_value` variable with the name of your device group:

```bash
curl --location 'https://api.azionapi.net/edge_applications/<application_id>/rules_engine/response/rules' \
--header 'Accept: application/json; version=3' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token [TOKEN VALUE]' \
--data '{
    "name": "DG - Mobile",
    "behaviors": [
        {
            "name": "add_response_header",
            "target": "Device-Is: Mobile device"
        }
    ],
    "criteria": [
        [
            {
                "variable": "${device_group}",
                "operator": "is_equal",
                "conditional": "if",
                "input_value": "Mobile"
            }
        ]
    ]
}'
```

4. You'll receive a response similar to this:

```json
{
    "results": {
        "id": <rule_id>,
        "name": "DG - Mobile",
        "phase": "response",
        "behaviors": [
            {
                "name": "add_response_header",
                "target": "Device-Is: Mobile device"
            }
        ],
        "criteria": [
            [
                {
                    "variable": "${device_group}",
                    "operator": "is_equal",
                    "conditional": "if",
                    "input_value": "Mobile"
                }
            ]
        ],
        "is_active": true,
        "order": 3,
        "description": ""
    },
}
```

5. Wait a few minutes for the changes to propagate to the edge nodes.
6. Run the following cURL request manipulating the `User-Agent` header to match the device group:

```bash
curl -I https://xxxxxxxxxx.map.azionedge.net/ -H "User-Agent: Android"
```

7. If you receive a response similar to the following, your device group has been successfully categorized:

```bash
HTTP/2 200 
content-type: text/html; charset=utf-8
content-length: 9593
device-is: Mobile device
```

You may now deactivate the rule or [create a different rule](/en/documentation/products/guides/build/work-with-rules-engine/) to fit the application's settings to your needs.

:::tip
Check the [Azion API documentation](https://api.azion.com/) and the [OpenAPI specification](https://github.com/aziontech/azionapi-openapi/) to know more about all features available via API.
:::
</Fragment>

</Tabs>