functions
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBrowserbase Functions
Browserbase Functions
Deploy serverless browser automation using the official CLI.
browse使用官方 CLI 部署无服务器浏览器自动化。
browsePrerequisites
前提条件
Get an API key from: https://browserbase.com/settings
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-functionThis creates:
my-function/
├── package.json
├── index.ts # Your function code
└── .env # Add credentials herebash
browse functions init my-function
cd my-function此命令会创建如下结构:
my-function/
├── package.json
├── index.ts # Your function code
└── .env # Add credentials here2. Add Credentials to .env
2. 将凭据添加到.env文件
bash
echo "BROWSERBASE_API_KEY=$BROWSERBASE_API_KEY" >> .envbash
echo "BROWSERBASE_API_KEY=$BROWSERBASE_API_KEY" >> .env3. Install Dependencies
3. 安装依赖
bash
pnpm installbash
pnpm installFunction 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:
- - CDP endpoint to connect Playwright
context.session.connectUrl - - Input parameters from invocation
context.params
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 };
});核心对象:
- - 用于连接Playwright的CDP端点
context.session.connectUrl - - 调用时传入的输入参数
context.params
Development Workflow
开发工作流程
1. Start Dev Server
1. 启动开发服务器
bash
browse functions dev index.tsServer runs at
http://127.0.0.1:14113bash
browse functions dev index.ts服务器运行在
http://127.0.0.1:141132. 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 for debugging - output appears in the terminal.
console.log()开发服务器会在文件变更时自动重载。使用 进行调试,输出内容会显示在终端中。
console.log()Deploying
部署
bash
browse functions publish index.tsOutput:
Function published successfully
Build ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Function ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxSave 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
快速参考
| Command | Description |
|---|---|
| Create new project |
| Start local dev server |
| Deploy to Browserbase |
For invocation examples, common patterns, and troubleshooting, see REFERENCE.md.
| 命令 | 描述 |
|---|---|
| 创建新项目 |
| 启动本地开发服务器 |
| 部署到Browserbase |
有关调用示例、常见模式和故障排查,请查看 REFERENCE.md。