---
name: azion-protect-an-api-with-firewall-and-waf
description: >-
  Put a Firewall with a WAF rule set in front of an API path, and watch it block a SQL injection while normal requests pass.
---

# Protect an API with Firewall and WAF

In this tutorial, you will protect an API with a [Firewall](/en/documentation/platform/firewall/) that applies a [WAF](/en/documentation/platform/firewall/waf/) rule set to every path that starts with `/api`. You will publish a small API, create the WAF rule set and the Firewall, deploy both on one Workload, and send a SQL injection that WAF blocks.

Every resource the tutorial creates links to one created before it:

1. The [Function](/en/documentation/platform/functions/) is the API: it answers every request with the path and the query string it received.
2. The [Application](/en/documentation/platform/applications/) runs the Function on every path.
3. The WAF rule set sets which threat families WAF scores, and how sensitive it is to each one.
4. The Firewall has WAF turned on, and a rule that applies the rule set in *Blocking* mode to the paths that start with `/api`.
5. The [Workload](/en/documentation/platform/workloads/) holds the domain, and its deployment sets the Firewall that inspects each request and the Application that answers it.

The tutorial uses Azion Console. To create a Firewall and its rules with the Azion CLI or the API, refer to [Firewall quickstart](/en/documentation/platform/firewall/quickstart/) and [Create and apply a WAF rule set](/en/documentation/guides/application-security/firewall-and-waf/create-waf-rule-set/).

---

## Prerequisites

- An Azion account. To create one, refer to [Create an account](/en/documentation/fundamentals/creating-account/).
- Access to Azion Console. To sign in, refer to [Access Azion Console](/en/documentation/guides/platform/account-and-billing/how-to-access-azion-console/).
- `curl`, to send requests to your API.

---

## 1. Write the API

Create a directory for the tutorial, go to it, and create an `index.js` file with this code:

```javascript title="index.js"
export default {
  async fetch(request) {
    const { pathname, searchParams } = new URL(request.url)
    return Response.json({ path: pathname, query: Object.fromEntries(searchParams) })
  }
}
```

The Function echoes the path and the query string of each request as JSON, so every response shows what reached it.

---

## 2. Create the Function

To create the Function in Azion Console:

1. Access [Azion Console](https://console.azion.com/) > **Functions**.
2. Select **+ Function**.
3. In **Name**, enter `protected-api`.
4. Keep **Application** selected in **Execution Environment**.
5. Select the **Code** tab.
6. Replace the sample code in the editor with the content of `index.js`.
7. Select **Save**.

The Console shows the message **Your function has been created**, and the account holds the Function.

---

## 3. Create the Application

The Application takes three parts: Functions turned on, a Function Instance of the Function, and a rule that runs it on every path.

To create the Application:

1. Access [Azion Console](https://console.azion.com/) > **Applications**.
2. Select **+ Application**.
3. In **Name**, enter `protected-api`.
4. In **Modules**, turn on **Functions**.
5. Select **Create**.

To attach the Function:

1. Select the **Functions Instances** tab.
2. Select **+ Function**.
3. In **Name**, enter `protected-api`.
4. In **Function**, select `protected-api`.
5. Select **Save**.

To run the Function on every request:

1. Select the **Rules Engine** tab.
2. Select **+ Rule**.
3. In **Name**, enter `Run protected-api`.
4. Keep **Request Phase** selected.
5. In **Criteria**, keep `${uri}` and **starts with**, and enter `/` as the argument.
6. In **Behaviors**, replace **Deliver** with **Run Function**.
7. In the field that **Run Function** adds, select the `protected-api` Function Instance.
8. Select **Save**.

The Application runs the Function on every request it receives.

---

## 4. Create the WAF rule set

The rule set scores eight threat families, each at medium sensitivity, which is what Azion Console proposes for a new rule set.

To create the rule set:

1. Access [Azion Console](https://console.azion.com/) > **WAF Rules**.
2. Select **+ WAF Rule**.
3. In **Name**, enter `protected-api`.
4. In **Threat Type Configuration**, keep the eight threat families turned on, each with **Sensitivity Medium**.
5. Select **Save**.

The Console shows the message **Your WAF rule has been created**. The account holds the rule set, and it scores nothing until a Firewall rule applies it.

---

## 5. Create the Firewall

The Firewall needs WAF turned on and a rule that applies the rule set to the paths that start with `/api`, in *Blocking* mode. Requests to other paths reach the Application without WAF inspection.

To create the Firewall:

1. Access [Azion Console](https://console.azion.com/) > **Firewalls**.
2. Select **+ Firewall**.
3. In **Name**, enter `protected-api`.
4. In **Modules**, turn on **Web Application Firewall**.
5. Select **Create**.

The Console opens the new Firewall on its **Main Settings** tab.

To apply the rule set to `/api`:

1. Select the **Rules Engine** tab.
2. Select **+ Rule**.
3. In **Name**, enter `Apply the WAF to /api`.
4. In **Criteria**, keep **Request Uri** and **starts with**, and enter `/api` as the argument.
5. In **Behaviors**, select **Set WAF**.
6. In the WAF field that **Set WAF** adds, select `protected-api`.
7. In **Select a WAF mode**, select **Blocking**.
8. Select **Save**.

The rule appears in the **Rules Engine** tab of the Firewall, which applies the rule set to `/api` in *Blocking* mode.

---

## 6. Create the Workload

The deployment of the Workload sets both the Firewall and the Application, so every request passes the Firewall before it reaches the Application.

To create the Workload and its deployment:

1. Access [Azion Console](https://console.azion.com/) > **Workloads**.
2. Select **+ Workload**.
3. In **Name**, enter `protected-api`.
4. In **Deployment Settings**, select `protected-api` in **Application**.
5. In **Firewall**, select `protected-api`.
6. Select **Save**.

The list of Workloads shows the domain in the **Workload Domain** column, in the format `xxxxxxxxxxx.map.azionedge.net`.

---

## 7. Test the protection

A new Workload answers several minutes after its deployment, once the change propagates across Azion's distributed infrastructure. In tests, it took 6 to 9 minutes, and no duration is guaranteed. Until then, the domain answers `404` with an HTML page, and the answers can alternate between `404` and the responses below.

Set `DOMAIN` to the domain of your Workload:

```bash
DOMAIN=<your-workload-domain>
```

Send a normal request to the API:

```bash
curl --silent --get "https://$DOMAIN/api/orders" --data-urlencode "id=42"
```

Repeat it every minute until the API answers three times in a row:

```json
{"path":"/api/orders","query":{"id":"42"}}
```

Send a SQL injection in the same parameter, and print the status code:

```bash
curl --silent --output /dev/null --write-out '%{http_code}\n' --get "https://$DOMAIN/api/orders" --data-urlencode "id=42' OR '1'='1"
```

```text
400
```

WAF blocked the request before it reached the Function. The body is Azion's default error page, headed **Bad Request**.

Send the same injection to `/`, which the Firewall rule does not cover:

```bash
curl --silent --get "https://$DOMAIN/" --data-urlencode "id=42' OR '1'='1"
```

```json
{"path":"/","query":{"id":"42' OR '1'='1"}}
```

The request reached the Function, because only the paths that start with `/api` pass through the rule set.

---

## 8. Delete the resources

The protected API keeps running on your domain after the tutorial, so this stage is optional. To remove the resources, delete the Workload first, because its deployment uses the Firewall and the Application. Then delete the Firewall before the rule set it applies.

To delete the Workload:

1. Access [Azion Console](https://console.azion.com/) > **Workloads**.
2. In the `protected-api` row, open the actions menu and select **Delete**.
3. In the **Delete Workload** dialog, enter `protected-api`.
4. Select **Delete**.

Repeat the steps in this order: **Firewalls** for the Firewall, **WAF Rules** for the rule set, **Applications** for the Application, and **Functions** for the Function. Each dialog asks for the name of the resource.

The account no longer holds the resources of the tutorial.

---

## Next steps

- [Scoring and modes](/en/documentation/platform/firewall/waf/scoring-and-modes.md): How sensitivity sets what WAF blocks, and when to start in Logging mode.
- [Create a WAF exception](/en/documentation/guides/application-security/firewall-and-waf/configure-waf-allowed-rules.md): Allow a legitimate request that the rule set blocks.
- [Apply WAF and a rate limit to one path](/en/documentation/guides/application-security/firewall-and-waf/apply-waf-and-rate-limit-to-one-path.md): Add a rate limit to the same Firewall rule.
- [Rules Engine for Firewall](/en/documentation/platform/firewall/rules-engine.md): Every criterion and behavior a Firewall rule takes.
