functions

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Browserbase Functions

Browserbase Functions

Deploy serverless browser automation using the official
browse
CLI.
使用官方
browse
CLI 部署无服务器浏览器自动化。

Prerequisites

前提条件

bash
export BROWSERBASE_API_KEY="your_api_key"
从以下地址获取API密钥:https://browserbase.com/settings
bash
export BROWSERBASE_API_KEY="your_api_key"

Creating a Function Project

创建函数项目

1. Initialize

1. 初始化

bash
browse functions init my-function
cd my-function
This creates:
my-function/
├── package.json
├── index.ts        # Your function code
└── .env            # Add credentials here
bash
browse functions init my-function
cd my-function
此命令会创建如下结构:
my-function/
├── package.json
├── index.ts        # Your function code
└── .env            # Add credentials here

2. Add Credentials to .env

2. 将凭据添加到.env文件

bash
echo "BROWSERBASE_API_KEY=$BROWSERBASE_API_KEY" >> .env
bash
echo "BROWSERBASE_API_KEY=$BROWSERBASE_API_KEY" >> .env

3. Install Dependencies

3. 安装依赖

bash
pnpm install
bash
pnpm install

Function Structure

函数结构

typescript
import { defineFn } from "@browserbasehq/sdk-functions";
import { chromium } from "playwright-core";

defineFn("my-function", async (context) => {
  const { session, params } = context;

  // Connect to browser
  const browser = await chromium.connectOverCDP(session.connectUrl);
  const page = browser.contexts()[0]!.pages()[0]!;

  // Your automation
  await page.goto(params.url || "https://example.com");
  const title = await page.title();

  // Return JSON-serializable result
  return { success: true, title };
});
Key objects:
  • context.session.connectUrl
    - CDP endpoint to connect Playwright
  • context.params
    - Input parameters from invocation
typescript
import { defineFn } from "@browserbasehq/sdk-functions";
import { chromium } from "playwright-core";

defineFn("my-function", async (context) => {
  const { session, params } = context;

  // Connect to browser
  const browser = await chromium.connectOverCDP(session.connectUrl);
  const page = browser.contexts()[0]!.pages()[0]!;

  // Your automation
  await page.goto(params.url || "https://example.com");
  const title = await page.title();

  // Return JSON-serializable result
  return { success: true, title };
});
核心对象:
  • context.session.connectUrl
    - 用于连接Playwright的CDP端点
  • context.params
    - 调用时传入的输入参数

Development Workflow

开发工作流程

1. Start Dev Server

1. 启动开发服务器

bash
browse functions dev index.ts
Server runs at
http://127.0.0.1:14113
bash
browse functions dev index.ts
服务器运行在
http://127.0.0.1:14113

2. Test Locally

2. 本地测试

bash
curl -X POST http://127.0.0.1:14113/v1/functions/my-function/invoke \
  -H "Content-Type: application/json" \
  -d '{"params": {"url": "https://news.ycombinator.com"}}'
bash
curl -X POST http://127.0.0.1:14113/v1/functions/my-function/invoke \
  -H "Content-Type: application/json" \
  -d '{"params": {"url": "https://news.ycombinator.com"}}'

3. Iterate

3. 迭代开发

The dev server auto-reloads on file changes. Use
console.log()
for debugging - output appears in the terminal.
开发服务器会在文件变更时自动重载。使用
console.log()
进行调试,输出内容会显示在终端中。

Deploying

部署

bash
browse functions publish index.ts
Output:
Function published successfully
Build ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Function ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Save the Function ID - you need it to invoke.
bash
browse functions publish index.ts
输出示例:
Function published successfully
Build ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Function ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
请保存函数ID - 调用函数时需要用到它。

Quick Reference

快速参考

CommandDescription
browse functions init <name>
Create new project
browse functions dev <file>
Start local dev server
browse functions publish <file>
Deploy to Browserbase
For invocation examples, common patterns, and troubleshooting, see REFERENCE.md.
命令描述
browse functions init <name>
创建新项目
browse functions dev <file>
启动本地开发服务器
browse functions publish <file>
部署到Browserbase
有关调用示例、常见模式和故障排查,请查看 REFERENCE.md