
A Next.js template demonstrating how to run browser automations in Vercel serverless functions, powered by Kernel.
This template shows how to:
Clone the repository:
git clone <your-repo-url>cd nextjs-kernel-template
Install dependencies:
bun install
Set up Kernel:
Get your Kernel API key from one of these sources:
Configure environment variables:
Create a .env file:
touch .env.local
Add your Kernel API key:
KERNEL_API_KEY=your_api_key_here
Run the development server:
bun devbun dev
Open http://localhost:3000 in your browser
app/├── api/│ ├── create-browser/│ │ └── route.ts # Creates a headful Kernel browser│ └── run-automation/│ └── route.ts # Connects via CDP and runs automation├── page.tsx # Main UI with live view and controls├── layout.tsx # Root layout└── globals.css # Global stylescomponents/└── ui/ # shadcn/ui components├── button.tsx├── card.tsx└── input.tsxlib/└── utils.ts # Utility functions
Step 1: Create Browser (app/api/create-browser/route.ts)
import { Kernel } from "@onkernel/sdk";// Initialize Kernel clientconst kernel = new Kernel({ apiKey: process.env.KERNEL_API_KEY });// Create a headful browser with live viewconst browser = await kernel.browsers.create({stealth: true,headless: false,});// Return browser details to clientreturn {sessionId: browser.session_id,liveViewUrl: browser.browser_live_view_url,cdpWsUrl: browser.cdp_ws_url,};
Step 2: Run Automation (app/api/run-automation/route.ts)
import { chromium } from "playwright-core";// Connect Playwright to the Kernel browser via CDPconst browser = await chromium.connectOverCDP(cdpWsUrl);// Get the default context and pageconst context = browser.contexts()[0];const page = context.pages()[0] || (await context.newPage());// Navigate and extract dataawait page.goto(url, { waitUntil: "domcontentloaded" });const title = await page.title();// Close Playwright connection (browser continues running)await browser.close();return { title, url };
Push to GitHub
Connect to Vercel:
KERNEL_API_KEY environment variableUsing Vercel Marketplace Integration:
Make sure to add this environment variable in your Vercel project settings:
KERNEL_API_KEY - Your Kernel API key