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
| Field | Required | Default | Notes |
|---|---|---|---|
searchUrl | yes | - | 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. |
analyticsUrl | yes | - | 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). |
apiKey | yes | - | Scoped API key. Sent as X-Api-Key on every request. createKelpClient throws if this is missing. |
tenantCode | no | - | 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. |
timeoutMs | no | 10000 | Per-request timeout in ms. See Transport and retries. |
fetch | no | globalThis.fetch | Inject a custom fetch implementation for SSR or testing. |
serializeFilters | no | { filters } | Override the filter wire shape. Receives KelpFilter[] and must return a Record<string, unknown> that is merged into the request body. |
maxRetries | no | 0 | Retry attempts after the initial try. Default 0 means no retries. See Transport and retries. |
retryBaseMs | no | 250 | Base delay in ms for exponential backoff with full jitter. |
retryMaxMs | no | 5000 | Cap in ms on per-attempt delay. Retry-After headers are also clamped to this value. |
debug | no | false | Emit a debug log line for every attempt. See Observability. |
logger | no | console | Custom log sink { debug, info, warn, error }. Output is emitted only when debug: true. The SDK never logs the API key or full response bodies. |
onRequest | no | - | Called before each request attempt with { url, method, attempt }. |
onResponse | no | - | Called after a response is received (before error-checking) with { url, method, status, attempt }. |
onError | no | - | 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
});Related
- Transport and retries - timeouts, retries, backoff
- Observability - debug mode, logger, lifecycle hooks
- Security and privacy - key scoping, proxy pattern, PII