快速上手
从拿到密钥到第一次搜索和网页读取,附错误处理。
1. 获取密钥
websearch.fast 使用 relay.fast 账户。用这个账户登录控制台创建密钥,或者在 relay.fast 的密钥页面创建。同一个密钥既能调用模型,也能调用 Web API,两者从同一份余额扣费。
export WEBSEARCH_API_KEY="sk-..."2. 搜索
curl https://api.websearch.fast/v1/web/search \
-H "Authorization: Bearer $WEBSEARCH_API_KEY" \
-H "Content-Type: application/json" \
-d '{"q": "postgres 18 release notes", "num": 5}'import os
import httpx
response = httpx.post(
"https://api.websearch.fast/v1/web/search",
headers={"Authorization": f"Bearer {os.environ['WEBSEARCH_API_KEY']}"},
json={
"q": "postgres 18 release notes",
"num": 5,
},
timeout=60,
)
response.raise_for_status()
print(response.json())const response = await fetch("https://api.websearch.fast/v1/web/search", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.WEBSEARCH_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
q: "postgres 18 release notes",
num: 5,
}),
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
console.log(await response.json());每条结果包含 position、title、url、domain、snippet 和 origin,网页标明了日期时还有 published_at。url 是网页本身的地址。全部字段见搜索。
3. 随结果获取网页正文
加上 content,同一个响应里就会带上排名靠前几个网页经过清洗的正文,模型不用再调用一次去读。
curl https://api.websearch.fast/v1/web/search \
-H "Authorization: Bearer $WEBSEARCH_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"q": "postgres 18 release notes",
"num": 5,
"content": {"pages": 3, "max_chars_per_page": 4000}
}'import os
import httpx
response = httpx.post(
"https://api.websearch.fast/v1/web/search",
headers={"Authorization": f"Bearer {os.environ['WEBSEARCH_API_KEY']}"},
json={
"q": "postgres 18 release notes",
"num": 5,
"content": {"pages": 3, "max_chars_per_page": 4000},
},
timeout=60,
)
response.raise_for_status()
print(response.json())const response = await fetch("https://api.websearch.fast/v1/web/search", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.WEBSEARCH_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
q: "postgres 18 release notes",
num: 5,
content: { pages: 3, max_chars_per_page: 4000 },
}),
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
console.log(await response.json());4. 读取网页
curl https://api.websearch.fast/v1/web/fetch \
-H "Authorization: Bearer $WEBSEARCH_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"url": "https://docs.example.org/guide/install",
"max_chars": 4000
}'import os
import httpx
response = httpx.post(
"https://api.websearch.fast/v1/web/fetch",
headers={"Authorization": f"Bearer {os.environ['WEBSEARCH_API_KEY']}"},
json={
"url": "https://docs.example.org/guide/install",
"max_chars": 4000,
},
timeout=60,
)
response.raise_for_status()
print(response.json())const response = await fetch("https://api.websearch.fast/v1/web/fetch", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.WEBSEARCH_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
url: "https://docs.example.org/guide/install",
max_chars: 4000,
}),
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
console.log(await response.json());5. 处理错误
所有原生路径的错误格式都一样。先看 HTTP 状态码,再看 error.code。消息是写给人看的,内容可能会变。报告故障时请附上 request_id。
{
"error": {
"code": "fetch_failed",
"message": "The page could not be read. (request id: 20261128120008123456789a1b2c3d4Kx7Qm2Ph)",
"type": "invalid_request_error",
"param": "url"
},
"request_id": "20261128120008123456789a1b2c3d4Kx7Qm2Ph"
}429 等到 Retry-After 之后重试,500、503 和 504 按退避策略重试并设上限。其他 4xx 不要原样重试。全部错误码见错误页面。
Base URL
| 域名 | Base URL |
|---|---|
| websearch.fast | https://api.websearch.fast/v1/web |
| relay.fast | https://relay.fast/v1/web |
| relay.fast 备用域名 | https://direct.relay-fast.com/v1/web |
所有域名都接受同样的 relay.fast 密钥,提供同样的路径。API 路径从不重定向,因为重定向可能丢掉 Authorization 请求头。路径错误时返回 404 invalid_endpoint。
面向 AI 智能体
- 从环境变量或密钥管理服务读取密钥。不要打印密钥,也不要把它放进 URL。
- 根据 HTTP 状态码和
error.code判断,不要依据消息文本。 - 网页调用会产生费用。一次带
content的search,比一次搜索再加几次fetch便宜。除非操作者明确要求,不要循环处理大批 URL。 - 遇到
429等待Retry-After。遇到5xx按退避策略重试并设上限。site_refused不要重试。 - 报告故障时附上
request_id。