Skip to main content
This guide walks you through how to build a fully functional backend that can convert any website into HTML, take screenshots, and fill out forms using Puppeteer, Vercel, and Browserbase.
To run these at scale, you’ll also use headless browsers (browsers without a user interface), which are often used for scaling web automations, testing, and data collection.

Prerequisites

Vercel: A developer infrastructure platform that lets you build, deploy, and scale. Vercel owns & maintains Next.js, one of the most popular frontend frameworks, allowing you to build applications completely out-of-box without additional configuration. Browserbase: A headless browser infrastructure platform that provides ready-to-use browsers out of the box. This includes observability, proxies, Verified, and additional debugging tools for your automation scripts. Browsers are essential for interacting with the web, and Browserbase simplifies this process by managing multiple browser sessions and providing debugging capabilities from the start. Puppeteer: A Node library that provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default but can be configured to run a full version of Chrome or Chromium.

Step 1: Setting up your project

First, you’ll need to create a Browserbase account. You can sign up for a free account here. You’ll also need a Vercel account, of which you can sign up for a free account here. Now, create a Next.js app through the CLI. This project is called vercel-automation.

Install packages

Install Browserbase’s Node.js SDK, Stagehand AI SDK, Zod for data validation, and Prettier for formatting.

Managing API keys

Be sure to add environment variables for this project. You’ll need a Browserbase API key. You can get it from the Browserbase Settings.

Step 2: Using Next.js route handlers

Next.js Route Handlers let you create custom API endpoints that process HTTP requests and return web content through APIs, directly within your application.
  • Next.js provides helper classes NextRequest and NextResponse to simplify working with native Request/Response APIs.
  • Route Handlers are exclusively available within the app directory.
In this project, you’ll create three route handlers, each for a different web automation task. The route handlers use Browserbase headless browser infrastructure for HTML content collection, screenshot captures, and form submissions. Make sure you have the following directory structure:

HTML

Create the first route handler for retrieving HTML. Create an html directory in the app/api folder. To create an endpoint, add a route.ts file that handles the API requests. Use the GET method to retrieve HTML content from a specified URL. Here’s the code for the first route handler:

Screenshots

For the second route handler, create a new browser session with a specified viewport, navigate to the URL, and screenshot the screen. Create screenshot/route.ts and use the following code to enable screenshot abilities.

Form inputs

Puppeteer can be a bit cumbersome for form interactions. For the Form API route handler, the example uses Stagehand, an AI SDK. Stagehand simplifies complex browser interactions by letting you use plain English. Stagehand consists of three main functions: Act, Extract, and Observe. In this example, Stagehand is initialized with Browserbase credentials and an LLM model to efficiently fill out a sample form, rather than writing a more complex Puppeteer script. Below is the same web automation task, comparing the Puppeteer and Stagehand implementations.
If you’re using Stagehand, you’ll need to set up an LLM provider. Be sure to include the environment variable for your LLM provider in your .env file.

Testing the API endpoints

Now that the three route handlers are implemented in the Next.js app, test the API endpoints.
  1. Start the development server:
  2. Access the API at the base URL: http://localhost:3000/api
  3. Test each endpoint by navigating to:
    • http://localhost:3000/api/html
    • http://localhost:3000/api/screenshot
    • http://localhost:3000/api/form
Each endpoint should return a 200 status code when working correctly.

Step 3: Deploying to Vercel

Finally, after testing the API endpoints locally, deploy to Vercel:
  1. Sign in to Vercel
  2. Click “Add New…”“Project”
  3. Connect and select your repository
  4. Add any environment variables
  5. Click “Deploy”
  6. Once complete, you’ll get a deployment URL

Deploying with fluid compute

Fluid Compute
Fluid compute is a new infrastructure model from Vercel that balances the benefits of dedicated servers and serverless computing. These mini-servers start up only when needed, grow instantly as traffic increases, use what’s already running before adding more compute. You only pay for what you actually use. Fluid compute also handles advanced tasks, cuts costs, runs close to your data, requires no setup, and works with standard Node.js and Python. To learn more, you can read more about it in the announcement and documentation.

Why Fluid Compute for browser automations

For your route handlers connecting to Browserbase’s headless browser infrastructure, Fluid Compute offers key benefits:
  • Performance optimization - Route handlers that orchestrate complex browser automations remain responsive under load, with warm mini-servers eliminating cold start delays when initiating browser sessions
  • Optimized concurrency - Multiple function invocations share a single instance, allowing concurrent processing while some requests wait for Browserbase responses, eliminating idle resource waste
  • Extended, efficient runtimes - Complex automation workflows that would timeout in standard serverless functions complete successfully, while you only pay when your route handlers are processing requests
Although Vercel doesn’t handle the browser sessions directly (Browserbase does), Fluid Compute makes browser automation projects significantly more reliable, cost-effective, and performant at scale for AI applications.

How to enable Fluid Compute

  1. Go to your project settings in Vercel
  2. Select Functions from the left navigation menu
  3. Toggle the Fluid compute button to enable it
  4. Click Save
  5. Redeploy your project
You will see a higher New Function Duration and New Function Max Duration as a result Fluid Compute shows how much storage and computing resources you’ve saved by optimizing resource usage across requests.
Comparison
As you grow in traffic, multiple requests begin to add up. You can monitor these savings in the Observability tab, which displays metrics on function performance, resource utilization, and cost efficiency. This data helps you quantify the benefits of Fluid compute as your application scales, potentially reducing your compute costs by up to 85% compared to traditional serverless approaches.

Conclusion

Congratulations! Now you have a fully functional web application that can convert any website into HTML, take screenshots, and fill out forms using Puppeteer and Browserbase. This project demonstrates how to leverage Vercel’s serverless functions, Next.js route handlers, Fluid Compute, Stagehand, and Browserbase headless browsers to create a practical web application. Feel free to check out the completed code on GitHub.