← ブログに戻る

Node.jsでGoogle Search API: 完全チュートリアル (2026)

Node.js / JavaScriptでGoogle Search APIを使う — fetchベースのリクエスト、非同期ページネーション、リトライ、型付きヘルパー。serp.cheapでコピペできる実例付き。

1 分で読めます

serp.cheapを使って、Node.jsからGoogleを検索し構造化されたJSONを取得します。Node 18以降にはfetchが組み込まれているため、依存パッケージは不要です。

最小限の例

const res = await fetch("https://api.serp.cheap/v1/search", {
  method: "POST",
  headers: { "X-API-Key": "YOUR_KEY", "Content-Type": "application/json" },
  body: JSON.stringify({ q: "best electric cars 2026", gl: "us", page: 1 }),
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const { organic, stats } = await res.json();
organic.forEach(r => console.log(`#${r.position} ${r.title} — ${r.link}`));
console.log("cost (credits):", stats.cost, "cached:", stats.cached);

リトライ付きの小さなヘルパー

async function search(q, { gl = "us", page = 1, tbs, retries = 4 } = {}) {
  const body = JSON.stringify({ q, gl, page, ...(tbs && { tbs }) });
  for (let attempt = 0; ; attempt++) {
    const res = await fetch("https://api.serp.cheap/v1/search", {
      method: "POST",
      headers: { "X-API-Key": process.env.SERP_KEY, "Content-Type": "application/json" },
      body,
    });
    if (res.ok) return res.json();
    if (![429, 500, 502, 503, 504].includes(res.status) || attempt >= retries) {
      throw new Error(`HTTP ${res.status}`);
    }
    await new Promise(r => setTimeout(r, 1000 * 2 ** attempt)); // backoff
  }
}

const data = await search("serp api nodejs", { gl: "us" });

ページネーション

async function pages(q, n = 3) {
  const results = [];
  for (let p = 1; p <= n; p++) results.push(...(await search(q, { page: p })).organic);
  return results;
}

各ページは個別に課金されるリクエストです。APIキーはサーバーサイドに保管し、ブラウザに送信しないでください。

次のステップ

Frequently asked questions

Node.jsでGoogle Search APIを呼び出すには?+

組み込みのfetch (Node 18以降) を使ってクエリをPOSTします。serp.cheapの場合: fetch('https://api.serp.cheap/v1/search', { method: 'POST', headers: { 'X-API-Key': KEY, 'Content-Type': 'application/json' }, body: JSON.stringify({ q, gl: 'us' }) })。

SDKは必要ですか?+

必須ではありません — SERP APIはシンプルなHTTPエンドポイントなので、ネイティブのfetch (またはaxios) で十分です。ただしserp.cheapは、型付きの呼び出し・リトライ・ページネーションを備えた公式のJavaScript/TypeScript SDK (npm install @serpcheap/sdk) に加えて、NestJSとNext.jsの統合も提供しています。

APIキーを取得

直接購入が最安, カードまたは暗号資産で支払い、KYC不要。またはマーケットプレイス経由。