// KnowFirm TypeScript SDK — STARTER (thin fetch client generated from openapi/openapi.yaml). // Finalize/replace via the `api-sdk-client-generation` skill once the foundation app is live // (typed models, retries, pagination). Kept minimal and dependency-free for now. // // const kf = new KnowFirm({ apiKey: process.env.KNOWFIRM_KEY! }); // const biz = await kf.business({ name: "bank of america" }); export interface KnowFirmOptions { apiKey: string; baseUrl?: string; fetchImpl?: typeof fetch } export interface BusinessQuery { ein?: string; lei?: string; cik?: string; cert?: string; ticker?: string; name?: string } export class KnowFirmError extends Error { constructor(public status: number, public body: unknown) { super(`KnowFirm API error ${status}`); } } export class KnowFirm { private apiKey: string; private baseUrl: string; private fetchImpl: typeof fetch; constructor(opts: KnowFirmOptions) { this.apiKey = opts.apiKey; this.baseUrl = (opts.baseUrl ?? "https://api.knowfirm.com").replace(/\/$/, ""); this.fetchImpl = opts.fetchImpl ?? fetch; } private async request(method: string, path: string, opts: { query?: Record; body?: unknown } = {}): Promise { const url = new URL(this.baseUrl + path); for (const [k, v] of Object.entries(opts.query ?? {})) if (v !== undefined) url.searchParams.set(k, String(v)); const res = await this.fetchImpl(url.toString(), { method, headers: { "X-API-Key": this.apiKey, ...(opts.body ? { "Content-Type": "application/json" } : {}) }, body: opts.body ? JSON.stringify(opts.body) : undefined, }); const data = await res.json().catch(() => ({})); if (!res.ok) throw new KnowFirmError(res.status, data); return data as T; } health() { return this.request("GET", "/v1/health"); } /** Resolve one business by ein | lei | cik | cert | ticker | name. Returns null on 404. */ async business(q: BusinessQuery) { try { return await this.request("GET", "/v1/business", { query: q as Record }); } catch (e) { if (e instanceof KnowFirmError && e.status === 404) return null; throw e; } } search(q: string, limit = 10) { return this.request("GET", "/v1/business/search", { query: { q, limit } }); } screening(input: { name: string; country?: string; address?: string }) { return this.request("POST", "/v1/screening", { body: input }); } enrich(items: BusinessQuery[]) { return this.request("POST", "/v1/enrich", { body: { items } }); } }