浏览文档
开始使用
使用 FlashData 完成首次 Google 搜索,为应用或 AI 智能体获取结构化结果。
发送首次 API 请求
想直接编写代码?在终端按以下三步操作。
获取 API Key
创建账户并验证邮箱,然后创建具有 Google 搜索权限的 API Key。已有密钥可直接使用。
在终端运行以下命令,在隐藏输入提示中粘贴密钥。已设置 FLASHDATA_API_KEY 时可跳过。
设置密钥# Bash: paste the key into a hidden prompt, not into this command. read -r -s -p "FlashData API key: " FLASHDATA_API_KEY printf '\n' export FLASHDATA_API_KEY设置密钥# PowerShell 7: paste the key into a hidden prompt. $flashdataSecureKey = Read-Host "FlashData API key" -AsSecureString $env:FLASHDATA_API_KEY = [System.Net.NetworkCredential]::new("", $flashdataSecureKey).Password Remove-Variable flashdataSecureKey执行搜索
选择语言,在同一终端运行示例。每次运行发送一次搜索请求。
Python 3.10+ · 将示例保存为 quickstart.py。
安装python -m pip install "flashdata==1.1.0"quickstart.pyimport json import os from flashdata import FlashData with FlashData(api_key=os.environ["FLASHDATA_API_KEY"]) as client: response = client.google.search("battery recycling research", num=5) if response["status"] != "done": raise RuntimeError(response) print(json.dumps(response, indent=2))运行python quickstart.pyNode.js 22.18+ · 在空文件夹中开始,保存为 quickstart.mts。
配置npm init -y npm install --save-exact @flashdata-dev/[email protected]quickstart.mtsimport { FlashData } from '@flashdata-dev/sdk'; const apiKey = process.env.FLASHDATA_API_KEY; if (!apiKey) throw new Error('Set FLASHDATA_API_KEY first.'); const client = new FlashData({ apiKey }); const response = await client.google.search({ query: 'battery recycling research', num: 5, }); if (response.status !== 'done') throw new Error(JSON.stringify(response)); console.log(JSON.stringify(response, null, 2));运行node quickstart.mtsNode.js 22+ · 在空文件夹中开始,保存为 quickstart.mjs。
配置npm init -y npm install --save-exact @flashdata-dev/[email protected]quickstart.mjsimport { FlashData } from '@flashdata-dev/sdk'; const apiKey = process.env.FLASHDATA_API_KEY; if (!apiKey) throw new Error('Set FLASHDATA_API_KEY first.'); const client = new FlashData({ apiKey }); const response = await client.google.search({ query: 'battery recycling research', num: 5, }); if (response.status !== 'done') throw new Error(JSON.stringify(response)); console.log(JSON.stringify(response, null, 2));运行node quickstart.mjsGo 1.25+ · 在空文件夹中开始,保存为 main.go。已有模块可跳过 go mod init。
配置go mod init example.com/flashdata-quickstart go get github.com/flashdatadev/[email protected]main.gopackage main import ( "context" "encoding/json" "log" "os" "time" flashdata "github.com/flashdatadev/flashdata-go" ) func main() { key := os.Getenv("FLASHDATA_API_KEY") if key == "" { log.Fatal("Set FLASHDATA_API_KEY through your server's secret manager.") } client, err := flashdata.New(flashdata.Options{APIKey: key}) if err != nil { log.Fatal(err) } ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) defer cancel() result, err := client.Google.Search(ctx, flashdata.GoogleSearchInput{ Query: "battery recycling research", Params: &flashdata.GoogleSearchParams{Num: flashdata.Ptr(int64(5))}, }) if err != nil { // Do not automatically resubmit an uncertain paid request. log.Fatal(err) } if err := json.NewEncoder(os.Stdout).Encode(result); err != nil { log.Fatal(err) } if result.Status != "done" { log.Fatalf("Search did not complete: %s", result.Status) } }运行go run .无需 SDK。在 Bash 中运行 cURL 7.76+ 命令,或使用 PowerShell 7 命令。
Bash / cURLcurl --fail-with-body --silent --show-error \ --request POST https://data.flashdata.dev/v1/queries/realtime \ --header "X-API-Key: $FLASHDATA_API_KEY" \ --header 'Content-Type: application/json' \ --data '{"source":"google_search","query":"battery recycling research","params":{"num":5}}'PowerShell 7$flashdataRequest = '{"source":"google_search","query":"battery recycling research","params":{"num":5}}' $flashdataResult = Invoke-RestMethod -Method Post -Uri 'https://data.flashdata.dev/v1/queries/realtime' -Headers @{ 'X-API-Key' = $env:FLASHDATA_API_KEY } -ContentType 'application/json' -Body $flashdataRequest $flashdataResult | ConvertTo-Json -Depth 20查看结果
成功响应的 status 为 done。在 results[].organic 中查看来源,在 usage.credits 中查看本次扣费。
响应示例(精简){ "status": "done", "results": [ { "organic": [ { "title": "Battery recycling research", "link": "https://example.org/recycling", "snippet": "An example search excerpt." } ] } ], "usage": { "credits": 0.02 } }
在控制台试用
- 创建账户或登录,完成邮箱验证以及控制台提示的账户设置。
- 打开 Google 搜索,选择 Google 搜索来源和实时模式,输入 battery recycling research。展开更多参数,将每页数量(num)设为 5。
- 检查显示的请求与可用 Credits,再执行一次搜索。
- 检查响应及用量。引用超出摘要的信息前,打开返回来源核对内容。
继续使用首次结果
首次请求失败时
- 缺少 FLASHDATA_API_KEY
- 在运行示例的同一终端或运行环境设置密钥;这些代码不会读取 .env 文件。
- 401 / 403
- 检查密钥或连接是否有效,并具有 google_search 权限。
- 402
- 在 Billing 检查可用 Credits。需要所有者访问时认领现有试用。
- 429
- 遵守 Retry-After,保留相同身份及注册凭证,不要创建另一试用。
- 超时或提交状态不确定
- 重试前检查现有运行。CLI 用户保留相同配置及运行 ID;SDK/API 用户保留已返回的任务或请求 ID。