Azion Runtime quickstart
Write a function in the ES Modules handler shape, call a Web API and the request metadata, run it with azion dev, and deploy it to Azion Runtime.
This guide takes you through your first function on Azion Runtime, from the code to a request on the deployed domain.
- Create a JavaScript function project with the Azion CLI.
- Write the handler in the recommended ES Modules shape,
export default { fetch(request, env, ctx) }. - Call a Web API and read the request metadata, an Azion API.
- Run the function with
azion dev, then deploy it and compare the two answers.
The function returns four values as JSON. EdgeRuntime names where the code runs, URL and crypto.randomUUID() are Web APIs, and request.metadata holds the request metadata that Azion adds to every request.
Prerequisites
- The Azion CLI, installed and logged in. For the setup, refer to Azion CLI quickstart.
- Node.js and npm. The CLI runs the Azion bundler with
npxto build the project. curlon your machine.
Create the project
Create a project named my-runtime-function with the CLI:
The command asks five questions. Select the Javascript preset and the Hello World template, enter Y to install the dependencies, and enter n to the local server and to the deploy. You run both after you write the handler. The command confirms the project:
The function entry is index.js, at the root of the new my-runtime-function directory. The template writes it in the older export default main shape, which the build reports as deprecated. For every shape the runtime accepts, refer to Handlers.
Write the handler
Replace the contents of index.js with this handler:
The runtime calls fetch once per request:
requestis the incoming Request, andrequest.metadatacarries the GeoIP and network data of the request.envholds the environment variables, andctxcarrieswaitUntil(). This handler uses neither.Response.json()builds a Response with the object as a JSON body andcontent-type: application/json.
The optional chaining in request.metadata?. keeps the handler working under azion dev, where the request carries no metadata.
Run the function locally
Go to the project directory and start the local development server:
The command builds the function and serves it on port 3333:
The build prints no deprecation warning, because the handler uses the ES Modules shape. In a second terminal, request a path:
The function answers with JSON:
Locally, EdgeRuntime is edge-runtime and country is null, because azion dev adds no request metadata. To stop the server, press Ctrl+C. For everything that differs between azion dev and a deployed function, refer to How Azion Runtime works.
Deploy the function
From the my-runtime-function directory, deploy the project:
The command uploads the project, builds it, creates the function, the application, and the workload, and prints the domain of the workload:
The first deploy can take several minutes to answer from every location. Until a location has the function, it answers 404 with a page titled There's nothing here yet. Wait and retry before you diagnose the deploy.
Request the deployed function
Request the same path from the domain that azion deploy printed. Replace <your-azion-domain> with that domain:
The deployed function answers with the same keys:
Deployed, EdgeRuntime is azion, and country holds the country of the client that sent the request, from the request metadata. requestId changes on every request. The function runs on Azion Runtime and answers on the workload domain.
To change the function, edit index.js and run azion deploy again from the my-runtime-function directory. Later deploys take about two minutes to answer from every location.