Skip to content

createClient

createClient creates a client that starts, resumes and recommends questionnaires.

const client = createClient(options)
import { apiKey, createClient } from '@partner-sdk/core';
const client = createClient({
baseUrl: 'https://api.example.com',
auth: apiKey(key),
});
  • baseUrl: the API origin (origin only, no path). Links the server returns on other origins are rebased onto it.
  • auth (optional): an auth hook. apiKey(key, header = "x-api-key") sends a key header. Auth is only sent to baseUrl’s origin.
  • fetch (optional): a fetch implementation. Defaults to the global.
  • timeoutMs (optional): per-attempt timeout. Default 30000.
  • retry (optional): { retries: 2, baseMs: 250, capMs: 5000, maxRetryAfterMs: 30000 } by default, or false. Only GET, PUT and PATCH are retried, on network errors, 429, 502, 503 and 504.
  • hooks (optional): onRequest, onResponse, onRetry, onRebase. Hooks never receive bodies.
  • transport (optional): your own transport from createTransport, in place of the options above.
  • startPath (optional): default /api/v0/application.
  • recommendPath (optional): default /api/v0/product_recommendation.

A Client.


Creates a quoting questionnaire for a product.

const q = await client.start('cyber');
  • vertical: a product id, such as "cyber" or "usa". Your editor suggests known ids; the server decides what’s valid.
  • options.discover (optional): read the API root first and reject unknown verticals before any POST.
  • options.signal (optional): an AbortSignal.

Returns a Promise<Questionnaire>. See Questionnaire.


Re-opens a questionnaire, for example after a page reload.

const q = await client.resume(storedId);

Returns a Promise<Questionnaire> with the outstanding questions only. The API never returns stored answers; you keep those.

Caveats: an unknown or expired id rejects with an ApiError (401, 403, 404 or 410).


Creates a product recommendation questionnaire. See Recommend a product.

Returns a Promise<Questionnaire>.


Lists the products the API offers, from the API root. Cached per client; pass refresh: true to re-fetch.

Returns a Promise<Vertical[]>, each { id, name, start(options?) }.


client.searchTrade(template, text, options?)

Section titled “client.searchTrade(template, text, options?)”

Searches trades with a question’s search template, through the client’s transport. Use this when your UI holds the template itself; otherwise use q.searchTrade(text) or session.searchTrade(name, text).

Returns Promise<{ results: TradeResult[]; searchId }>.

const verticals = await client.listVerticals();
const cyber = verticals.find((v) => v.id === 'cyber');
const q = await cyber?.start();
const client = createClient({ baseUrl, fetch: myInstrumentedFetch });
const client = createClient({
baseUrl,
hooks: {
onResponse: ({ method, status, durationMs, requestId }) =>
metrics.record(method, status, durationMs, requestId),
},
});

Deployed environments sit behind an API Gateway that needs x-api-key. Pass auth: apiKey(key). Local servers and the mock API need no key.

My browser blocks requests with a CORS error

Section titled “My browser blocks requests with a CORS error”

The V0 API doesn’t send CORS headers yet. For local work, run the mock with ALLOWED_ORIGINS=http://localhost:4321 bun start.