SDK API reference

Functions

All exported functions from @kelpclick/client - signatures, return types, and purpose.

createKelpClient

function createKelpClient(config: KelpClientConfig): KelpClient

Creates the umbrella client composing search and analytics transport. Throws immediately if apiKey, searchUrl, or analyticsUrl are missing.

Returns { search, browse, suggest, trackEvents, identify }.

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

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

Client methods

The object returned by createKelpClient exposes:

MethodSignaturePurpose
search<TDoc>(collection, params: SearchParams, opts?) => Promise<KelpSearchResponse<TDoc>>Full-text search a collection. q is required. Optional semanticWeight opts into semantic ranking when the collection flag is on.
browse<TDoc>(collection, params?: BrowseParams, opts?) => Promise<KelpSearchResponse<TDoc>>Filter/sort a collection without a query (category pages).
suggest<TDoc>(collection, params: SuggestParams, opts?) => Promise<KelpSearchResponse<TDoc>>Autocomplete suggestions. Throws KelpNotImplementedError until the endpoint ships.
trackEvents(events: KelpAnalyticsEvent[], opts?) => Promise<void>Low-level: POST a batch of fully-formed events. The SDK generates the batch id automatically.
identify(traits: IdentifyTraits) => Promise<void>Record a known user server-side. Throws KelpNotImplementedError until the endpoint ships. Use createIdentity().identify() for local identity now.

createKelpSearch

function createKelpSearch(config: KelpClientConfig): KelpSearch

Lightweight search-only client. Use when you need search without analytics. Accepts the same KelpClientConfig but only searchUrl and apiKey are used.

Returns { search, browse, suggest }.

import { createKelpSearch } from '@kelpclick/client/search';

const kelpSearch = createKelpSearch({
  searchUrl: 'https://{TENANT_ID}-search.kelp.click',
  apiKey: 'kelp_pk_...',
});

createAnalytics

function createAnalytics(options: CreateAnalyticsOptions): KelpAnalytics

Creates a buffering analytics tracker. Auto-fills occurredAt, sessionId, and userId on every event. Buffers events and flushes on batch size, interval, and pagehide.

Returns { track, trackSearch, trackProductViewed, trackProductClicked, trackProductWishlisted, trackProductAddedToCart, trackCollectionViewed, trackPurchase, startSession, trackSessionStarted, flush, close, isClosed }. track() accepts a named KelpEventKind only - not a custom string.

import { createKelpClient, createIdentity, createAnalytics } from '@kelpclick/client';

const kelp = createKelpClient({ /* ... */ });
const identity = createIdentity();
const analytics = createAnalytics({ client: kelp, identity });

See CreateAnalyticsOptions in Types.

defaultSerializeFilterBy

function defaultSerializeFilterBy(filters: KelpFilters): string

Serializes KelpFilter[] to the backend filterBy string, e.g. brand:=[globex] && price:>0. Exported for reuse or override when building a custom serializeFilterBy.


createIdentity

function createIdentity(options?: KelpStorage | KelpIdentityOptions): KelpIdentity

Creates an identity: a persistent anonymous id, a rolling session id, and known-user tracking. SSR-safe - falls back to in-memory storage when localStorage is unavailable.

Returns { getAnonymousId, getSessionId, getUserId, identify, reset }.

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

const identity = createIdentity({
  sessionTtlMs: 30 * 60 * 1000, // 30 min
  keyPrefix: 'kelp',
});

identity.getAnonymousId(); // stable per-browser id
identity.getSessionId();   // rolling session id
identity.getUserId();      // string | null
identity.identify({ userId: 'user_123', email: 'a@b.com' });
identity.reset();          // clears known user, keeps anonymous id

createKelpEmail

function createKelpEmail(options: CreateKelpEmailOptions): KelpEmail

Capture-only email. subscribe emits email_subscribed and calls identity.identify({ email }); unsubscribe emits email_unsubscribed.

Returns { subscribe, unsubscribe, flush, close, isClosed }. See Email capture.

import { createKelpEmail } from '@kelpclick/client/email';

const email = createKelpEmail({ identity, tracker: analytics });
email.subscribe({
  email: 'shopper@example.com',
  source: 'footer-form',
  listId: 'newsletter',
  meta: { formId: 'footer-form' },
});

Storage utilities

function memoryStorage(): KelpStorage

Returns a simple in-memory KelpStorage implementation. Useful for SSR or testing.

function localStorageAdapter(): KelpStorage

Returns a localStorage-backed KelpStorage. Browser-only; all methods are wrapped in try/catch so quota or security errors do not propagate.

function resolveStorage(explicit?: KelpStorage): KelpStorage

Returns explicit if provided, localStorageAdapter() in the browser, or memoryStorage() on the server. This is the SSR-safe entry point.


React exports (@kelpclick/client/react)

ExportKindPurpose
KelpProviderComponentWraps the app. Accepts client, optional identity and analytics. All hooks require a KelpProvider ancestor.
useSearchHookDebounced search with pagination, facets, filters, and auto analytics. Returns { query, setQuery, hits, facetCounts, ... }.
useBrowseHookSame return shape as useSearch minus query fields. For category/collection pages.
useKelpClientHookReturns the KelpClient from context.
useKelpIdentityHookReturns the KelpIdentity from context.
useKelpAnalyticsHookReturns the KelpAnalytics tracker from context.
useKelpQueryHookLower-level query runner used internally by useSearch and useBrowse.
useDebouncedValueHookDebounces a value. Used internally by useSearch.

All hooks throw if called outside a <KelpProvider>.

On this page