serpapi-sdk-patterns

v2026.09.24

Wrap the official SerpAPI Python or JavaScript client behind typed, testable boundaries with safe errors, pagination, and output selection. Use when production code needs a stable search adapter. Trigger with "design a SerpAPI client wrapper".

GitHub
安装命令
npx skhub add jeremylongshore/serpapi-sdk-patterns
Markdown
SKILL.md

SerpAPI Production Client Patterns

Overview

Create a narrow application-owned gateway instead of spreading vendor parameters, credentials, and variable result schemas through business code.

Prerequisites

  • A selected official client and pinned dependency version
  • Supported engines, required output fields, latency objective, and failure policy
  • Sanitized fixtures for each supported response shape

Tool Discipline

Use Read, Glob, and Grep to map current call sites and types, WebFetch to verify official client interfaces, and Write or Edit for the gateway, schemas, fixtures, and tests.

Current Contract

The Python client returns a SerpResults mapping and provides next_page() and yield_pages(). The JavaScript client exposes promise and callback forms including getJson, getJsonBySearchId, and getAccount, but does not provide built-in pagination. Search output can be JSON, HTML, or Markdown; json_restrictor can reduce JSON payload fields.

Authentication

Inject SERPAPI_KEY at the outer server-side composition root. Do not include it in domain types, cache keys, error objects, telemetry, or serialized request parameters.

Instructions

  1. Define a request type that admits only supported engines and application-controlled locale, safety, pagination, and output options.
  2. Define a normalized response type with explicit optional sections and vendor metadata isolated from domain data.
  3. Inject the official client behind a small interface so fixtures can replace it without network interception.
  4. Centralize timeouts, bounded retry for transient failures, 429 classification, redaction, and search-ID logging.
  5. For Python, use yield_pages() only with a page/search budget; for JavaScript, implement engine-specific manual pagination from documented tokens or offsets.
  6. Select JSON for structured parsing, Markdown for token-efficient agent consumption, HTML only for approved debugging, and json_restrictor when supported fields are known.
  7. Add contract tests for empty-success, optional sections, processing/error states, pagination termination, timeout, and redaction.

Output

Return request and response types, gateway code, client-version pin, pagination and retry budgets, output-selection rationale, fixture tests, and redaction evidence.

Error Handling

ConditionResponse
Unknown engine or parameterReject locally before making a request.
Python HTTPError or TimeoutErrorMap to a typed application error and preserve safe status/search metadata.
JavaScript rejectionNormalize the status and message without serializing request credentials.
Pagination budget reachedReturn an explicit partial result and continuation state.

Example

import { getJson } from "serpapi";

export async function searchGoogle(query: string) {
  const result = await getJson({
    engine: "google",
    q: query,
    api_key: process.env.SERPAPI_KEY,
    json_restrictor: "organic_results[].{position,title,link}",
  });
  return (result.organic_results ?? []).map(({ position, title, link }) => ({
    position, title, link,
  }));
}

Resources

Next Steps

Exercise the gateway against sanitized fixtures, then canary one approved live request per supported engine.

发现
标签

此技能尚未发布标签。

版本
最新版本元数据

版本

v2026.09.24

发布时间

2026年9月24日

分类

未分类

许可证

MIT

源路径

skills/.curated/serpapi-sdk-patterns

默认分支

main

最新提交

e5a6c3b

Tree SHA

c2dc8e8