Migrate from a provider's API
Replace a direct call to a search provider with searchSDK, keep provider-only options, and map the response fields.
A direct call builds the provider's request, reads its own response shape and handles its own errors. With searchSDK, the request, the result shape and the error codes are the same for every provider.
Before
const response = await fetch("https://api.exa.ai/search", {
method: "POST",
headers: { "content-type": "application/json", "x-api-key": process.env.EXA_API_KEY ?? "" },
body: JSON.stringify({ query: "sqlite wal mode", numResults: 5, category: "news" }),
});
if (!response.ok) throw new Error(`Exa ${response.status}`);
const body = (await response.json()) as { results: { url: string; title: string | null }[] };
console.log(body.results.map((r) => r.url));After
import { search } from "@searchsdk/sdk";
const res = await search("sqlite wal mode", {
provider: "exa",
maxResults: 5,
providerOptions: { exa: { category: "news" } },
});
console.log(res.results.map((r) => r.url), res.cost.usd);Step by step
- Install
@searchsdk/sdkand keep the provider's key variable; searchSDK reads the same name. - Map the parameters that have a common name (
maxResults, domains, dates,country,language,depth). Provider support shows each provider's mapping. - Move the rest under
providerOptions[id]. They are validated before sending. - Read
results, orpagesfor contents. Fields with no common name stay inproviderFields, and the provider's whole body stays inraw. - Replace status checks with
SearchErrorcodes (Errors). - Add a
fallbackwhen you want a second provider.
Options a provider cannot honour fail as unsupported instead of being dropped, so a migrated call never silently changes meaning.