Configuration

Client configuration

All KelpClientConfig options, required fields, headers, and setup examples.

createKelpClient(config) is the main entry point. It composes the search capability and events transport into a single client, and throws immediately if a required field is missing.

import { createKelpClient } from '@kelpclick/client';

const kelp = createKelpClient({
  searchUrl: 'https://{TENANT_ID}-search.kelp.click',
  analyticsUrl: 'https://{TENANT_ID}-events.kelp.click',
  apiKey: process.env.VITE_KELP_API_KEY!,
});

Every request automatically sends an X-Kelp-Client: @kelpclick/client/<version> header so the server can identify the SDK version in logs. The API key is sent as X-Api-Key on every request.

Options reference

FieldRequiredDefaultNotes
searchUrlyes-Kelp search base URL, e.g. https://{TENANT_ID}-search.kelp.click. The SDK appends /api/v1/collections/{collection}/search or /browse. Trailing slashes are trimmed. Pass '' to use a same-origin proxy.
analyticsUrlyes-Analytics base URL, e.g. https://{TENANT_ID}-events.kelp.click. The SDK appends /analytics/events when posting batches. Not inferred from searchUrl - search and analytics run on separate hosts. Pass '' for a same-origin proxy (e.g. in Hydrogen).
apiKeyyes-Scoped API key. Sent as X-Api-Key on every request. createKelpClient throws if this is missing.
tenantCodeno-Tenant slug. Included on every analytics batch envelope as tenantCode (alongside the batch id). Useful for multi-tenant deployments where events from different storefronts flow to a shared ingester.
timeoutMsno10000Per-request timeout in ms. See Transport and retries.
fetchnoglobalThis.fetchInject a custom fetch implementation for SSR or testing.
serializeFiltersno{ filters }Override the filter wire shape. Receives KelpFilter[] and must return a Record<string, unknown> that is merged into the request body.
maxRetriesno0Retry attempts after the initial try. Default 0 means no retries. See Transport and retries.
retryBaseMsno250Base delay in ms for exponential backoff with full jitter.
retryMaxMsno5000Cap in ms on per-attempt delay. Retry-After headers are also clamped to this value.
debugnofalseEmit a debug log line for every attempt. See Observability.
loggernoconsoleCustom log sink { debug, info, warn, error }. Output is emitted only when debug: true. The SDK never logs the API key or full response bodies.
onRequestno-Called before each request attempt with { url, method, attempt }.
onResponseno-Called after a response is received (before error-checking) with { url, method, status, attempt }.
onErrorno-Called when a request ultimately fails (after all retries) with the thrown error.

Full example

const kelp = createKelpClient({
  // required
  searchUrl: 'https://{TENANT_ID}-search.kelp.click',
  analyticsUrl: 'https://{TENANT_ID}-events.kelp.click',
  apiKey: 'kelp_...',

  // tenant + transport
  tenantCode: 'acme',
  timeoutMs: 10_000,
  fetch: myFetch,
  serializeFilters: (f) => ({ filters: f }),

  // resilience
  maxRetries: 2,
  retryBaseMs: 250,
  retryMaxMs: 5000,

  // observability
  debug: true,
  logger: console,

  // lifecycle hooks
  onRequest(info)  { /* { url, method, attempt } */ },
  onResponse(info) { /* { url, method, status, attempt } */ },
  onError(err)     { /* called after all retries exhausted */ },
});

Never hard-code your API key. Load it from an environment variable (VITE_KELP_API_KEY, PUBLIC_KELP_API_KEY, etc.) and never commit it to source control.

Same-origin proxy

In production you may want to avoid exposing your Kelp host directly to the browser. Set searchUrl and/or analyticsUrl to an empty string (or a path like /api/kelp) and proxy the requests server-side. The SDK will post relative URLs that your server can forward with the key attached.

const kelp = createKelpClient({
  searchUrl: '',          // requests go to /api/v1/collections/...
  analyticsUrl: '',       // requests go to /analytics/events
  apiKey: '',             // key attached by the proxy; pass empty string to satisfy the type
});

On this page