# Functions quickstart

This guide instructs you through running your first function on Azion. The steps take about 10 minutes.

- Create a function that returns `Hello World!`.
- Instantiate the function on an application.
- Add a Rules Engine rule that runs the instance on the `/hello-world` URI.
- Request that URI and read the response.

Three objects make a function run, and each one links to the next:

1. The **function** holds the JavaScript code.
2. The **function instance** binds the function to an application. It also carries the Args, a JSON object passed into the execution context.
3. The **Rules Engine** rule selects the instance with the **Run Function** behavior. The rule sets the execution phase and the criteria that trigger it.

Creating a function does not execute it. An instance without a Rules Engine rule never runs.

The [Azion Terraform provider](/en/documentation/devtools/terraform/) manages the same three objects. For every interface that carries them, refer to [Functions](/en/documentation/platform/functions/#scope-and-limits).

---

Select the interface you will use. The prerequisites and every stage below follow that choice.

## Prerequisites

- An Azion account. To create one, refer to [How to create an account on Azion](/en/documentation/fundamentals/creating-account/).
- The **Edit Functions** permission on the account. It grants permission to create, edit, and remove functions, and it also requires the permission **View Functions**.
- The **Edit Applications** permission on the account. The instance and the rule change an application, and this permission grants permission to edit, create, and remove applications. It also requires the permission **View Applications**. Refer to [Teams Permissions](/en/documentation/fundamentals/teams-permissions/).
- An application. To create one, refer to [Applications quickstart](/en/documentation/platform/applications/quickstart/).

**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](/en/documentation/devtools/cli/) installed and authorized.

**API**

- A [personal token](/en/documentation/guides/platform/account-and-billing/personal-tokens/) and `curl`.

---

## Create the function

The function holds the code that answers the request. This one returns the string `Hello World!`.

**Console**

To create the function in Azion Console:

1. **Open the Functions page**

   Access [Azion Console](https://console.azion.com/) > **Products Menu** > **Libraries** > **Functions**.

2. **Select + Function**

3. **Name the function**

   Enter a name for the function. For example: `hello-world`.

4. **Paste the code in the Code tab**

   In the **Code** tab, paste the following code:

   ```javascript
   export default {
     async fetch(request, env, ctx) {
       return new Response('Hello World!');
     },
   };
   ```

5. **Select Save**

The function is saved and available to instantiate on an application.

> **Tip**
>
> To read the response of the function before it serves traffic, use [Preview deployment](/en/documentation/platform/functions/preview-deployment/). It runs the code in Azion Console without a deployment.

**CLI**

The CLI reads the code from a local file. To create the function:

1. **Save the code to a file**

   Write the following code to `index.js`:

   ```javascript
   export default {
     async fetch(request, env, ctx) {
       return new Response('Hello World!');
     },
   };
   ```

2. **Run the create command**

   ```bash
   azion create function --name hello-world --code ./index.js --active true
   ```

3. **Read the output**

   The command prints the id of the new function:

   ```text
   Created function with ID 12346
   ```

Record the id. The instance takes it. For every flag the command accepts, refer to [Azion CLI create](/en/documentation/devtools/cli/resources/).

**API**

The request body carries the code as a JSON string, so newlines are escaped as `\n`.

1. **Send the create request**

   Replace `[TOKEN VALUE]` with your personal token:

   ```bash
   curl --request POST \
     --url https://api.azion.com/v4/workspace/functions \
     --header 'Accept: application/json' \
     --header 'Authorization: Token [TOKEN VALUE]' \
     --header 'Content-Type: application/json' \
     --data '{
     "name": "hello-world",
     "code": "export default {\n  async fetch(request, env, ctx) {\n    return new Response(\"Hello World!\");\n  },\n};"
   }'
   ```

2. **Read the response**

   A `202` carries the function:

   ```json
   {
     "state": "pending",
     "data": {
       "id": 12345,
       "name": "hello-world",
       "last_editor": "user@example.com",
       "last_modified": "2026-01-01T12:00:00.385220Z",
       "product_version": "2.0",
       "active": true,
       "runtime": "azion_js",
       "execution_environment": "application",
       "default_args": {},
       "reference_count": 0,
       "code": "export default {\n  async fetch(request, env, ctx) {\n    return new Response(\"Hello World!\");\n  },\n};"
     }
   }
   ```

Record the `id`. The instance takes it. The `"state": "pending"` value means the change takes a few minutes to propagate.

---

## Instantiate the function on your application

An instance binds the function to one application, and one function can back several instances. Each path turns on the **Functions** module for the application, then creates the instance.

> **Caution**
>
> Computing time and invocations for functions generate usage-related costs. For the rates, refer to [Pricing](/en/documentation/fundamentals/pricing/).

**Console**

To bind the function to an application in Azion Console:

1. **Still in Azion Console, go to Products Menu > Build > Applications**

2. **Select the application that runs the function**

3. **In the Main Settings tab, turn on the Functions module**

4. **Select Save**

5. **Go to the Functions Instances tab**

6. **Select + Function Instance**

7. **Name the instance**

   Enter a name for the instance. For example: `hello-world instance`.

8. **Select the function**

   Select the `hello-world` function.

9. **Select Save**

The new instance appears in the **Functions Instances** tab.

**CLI**

To turn on the module and create the instance with the Azion CLI, replace the application id and the function id with your own:

1. **Turn on the Functions module**

   ```bash
   azion update application --application-id <application_id> --functions true
   ```

   The command confirms the application:

   ```text
   Updated Application with ID <application_id>
   ```

2. **Create the instance**

   ```bash
   azion create function-instance --application-id <application_id> --function-id <function_id> --name "hello-world instance"
   ```

3. **Read the output**

   The command prints the id of the new instance:

   ```text
   Created Function Instance with ID 12348
   ```

Record the instance id. The rule takes it.

**API**

Two requests turn on the module and create the instance.

1. **Turn on the Functions module**

   Replace `<application_id>` with [your application ID](/en/documentation/guides/application-development/getting-started/configure-main-settings/):

   ```bash
   curl --request PATCH \
     --url https://api.azion.com/v4/workspace/applications/<application_id> \
     --header 'Accept: application/json' \
     --header 'Authorization: Token [TOKEN VALUE]' \
     --header 'Content-Type: application/json' \
     --data '{
     "modules": {
       "functions": {
         "enabled": true
       }
     }
   }'
   ```

   The response returns the modules the application now carries:

   ```json
   {
     "cache": { "enabled": true },
     "functions": { "enabled": true },
     "application_accelerator": { "enabled": false },
     "image_processor": { "enabled": false }
   }
   ```

2. **Create the instance**

   Replace `<function_id>` with the `id` the function create returned:

   ```bash
   curl --request POST \
     --url https://api.azion.com/v4/workspace/applications/<application_id>/functions \
     --header 'Accept: application/json' \
     --header 'Authorization: Token [TOKEN VALUE]' \
     --header 'Content-Type: application/json' \
     --data '{
     "name": "hello-world instance",
     "function": <function_id>,
     "args": {},
     "active": true
   }'
   ```

3. **Read the response**

   A `202` carries the instance:

   ```json
   {
     "state": "pending",
     "data": {
       "id": 12347,
       "name": "hello-world instance",
       "args": {},
       "function": 12345,
       "active": true,
       "last_editor": "user@example.com",
       "last_modified": "2026-01-01T12:00:21.272376Z"
     }
   }
   ```

   The `function` field holds the id of the function the instance runs. It differs from `id`, which is unique for each instance.

Record the instance `id`. The rule takes it.

---

## Add the rule that runs the function

An instance runs only when a Rules Engine rule selects it. This rule runs the instance on requests to `/hello-world`.

**Console**

To add the rule in Azion Console:

1. **In the same application, go to the Rules Engine tab**

2. **Select + Rule**

3. **Name the rule**

   Enter a name for the rule. For example: `Run hello-world`.

4. **Select Request Phase**

5. **Select the variable in the Criteria section**

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

6. **Select is equal as the comparison operator**

7. **Enter the argument**

   Enter `/hello-world` as the argument.

8. **In the Behaviors section, select Run Function**

9. **Select the instance you created**

10. **Select Save**

The rule runs the instance on every request whose URI is `/hello-world`.

> **Note**
>
> If **Run Function** is not in the **Behaviors** list, the application also needs the **Application Accelerator** module. Refer to [Run Function is missing from the behaviors list](/en/documentation/platform/functions/troubleshooting/#run-function-is-missing-from-the-behaviors-list) for the fix.

**CLI**

The CLI reads the rule from a JSON file. To add the rule:

1. **Write the rule to a file**

   Save the following as `rule.json`, with the id of your instance in `attributes.value`:

   ```json
   {
     "name": "Run hello-world",
     "description": "Run the function on /hello-world",
     "active": true,
     "criteria": [
       [
         {
           "conditional": "if",
           "variable": "${uri}",
           "operator": "is_equal",
           "argument": "/hello-world"
         }
       ]
     ],
     "behaviors": [
       {
         "type": "run_function",
         "attributes": { "value": 12348 }
       }
     ]
   }
   ```

2. **Create the rule**

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

3. **Read the output**

   The command prints the id of the new rule:

   ```text
   Created Rules Engine with ID 234568
   ```

The rule runs the instance on every request whose URI is `/hello-world`.

**API**

To add the rule with the Azion API:

1. **Write the rule to a file**

   The criteria carry `${uri}`, which a shell expands, so the body is sent from a file. Save the following as `rule.json`, with the id of your instance in `attributes.value`:

   ```json
   {
     "name": "Run hello-world",
     "description": "Run the function on /hello-world",
     "active": true,
     "criteria": [
       [
         {
           "conditional": "if",
           "variable": "${uri}",
           "operator": "is_equal",
           "argument": "/hello-world"
         }
       ]
     ],
     "behaviors": [
       {
         "type": "run_function",
         "attributes": { "value": 12347 }
       }
     ]
   }
   ```

2. **Send the create request**

   ```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 @rule.json
   ```

3. **Read the response**

   A `202` carries the rule, with the `order` it holds among the rules of the application:

   ```json
   {
     "state": "pending",
     "data": {
       "id": 234567,
       "name": "Run hello-world",
       "active": true,
       "behaviors": [
         {
           "type": "run_function",
           "attributes": { "value": 12347 }
         }
       ],
       "order": 0,
       "last_editor": "user@example.com",
       "last_modified": "2026-01-01T12:01:25.270982Z"
     }
   }
   ```

The rule runs the instance on every request whose URI is `/hello-world`.

New rules can take a few minutes to propagate. Wait before you request the URI.

---

## Verify the response

Your application answers on a domain in the format `xxxxxxxxx.map.azionedge.net`. To set the domain for your application, refer to [Add a custom domain to a workload](/en/documentation/guides/platform/migration/configure-a-domain/).

To confirm that the function runs, request the URI set in the rule:

```bash
curl https://<your-azion-domain>/hello-world
```

The response body is the string the function returns:

```
Hello World!
```

The function now runs on every request that matches the rule criteria.

---

## Next steps

- [How it works](/en/documentation/platform/functions/how-it-works.md): The invocation chain in detail, from the function to the request that triggers it.
- [Function instances](/en/documentation/platform/applications/functions-instances.md): Pass Args to an instance in JSON.
- [Environment variables](/en/documentation/platform/functions/environment-variables.md): Store configuration and secrets outside the function code, then read each one by key at run time.
- [JavaScript examples](/en/documentation/platform/functions/javascript-examples.md): Function code to adapt for your own logic.
- [Best practices](/en/documentation/platform/functions/best-practices.md): What each part of the handler does, and the design choices that keep an invocation inside its limits.
- [Local development](/en/documentation/devtools/cli/dev-command.md): Build and run the project on your own machine with the azion dev command.
