Handlers
The handler shapes Azion Runtime calls in a function, the request, env, and ctx parameters of the fetch handler, and the deprecated shape.
A handler is the code Azion Runtime calls when a request reaches a function in 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.
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:
| Parameter | Type | Description |
|---|---|---|
request | Request | Incoming HTTP request. request.metadata carries the 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 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.
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. |
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:
A function instance on a firewall uses a firewall handler in place of fetch. For more information, refer to Functions on a firewall.
Example
This fetch handler returns a plain-text response to every request:
Deployed, the function returns this response: