Run the Function
Deploy once. Invoke instantly.
Code
import { defineFn } from "@browserbasehq/sdk-functions";
import { chromium } from "playwright-core";
type HNSubmission = {
title: string | null;
url: string | null;
rank: number;
};
defineFn("my-function", async (context) => {
const { session } = context;
const browser = await chromium.connectOverCDP(session.connectUrl);
const browserContext = browser.contexts()[0]!;
const page = browserContext.pages()[0]!;
await page.goto("https://news.ycombinator.com");
await page.waitForSelector(".athing", { timeout: 30000 });
const titles = await page.evaluate(() => {
const results: HNSubmission[] = [];
document.querySelectorAll(".athing").forEach((submission, idx) => {
if (idx >= 3) return;
const titleElement = submission.querySelector(".titleline > a");
if (titleElement) {
results.push({
title: titleElement.textContent ?? null,
url: titleElement.getAttribute("href"),
rank: idx + 1,
});
}
});
return results;
});
return {
message: "Successfully fetched top Hacker News stories",
timestamp: new Date().toISOString(),
results: titles,
};
});Output
{
}Invokes a deployed Browserbase Function and returns structured JSON
This demo mirrors the real invocation flow.