SerpBase
Back to Blog

Google Search API in TypeScript: Typed Results With fetch

Interfaces for the search response and a typed fetch function, no SDK. Tested 5 December 2026: runs on Node.js 22 and passes tsc --strict.

December 5, 2026
By SerpBase Team3 min read

Short answer: in TypeScript, call fetch with a POST to https://api.serpbase.dev/google/search, put your key in the X-API-Key header, and cast the JSON to an interface you declare. No SDK is needed; the built-in fetch in Node.js 18 and later is enough.

Types and a typed function

interface OrganicResult {
  rank: number;
  title: string;
  link: string;
  snippet?: string;
  date?: string;
}

interface SearchResponse {
  status: number;
  error?: string;
  credits_charged?: number;
  organic?: OrganicResult[];
  people_also_ask?: { question: string }[];
  related_searches?: string[];
}

interface SearchParams {
  q: string;
  gl?: string;
  hl?: string;
  page?: number;
  device?: "mobile" | "desktop";
}

async function googleSearch(params: SearchParams): Promise<SearchResponse> {
  const res = await fetch("https://api.serpbase.dev/google/search", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "X-API-Key": process.env.SERPBASE_API_KEY ?? "",
    },
    body: JSON.stringify(params),
    signal: AbortSignal.timeout(60_000),
  });
  const data = (await res.json()) as SearchResponse;
  if (data.status !== 0) {
    throw new Error(`search failed: status ${data.status} ${data.error ?? ""}`);
  }
  return data;
}

const data = await googleSearch({ q: "typescript orm", gl: "us", hl: "en" });
for (const r of data.organic ?? []) {
  console.log(`${r.rank}. ${r.title}\n   ${r.link}`);
}
console.log("questions:", (data.people_also_ask ?? []).map((p) => p.question));

Every module of the results page is optional — Google shows a People Also Ask box for some queries and not others — so declare them with ? and read them with ?? []. Add more fields from the docs as you need them; TypeScript does not complain about fields you leave out of the interface.

What it printed

We sent the request on 5 December 2026 and ran the file against the recorded answer with Node.js 22 (the file also passes tsc --strict with TypeScript 5.9). The search took 2.9 seconds and returned 8 organic results, 4 People Also Ask questions and 1 related search:

1. TypeORM: A Smarter Way to Handle Databases in Node.js
   https://medium.com/@Piccosoft/typeorm-a-smarter-way-to-handle-databases-in-node-js-5368019383f7
2. Prisma ORM 8: TypeScript and Database Development Evolution
   https://www.linkedin.com/posts/akshitsharma0802_prismaorm-typescript-nodejs-activity-7520007287407669248-_OpE
3. Prisma vs Drizzle vs TypeORM: Choosing a Node.js ORM
   https://hunchbite.com/guides/prisma-vs-drizzle-vs-typeorm
questions: [
  'Is TypeScript still relevant in 2026?',
  'Is TypeScript difficult to learn?',
  'What is the latest version of TypeScript?',
  ...

Node.js 22 runs a .ts file directly (node search.ts) when the code only uses type annotations, as this one does; with older versions compile it with tsc first. Top-level await needs the file to be an ES module ("type": "module" in package.json).

Things that trip people up

  • A cast is not validation. as SearchResponse tells the compiler what to expect but checks nothing at run time. Read optional fields with ?. and ??, or validate with a schema library if bad data would be costly.
  • HTTP 200 does not mean success. Search failures return HTTP 200 with a non-zero status; the function throws on them. See SERP API error codes. Failed requests are not charged.
  • Some links are Google redirect links. 5 of the 8 link values in this run were https://www.google.com/goto?url=…. The free Google link resolver turns them into the real address.
  • Do not call it from browser code. The key would be visible to every visitor. Call the API from your server or a serverless function.

Related

Cost and docs

A Google search is 1 credit, $0.25–$0.50 per 1,000 depending on the pack. New accounts get 500 free credits.