Skip to main content

The result union

No SDK method throws for API or network errors. Every scraping method returns an ApiResult, a discriminated union you narrow on the type field:
type ApiResult<T> =
  | { type: 'success'; data: T; timeMs: number; creditsCost: number }
  | { type: 'error'; status: number; message: string };
Checking type narrows the rest of the object — in the success branch data, timeMs, and creditsCost are all available and typed; in the error branch you get status and message.
const res = await client.x.getUser({ username: 'jack' });

if (res.type === 'error') {
  // handle and return early
  console.error(`Bridgly error ${res.status}: ${res.message}`);
  return;
}

// res.data is fully typed from here on
console.log(res.data);

Error status codes

The status field carries the HTTP status code. Branch on it rather than parsing the message string.
StatusMeaningWhat to do
400The request failed validation.Check the field values against the endpoint schema.
401Missing, malformed, or revoked API key.Verify the key you passed to new Bridgly(...).
402Out of credits.Top up from the dashboard or with client.billing.buyCredits.
404The requested resource wasn’t found.Confirm the identifier exists and is public.
500The scrape failed unexpectedly.Retry after a short delay.
0Network error — the request never completed.Check connectivity; the message has the detail.