# Handlers

A handler is the code Azion Runtime calls when a request reaches a function in [Functions](/en/documentation/platform/functions/). In the ES Modules pattern, the default export of the module is an object whose `fetch` method receives the request and returns a `Response`. A handler answers HTTP requests, applies firewall and access control logic, and runs asynchronous work while Azion Runtime processes the request.

> **Note**
>
> Under `azion dev`, `env` is the `process.env` of your machine: the whole shell environment when the project has no `.env` file, or only the `.env` keys when it has one. `ctx` holds only `waitUntil`, and `request.metadata` is `undefined`.

---

## Fetch handler

The fetch handler is the `fetch` method of the default export. Azion Runtime calls it with three arguments each time the function runs:

```javascript
async fetch(request, env, ctx)
```

| Parameter                | Type                                                                   | Description                                                                                                                                                                            |
| ------------------------ | ---------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `request`                | [`Request`](/en/documentation/devtools/runtime/api-reference/request/) | Incoming HTTP request. `request.metadata` carries the [metadata](/en/documentation/devtools/runtime/api-reference/metadata/) of the request, such as its geolocation and TLS protocol. |
| `env`                    | Object                                                                 | An empty object in a deployed function. Environment variables are read with `Azion.env.get()` or `process.env`.                                                                        |
| `ctx`                    | `Context`                                                              | Execution context of the request. It carries `args` and `waitUntil`.                                                                                                                   |
| `ctx.args`               | Object                                                                 | [Args](/en/documentation/platform/functions/how-it-works/#args) of the function instance, a JSON object.                                                                               |
| `ctx.waitUntil(promise)` | Function                                                               | Extends the execution until `promise` settles.                                                                                                                                         |

The `env` parameter holds no variables in a deployed function. To read a variable, call `Azion.env.get('<name>')` or read `process.env.<name>`: both return its value. For more information, refer to [Environment variables API](/en/documentation/devtools/runtime/api-reference/environment-variables/).

---

## Handler shapes

Azion Runtime runs a function written in the ES Modules pattern, in the Service Worker pattern, or as a default-export function. The ES Modules object is the recommended shape:

| Shape                                             | Pattern                 | What the handler receives                                                                                                                                                                                     |
| ------------------------------------------------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `export default { fetch(request, env, ctx) }`     | ES Modules, recommended | `request`, `env`, and `ctx`, as the fetch handler table describes them.                                                                                                                                       |
| `addEventListener('fetch', (event) => {})`        | Service Worker          | A `FetchEvent` with `request`, `args`, `console`, `respondWith()`, and `waitUntil()`. For more information, refer to [addEventListener](/en/documentation/devtools/runtime/api-reference/add-eventlistener/). |
| `export default main`, where `main` takes `event` | Deprecated              | The same `FetchEvent` that the Service Worker pattern receives.                                                                                                                                               |

A function whose default export is a function still runs, and `azion build` prints this deprecation line for it:

```text
[Azion] [Build] › ⚠  warning   DEPRECATED: Migrate handler to → export default { fetch: (request, env, ctx) => {...} }
```

A function instance on a firewall uses a `firewall` handler in place of `fetch`. For more information, refer to [Functions on a firewall](/en/documentation/platform/functions/how-it-works/#functions-on-a-firewall).

---

## Example

This fetch handler returns a plain-text response to every request:

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

Deployed, the function returns this response:

```json
{
 "status": 200,
 "statusText": "",
 "headers": {
  "content-type": "text/plain;charset=UTF-8"
 },
 "body": "Hello World!"
}
```

---

## Related resources

- [How Functions works](/en/documentation/platform/functions/how-it-works.md): How a rule, a function instance, and its Args reach the handler.
- [Environment variables API](/en/documentation/devtools/runtime/api-reference/environment-variables.md): How a handler reads the variables and secrets of the account.
- [FetchEvent](/en/documentation/devtools/runtime/api-reference/fetch-event.md): The event a Service Worker handler receives, and its methods.
- [Response](/en/documentation/devtools/runtime/api-reference/response.md): The object a handler returns to answer the request.
