Build a screenshot API with Functions and Browserless
Deploy a Hono project on Functions that captures a page screenshot through the Browserless API and returns the PNG image.
In this tutorial, you will build a screenshot API that returns a web page as a PNG image. You will create the project from the Hono template, write the route, store the API token, deploy the project, and request the endpoint.
The function runs no browser of its own. It posts the target URL to Browserless, a hosted service that renders the page and answers with the image. The function returns those bytes to the client.
Prerequisites
- An Azion account. To create one, refer to How to create an account on Azion.
- Azion CLI installed on your machine. Refer to Azion CLI.
- Node.js version 18 or higher.
- A Browserless account and its API token. To create one, refer to Browserless.
- Working knowledge of JavaScript.
1. Authenticate Azion CLI
Sign in to your Azion account from the terminal:
Azion CLI opens a browser-based flow when you omit the credential flags. It stores the resulting credentials locally, and they authorize every later command.
2. Create the project from the Hono template
Azion CLI initializes the project from a starter template. To create it:
Run the command and answer the prompts that follow:
The prompt lists one preset per framework:
The prompt appears after Azion CLI fetches and configures the template:
The route and the API token are not in place yet:
Azion CLI creates the screenshot-api directory and writes the Hono project into it. Go to that directory:
The remaining commands run from there.
3. Write the screenshot route
The entry field of azion.config.js names the entry file of the project. Open that file and replace its contents with the route that captures the page:
The route reads the target page from the url query parameter, and it falls back to https://www.example.com. export default app is the ES Modules handler pattern, which Azion recommends over the Service Worker pattern. To compare both patterns, refer to Migrate handler patterns in Functions.
The complete project is in the browserless package of the functions examples repository.
4. Store the Browserless token as an environment variable
An environment variable holds the token outside the function code and outside version control. To create it:
Azion stores the variable on the account, and Azion.env.get() returns its value at run time. A variable whose key contains password, pwd, secret, key, hash, encrypted, passcode, auth, or token is sent as a secret by default. The --secret true flag repeats that default here.
A change to a variable does not reach a function that is already running. Redeploy the function for a new value to take effect. Azion Runtime also reads a variable through process.env. For both interfaces, the fields of a variable, and the other management interfaces, refer to Environment variables.
5. Deploy the project
Build the project and send it to Azion:
The command uploads the function code and configures the application and its routing rules. It returns a workload domain in the format https://xxxxxxx.map.azionedge.net.
The application answers on that domain a few minutes later, once the DNS propagation completes.
6. Verify the screenshot response
Request the route with the address of the page to capture:
The command prints the content type of the response:
The file screenshot.png holds the capture of https://www.example.com. A request without the url parameter captures the same page, because the route falls back to it. When Browserless rejects a request, the route answers with the body and the status code that Browserless returned.