A Node.js Google search client needs two checks: the HTTP request succeeded, and the provider’s business response contains the expected result object. For SearchCans, parse data.organic and use each result’s link. Keep an empty valid list distinct from a malformed response, business error or failed request.
Do not confuse fetch with the Axios wrapper
With native fetch, await response.json() gives the business payload, so the list is payload.data.organic. With Axios, response.data is that business payload, making the same path response.data.data.organic. In both cases data is an object, not the result array. Variable names alone do not establish which wrapper a function receives.
| Input | Expected behavior |
|---|---|
| HTTP failure | Stop before treating the body as results |
| Business code is not zero | Reject the response |
| data is a list or null | Reject the invalid envelope |
| data.organic is an empty list | Return a valid empty result set |
| Invalid or non-web result link | Exclude that result from URL output |
Run a complete client with native fetch
The example uses Node.js with native fetch; this revision was exercised on Node.js 24. Save it as wp229.mjs, set SEARCHCANS_API_KEY and run node wp229.mjs followed by the query words. It supplies the engine, country and language explicitly. The timeout bounds client waiting; it does not prove that remote work was cancelled.
import {pathToFileURL} from 'node:url';
export function parsePayload(payload) {
if (!payload || typeof payload !== 'object' || Array.isArray(payload) || payload.code !== 0)
throw new Error('Business error or invalid envelope');
const data = payload.data;
if (!data || typeof data !== 'object' || Array.isArray(data) || !Array.isArray(data.organic))
throw new Error('Expected data.organic list');
return data.organic.filter(item => {
if (!item || typeof item.link !== 'string') return false;
try {return ['https:', 'http:'].includes(new URL(item.link).protocol);} catch {return false;}
})
.map(item => ({link: item.link, title: item.title || item.link,
snippet: item.snippet || '', position: item.position ?? null}));
}
export async function search(query, request = fetch, apiKey = process.env.SEARCHCANS_API_KEY) {
if (!apiKey || typeof query !== 'string' || !query.trim()) throw new Error('Key and query required');
const response = await request('https://www.searchcans.com/api/v1/search', {
method: 'POST', headers: {Authorization: 'Bearer ' + apiKey, 'Content-Type': 'application/json'},
body: JSON.stringify({t: 'google', s: query, country: 'us', language: 'en', d: 30000}),
signal: AbortSignal.timeout(45000)
});
if (!response.ok) throw new Error('HTTP request failed: ' + response.status);
return {query, engine: 'google', country: 'us', language: 'en',
observed_at: new Date().toISOString(), results: parsePayload(await response.json())};
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
search(process.argv.slice(2).join(' ')).then(x => console.log(JSON.stringify(x, null, 2)))
.catch(() => {console.error('Search failed; check status, key, input and business response.'); process.exitCode = 1;});
}
Test the business envelope before a live query
Use injected responses for normal results, a valid empty list, malformed data, a nonzero business code, non-JSON content and an HTTP failure. Verify that the query and market survive in successful output. A synthetic request test does not establish a production availability or extraction success rate.
The parser excludes unusable result links and retains title, snippet and position when provided. A snippet remains a search observation, not a reviewed full-page source. To read selected sources, use Reader API and preserve extraction status alongside each URL.
Keep downstream tasks separate
Use this client in the source snapshot dashboard for sampled observations, or adapt its result object to the source-tracking agent tool. the SERP versus webpage collection guide owns operation choice. This page owns the Node.js request and response contract; it does not add an unsupported claim about no storage, legal compliance or model accuracy.