fetch
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBrowserbase Fetch API
Browserbase Fetch API
Fetch a page and return its content, headers, and metadata — no browser session required.
抓取页面并返回其内容、请求头和元数据,无需启动浏览器会话。
Prerequisites
前置要求
Get your 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"When to Use Fetch vs Browser
Fetch API 与 Browser Skill 使用场景对比
| Use Case | Fetch API | Browser Skill |
|---|---|---|
| Static page content | Yes | Overkill |
| Check HTTP status/headers | Yes | No |
| JavaScript-rendered pages | No | Yes |
| Form interactions | No | Yes |
| Page behind bot detection | Possible (with proxies) | Yes (stealth mode) |
| Simple scraping | Yes | Overkill |
| Speed | Fast | Slower |
Rule of thumb: Use Fetch for simple HTTP requests where you don't need JavaScript execution. Use the Browser skill when you need to interact with or render the page.
| 使用场景 | Fetch API | Browser Skill |
|---|---|---|
| 静态页面内容 | 是 | 大材小用 |
| 检查HTTP状态/请求头 | 是 | 否 |
| JavaScript渲染的页面 | 否 | 是 |
| 表单交互 | 否 | 是 |
| 反爬机制保护的页面 | 可实现(搭配代理) | 支持(隐身模式) |
| 简单爬取 | 是 | 大材小用 |
| 速度 | 快 | 较慢 |
经验法则:不需要执行JavaScript的简单HTTP请求使用Fetch。当你需要与页面交互或渲染页面时使用Browser skill。
Using with cURL
通过cURL调用
bash
curl -X POST "https://api.browserbase.com/v1/fetch" \
-H "Content-Type: application/json" \
-H "X-BB-API-Key: $BROWSERBASE_API_KEY" \
-d '{"url": "https://example.com"}'bash
curl -X POST "https://api.browserbase.com/v1/fetch" \
-H "Content-Type: application/json" \
-H "X-BB-API-Key: $BROWSERBASE_API_KEY" \
-d '{"url": "https://example.com"}'Request Options
请求参数
| Field | Type | Default | Description |
|---|---|---|---|
| string (URI) | required | The URL to fetch |
| boolean | | Whether to follow HTTP redirects |
| boolean | | Whether to bypass TLS certificate verification |
| boolean | | Whether to enable proxy support |
| 字段 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| string (URI) | 必填 | 要抓取的URL |
| boolean | | 是否跟随HTTP重定向 |
| boolean | | 是否绕过TLS证书校验 |
| boolean | | 是否启用代理支持 |
Response
响应
Returns JSON with:
| Field | Type | Description |
|---|---|---|
| string | Unique identifier for the fetch request |
| integer | HTTP status code of the fetched response |
| object | Response headers as key-value pairs |
| string | The response body content |
| string | The MIME type of the response |
| string | The character encoding of the response |
返回的JSON包含以下字段:
| 字段 | 类型 | 描述 |
|---|---|---|
| string | 抓取请求的唯一标识符 |
| integer | 抓取响应的HTTP状态码 |
| object | 响应头,以键值对形式返回 |
| string | 响应体内容 |
| string | 响应的MIME类型 |
| string | 响应的字符编码 |
Using with the SDK
通过SDK调用
Node.js (TypeScript)
Node.js (TypeScript)
bash
npm install @browserbasehq/sdktypescript
import { Browserbase } from "@browserbasehq/sdk";
const bb = new Browserbase({ apiKey: process.env.BROWSERBASE_API_KEY });
const response = await bb.fetchAPI.create({
url: "https://example.com",
allowRedirects: true,
});
console.log(response.statusCode); // 200
console.log(response.content); // page HTML
console.log(response.headers); // response headersbash
npm install @browserbasehq/sdktypescript
import { Browserbase } from "@browserbasehq/sdk";
const bb = new Browserbase({ apiKey: process.env.BROWSERBASE_API_KEY });
const response = await bb.fetchAPI.create({
url: "https://example.com",
allowRedirects: true,
});
console.log(response.statusCode); // 200
console.log(response.content); // page HTML
console.log(response.headers); // response headersPython
Python
bash
pip install browserbasepython
from browserbase import Browserbase
import os
bb = Browserbase(api_key=os.environ["BROWSERBASE_API_KEY"])
response = bb.fetch_api.create(
url="https://example.com",
allow_redirects=True,
)
print(response.status_code) # 200
print(response.content) # page HTML
print(response.headers) # response headersbash
pip install browserbasepython
from browserbase import Browserbase
import os
bb = Browserbase(api_key=os.environ["BROWSERBASE_API_KEY"])
response = bb.fetch_api.create(
url="https://example.com",
allow_redirects=True,
)
print(response.status_code) # 200
print(response.content) # page HTML
print(response.headers) # response headersCommon Options
常用配置
Follow redirects
跟随重定向
bash
curl -X POST "https://api.browserbase.com/v1/fetch" \
-H "Content-Type: application/json" \
-H "X-BB-API-Key: $BROWSERBASE_API_KEY" \
-d '{"url": "https://example.com/redirect", "allowRedirects": true}'bash
curl -X POST "https://api.browserbase.com/v1/fetch" \
-H "Content-Type: application/json" \
-H "X-BB-API-Key: $BROWSERBASE_API_KEY" \
-d '{"url": "https://example.com/redirect", "allowRedirects": true}'Enable proxies
启用代理
bash
curl -X POST "https://api.browserbase.com/v1/fetch" \
-H "Content-Type: application/json" \
-H "X-BB-API-Key: $BROWSERBASE_API_KEY" \
-d '{"url": "https://example.com", "proxies": true}'bash
curl -X POST "https://api.browserbase.com/v1/fetch" \
-H "Content-Type: application/json" \
-H "X-BB-API-Key: $BROWSERBASE_API_KEY" \
-d '{"url": "https://example.com", "proxies": true}'Bypass TLS verification
绕过TLS校验
bash
curl -X POST "https://api.browserbase.com/v1/fetch" \
-H "Content-Type: application/json" \
-H "X-BB-API-Key: $BROWSERBASE_API_KEY" \
-d '{"url": "https://self-signed.example.com", "allowInsecureSsl": true}'bash
curl -X POST "https://api.browserbase.com/v1/fetch" \
-H "Content-Type: application/json" \
-H "X-BB-API-Key: $BROWSERBASE_API_KEY" \
-d '{"url": "https://self-signed.example.com", "allowInsecureSsl": true}'Error Handling
错误处理
| Status | Meaning |
|---|---|
| 400 | Invalid request body (check URL format and parameters) |
| 429 | Concurrent fetch request limit exceeded (retry later) |
| 502 | Response too large or TLS certificate verification failed |
| 504 | Fetch request timed out (default timeout: 60 seconds) |
| 状态码 | 含义 |
|---|---|
| 400 | 请求体无效(请检查URL格式和参数) |
| 429 | 超出并发抓取请求限制(请稍后重试) |
| 502 | 响应过大或TLS证书校验失败 |
| 504 | 抓取请求超时(默认超时时间:60秒) |
Best Practices
最佳实践
- Start with Fetch for simple page retrieval — it's faster and cheaper than a browser session
- Enable when fetching URLs that may redirect (shortened URLs, login flows)
allowRedirects - Use when the target site has IP-based rate limiting or geo-restrictions
proxies - Check before processing
statusCodeto handle errors gracefullycontent - Fall back to Browser if Fetch returns empty content (page requires JavaScript rendering)
For detailed examples, see EXAMPLES.md.
For API reference, see REFERENCE.md.
- 优先使用Fetch获取简单页面,比浏览器会话速度更快、成本更低
- 抓取可能发生重定向的URL(短链接、登录流程)时启用
allowRedirects - 目标站点存在IP限流或地域限制时使用
proxies - 处理前先检查
content,优雅处理错误statusCode - 如果Fetch返回空内容请切换为Browser(页面需要JavaScript渲染)
更多详细示例请参考EXAMPLES.md。
完整API参考请查看REFERENCE.md。