# How to instantiate functions in your application

import Tabs from '~/components/tabs/Tabs'
import Code from '~/components/Code/Code.astro'


Instantiate [serverless functions](/en/documentation/products/build/applications/functions-instances/) directly within your application.

This guide uses the pre-configured *Azion - Hello World* function, which is linked to your account. You can repeat this process using any function already available on [Azion Marketplace](/en/documentation/products/marketplace/) or [develop your own function](/en/documentation/products/guides/edge-functions/api-builder/).

:::caution[warning]
You must activate the [Functions](/en/documentation/products/build/applications/functions/) module to implement functions in your application. If **Functions** is activated, computing time and invocations could generate usage-related costs. Check the [pricing page](/en/documentation/products/pricing/) for more information.
:::

---

<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 you want to configure. 
3. Activate the **Functions** module to enable functions.
4. Click the **Save** button.
5. Go to the **Functions Instances** tab.
6. Click **+ Function Instance**.
7. Name your function instance. For example: `Hello World function`.
8. Select the **Azion - Hello World** function.
9. Click the **Save** button.

The functions page now lists the newly created instance. However, this new function isn't yet active in your application. You need to define what will trigger the function in your application. To invoke the instantiated function in the URI `xxxxxxxxxx.map.azionedge.net/hello-world`:

10. Still on the **Applications** page, navigate to the **Rules Engine** tab.
11. Click the **+ Rule** button.
12. Give a name for your rule.
13. Select **Request Phase**.
14. Under the **Criteria** section, select the variable `${uri}`.

:::note
The `${uri}` variable may already be selected by default if you didn't activate the [Application Accelerator](/en/documentation/products/build/applications/application-accelerator/) module.
:::

15. As a comparison operator, select **is equal**.
16. As an argument, add `/hello-world`.
17. In the **Behaviors** section, select **Run Function** from the behavior list.
18. Select the **Hello World** function.
19. Click the **Save** button.

To see your function running, access your application [using its domain](/en/documentation/products/guides/configure-a-domain/) and the URI set in the rule, which is in the format `xxxxxxxxx.map.azionedge.net/hello-world`.
</Fragment>

<Fragment slot="panel.api">
1. Run the following `PATCH` 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/) to activate the [Functions](/en/documentation/products/build/applications/functions/) module:


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


2. You'll receive a response with the updated value.
3. Run the following `GET` request to retrieve the `edge_function_id` of the *Hello World* function:

<Code lang="bash" code={` 
curl --request GET \
  --url https://api.azion.com/v4/edge_functions/functions \
  --header 'Accept: application/json' \
  --header 'Authorization: Token [TOKEN VALUE]]'
`} />


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

<Code lang="json" code={` 
{
  "count": 1,
  "results": [
    {
      "id": <function_id>,
      "name": "My Function",
      "last_editor": "education@azion.com",
      "last_modified": "2025-08-14T16:37:54.966088Z",
      "product_version": "2.0",
      "active": true,
      "runtime": "azion_js",
      "execution_environment": "application",
      "code": "",
      "default_args": {},
      "reference_count": 0,
      "version": "-",
      "vendor": null
    }
  ]
}
`} />

1. Copy the `<function_id>` value.
2. Run the following `POST` request, replacing the function ID value with the value you received in the previous response:

<Code lang="bash" code={` 
curl --request POST \
  --url https://api.azion.com/v4/edge_application/applications/<application_id>/functions \
  --header 'Accept: application/json' \
  --header 'Authorization: Token [TOKEN VALUE]' \
  --header 'Content-Type: application/json' \
  --data '{
  "name": "My instance Name",
  "edge_function": <function_id>,
  "active": true
}'
`} />

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

<Code lang="json" code={`{
  "state": "pending",
  "data": {
    "id": <edge_function_instance_id>,
    "name": "My Function Instance",
    "args": {},
    "edge_function": 123456,
    "active": true,
    "last_editor": "your-email@example.com",
    "last_modified": "2025-08-14T16:41:56.699896Z"
  }
}`} />


  | Key | Description |
  | --- | --- |
  | `edge_function` | The ID of the function. This is not the same as the ID of the instantiated function, which is unique for each application's function instance. |
  | `name` | Name of the instantiated function. |
  | `args` | Definition of arguments required for the function. Some functions don't take arguments. |
  | `id` | ID of the instantiated function. |
  
1. 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 `<edge_function_instance_id>` variable with the function instance ID received in the previous response:

<Code lang="bash" code={` 
curl --request POST \
  --url https://api.azion.com/v4/edge_application/applications/<application_id>/request_rules \
  --header 'Accept: application/json' \
  --header 'Authorization: Token [TOKEN VALUE]' \
  --header 'Content-Type: application/json' \
  --data '{
  "name": "Run Hello World",
  "active": true,
  "criteria": [
    [
      {
        "conditional": "if",
        "variable": "\${uri}",
        "operator": "is_equal",
        "argument": "/hello-world"
      }
    ]
  ],
  "behaviors": [
    {
      "type": "run_function",
      "attributes": { "value": <edge_function_instance_id> }
    }
  ],
  "description": "Run function on /hello-world"
}'
`} />



:::note
For more information on the rules endpoint, see [Work with Rules Engine](/en/documentation/products/guides/build/work-with-rules-engine/).
:::

9. You'll receive a response similar to this, confirming your function was created successfully:

<Code lang="json" code={` 
{
  "state": "pending",
  "data": {
    "id": <rule_id>,
    "name": "Run Hello World",
    "active": true,
    "criteria": [
      [
        {
          "conditional": "if",
          "variable": "\${uri}",
          "operator": "is_equal",
          "argument": "/hello-world"
        }
      ]
    ],
    "behaviors": [
      {
        "type": "run_function",
        "attributes": {
          "value": <edge_function_instance_id> 
        }
      }
    ],
    "description": "Run function on /hello-world",
    "order": 0,
    "last_editor": "your-email@example.com",
    "last_modified": "2025-08-14T17:04:44.222534Z"
  }
}
`} />


10. Wait a few minutes for the changes to propagate.

To see your function running, access your application [using its domain](/en/documentation/products/guides/configure-a-domain/) and the URI set in the rule, which is in the format `xxxxxxxxx.map.azionedge.net/hello-world`.

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

</Tabs>