# Azion CLI function

The Azion CLI `function` commands create, list, describe, update, and delete [Functions](/en/documentation/platform/functions/) in the functions library of your account. Each function holds its code, its default arguments, and the environment it runs in, an application or a firewall. An application runs a function through a function instance, which the [function-instance commands](/en/documentation/devtools/cli/resources/function-instance/) manage. The options every command accepts, such as `--format`, `--out`, and `-y`, are on [Global options](/en/documentation/devtools/cli/globals/).

---

## Create

`azion create function` adds a function to the functions library from a code file and the settings you pass:

```bash
azion create function [flags]
```

| Flag                      | Short | Type   | Default | Description                                                                                                                                         |
| ------------------------- | ----- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--active`                | —     | string | —       | **Required** unless `--file` is set. Turns the function on (`true`) or off (`false`). Without it, the command asks you to select `true` or `false`. |
| `--args`                  | —     | string | —       | Path to a JSON file with the arguments of the function. The command stores the content of the file as `default_args`.                               |
| `--code`                  | —     | string | —       | **Required** unless `--file` is set. Path to the file with the code of the function. Without it, the command asks for the path.                     |
| `--execution-environment` | —     | string | —       | Where the function runs: `application` or `firewall`.                                                                                               |
| `--file`                  | —     | string | —       | Path to a JSON file with the attributes of the function. Use `-` to read the JSON from standard input.                                              |
| `--name`                  | —     | string | —       | Name of the function.                                                                                                                               |

`--execution-environment` takes only `application` or `firewall`. Any other value, such as `edge_application`, is refused with this error:

```text
Error: Failed to create function: ["\"edge_application\" is not a valid choice."]. Check your settings and try again. If the error persists, contact Azion support
```

This file, `./code/function.js`, holds the code of the function. It answers every request with the text `my-function`:

```javascript
async function handleRequest(request) {
  return new Response("my-function", { status: 200 });
}
addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});
```

This command creates an active function named `my-function` from that file, with the arguments in `./code/args.json`, to run in an application:

```bash
azion create function --name my-function --code ./code/function.js --args ./code/args.json --active true --execution-environment application
```

The command prints the ID of the function:

```text
Created function with ID 12359
```

---

## List

`azion list function` lists the functions of your account, 50 to a page:

```bash
azion list function [flags]
```

| Flag          | Short | Type   | Default | Description                                                                                                               |
| ------------- | ----- | ------ | ------- | ------------------------------------------------------------------------------------------------------------------------- |
| `--details`   | —     | —      | —       | Adds the `LAST EDITOR`, `REFERENCE COUNT`, and `EXECUTION ENVIRONMENT` columns to the `ID`, `NAME`, and `ACTIVE` columns. |
| `--filter`    | —     | string | —       | Name to filter the list by.                                                                                               |
| `--order-by`  | —     | string | —       | Field to sort the list by.                                                                                                |
| `--page`      | —     | int    | `1`     | Number of the page to return.                                                                                             |
| `--page-size` | —     | int    | `50`    | Number of functions on each page.                                                                                         |

This command returns the first page of a list split into pages of two functions:

```bash
azion list function --page 1 --page-size 2
```

The command prints one row per function:

```text
ID     NAME                                    ACTIVE
12357  my-bot-filter                           true
12358  my-api                                  true
```

---

## Describe

`azion describe function` prints the settings of one function:

```bash
azion describe function [flags]
```

| Flag            | Short | Type | Default | Description                                                                                                                      |
| --------------- | ----- | ---- | ------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `--function-id` | —     | int  | —       | ID of the function to describe.                                                                                                  |
| `--with-code`   | —     | —    | —       | Prints the same fields as the command without it. The code of the function is in the `code` field of the `--format json` output. |

This command describes the function with ID `12359`:

```bash
azion describe function --function-id 12359
```

The command prints the ID, the name, the last editor, the state, and the reference count of the function:

```text
ID:                12359
Name:              my-function
Last Editor:       you@example.com
Active:            true
Reference Count:   0
```

With `--format json`, the command prints the full object: `active`, `azion_form`, `code`, `default_args`, `execution_environment`, `id`, `is_versioned`, `last_editor`, `last_modified`, `name`, `product_version`, `reference_count`, `resource_version`, `runtime`, `vendor`, `version`, `version_id`, and `version_state`. The `code` field holds the source of the function, and `default_args` holds the content of the `--args` file.

With an ID that does not exist, the command fails with this error:

```text
Error: Failed to get the Edge Function: The given ID or API's endpoint doesn't exist or isn't available. Check that the identifying information is correct. Check your settings and try again. If the error persists, contact Azion support
```

---

## Update

`azion update function` changes the name, the state, the code, the arguments, or the environment of a function:

```bash
azion update function [flags]
```

| Flag                      | Short | Type   | Default | Description                                                                                                                                         |
| ------------------------- | ----- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--active`                | —     | string | —       | Turns the function on (`true`) or off (`false`).                                                                                                    |
| `--args`                  | —     | string | —       | Path to a JSON file with the new arguments of the function. Its content replaces `default_args`.                                                    |
| `--code`                  | —     | string | —       | Path to the file with the new code of the function.                                                                                                 |
| `--execution-environment` | —     | string | —       | Where the function runs: `application` or `firewall`.                                                                                               |
| `--file`                  | —     | string | —       | Path to a JSON file with the attributes to change. Use `-` to read the JSON from standard input.                                                    |
| `--function-id`           | —     | int    | —       | **Required**, with `--file` too. ID of the function to update. Without it, the command asks for the ID, even when the `--file` JSON carries `"id"`. |
| `--name`                  | —     | string | —       | New name of the function.                                                                                                                           |

This command renames the function with ID `12359` to `my-function-renamed` and turns it off:

```bash
azion update function --function-id 12359 --name my-function-renamed --active false
```

The command prints the ID of the updated function:

```text
Updated function with ID 12359
```

With `--code` and `--args`, the same command replaces the code and the stored arguments of the function. The `default_args` field then holds the content of the new `--args` file.

---

## Delete

`azion delete function` deletes a function from the functions library:

```bash
azion delete function [flags]
```

| Flag            | Short | Type | Default | Description                   |
| --------------- | ----- | ---- | ------- | ----------------------------- |
| `--function-id` | —     | int  | —       | ID of the function to delete. |

This command deletes the function with ID `12370`:

```bash
azion delete function --function-id 12370 -y
```

The command confirms the deletion:

```text
Function 12370 was successfully deleted
```

---

## Use a JSON file

`azion create function` and `azion update function` read the attributes of the function from a JSON file with `--file`.

This file creates an active function named `my-firewall-function` that runs in a firewall. It carries the code inline, in the `code` field:

```json
{
  "name": "my-firewall-function",
  "code": "addEventListener('firewall', (event) => { event.continue(); });",
  "active": true,
  "runtime": "azion_js",
  "execution_environment": "firewall",
  "default_args": {}
}
```

Pass the file to the create command:

```bash
azion create function --file fn-create.json
```

The command prints the ID of the function:

```text
Created function with ID 12370
```

On update, the command does not read `"id"` from the file, so pass `--function-id` on the command line. This file renames the function with ID `12370`:

```json
{
  "id": 12370,
  "name": "my-firewall-function-updated"
}
```

Pass the file and the ID to the update command:

```bash
azion update function --function-id 12370 --file fn-update.json
```

The command prints the ID of the updated function:

```text
Updated function with ID 12370
```

The function keeps the fields the file leaves out. In this example, it stays active.

---

## Related resources

- [Global options](/en/documentation/devtools/cli/globals.md): The options every command accepts, such as `--format`, `--out`, and `-y`.
- [Functions](/en/documentation/platform/functions.md): What a function is and how a request reaches its code.
- [Azion CLI function-instance](/en/documentation/devtools/cli/resources/function-instance.md): The commands that add a function to an application.
- [Azion CLI firewall-instance](/en/documentation/devtools/cli/resources/firewall-instance.md): The commands that add a function to a firewall.
