Quickstart

Quickstart: Write a typed client in TypeScript

A handful of interfaces gives you end-to-end type safety over the public API — products, quotes and the shared error envelope. This recipe shows the exact code, uses only the unauthenticated public ring, and links out to the endpoints and the application flow so the reader always has a next step.

2 min read

interfacesModel every response
discriminatedUnion on product type
never anyType the envelope

Model the responses

interface Product {
  id: string;
  name: string;
  type: 'flex' | 'onetime';
  min_amount: number;
  max_amount: number;
  representative_apr: number;
}
interface ApiError {
  error: { type: string; code: string; message: string; request_id: string };
}
async function listProducts(base: string): Promise<Product[]> {
  const res = await fetch(`${base}/products`);
  const body = await res.json();
  if (!res.ok) throw new Error((body as ApiError).error.code);
  return (body as { data: Product[] }).data;
}

Generate types from the schema

If you consume many endpoints, generate types from the published OpenAPI description rather than hand-writing them, so your client stays in lockstep with the API contract.

Frequently asked questions

Hand-write or generate types?

Hand-write for one or two endpoints; generate from the OpenAPI schema once you use several, so types never drift from the contract.

How do I type the error path?

Model the envelope as an ApiError interface and narrow on it in your error handler, so error.code is fully typed.

Funding for UK limited companies

Credicorp lends to your company, not to you personally — short-term working capital with no personal guarantee. See what your business could access.