Skip to main content
The SDK is a lightweight TypeScript client with built-in event batching, automatic retries, and structured logging for the NeuronSearchLab API.Need PHP instead? View the PHP SDK guide or install it from Packagist.

Installation

The SDK ships with TypeScript declarations - no additional type packages needed.

Initialize the client

The SDK uses a Bearer token for authentication. Obtain an access token by exchanging your SDK credentials and pass it when creating the client:

Configuration options


Get recommendations

Each recommendation includes id, object, item_id, rank, score, and an embedded item object with the metadata you stored. The response also includes a request_id for event attribution. Use search when the user supplies a free-text query and you want ranked catalog items back through the Core API data plane:
The SDK posts to https://api.neuronsearchlab.com/v1/search, not the console Platform API. Search responses use the same item shape as recommendations and include query, request_id, data, and recommendations.

Search-to-click loop

For search-backed surfaces, treat search and events as one attribution loop:
  1. Run search with the user’s query and optional context.
  2. Render results from data or recommendations.
  3. Capture request_id from the search response.
  4. Send that requestId back with the click, view, or purchase event.
This keeps attribution tied to the exact result set the user saw.

Auto-generated sections

Use getAutoRecommendations for paginated, auto-titled recommendation sections - ideal for infinite-scroll feeds:

Track events

Events provide the feedback loop that powers personalization. The SDK buffers events in memory and flushes them in batches for efficiency.

How batching works

1

Buffer events

Events are buffered with a timestamp that maps to occurred_at in the API.
2

Flush on window or batch size

The buffer flushes when the collate window elapses (default 3s) or the buffer reaches maxBatchSize.
3

Flush during page unload

On page unload (beforeunload, pagehide), remaining events are flushed automatically.
4

Retry failed sends

Failed sends are retried with exponential backoff.
You can flush manually at any time:

Request ID propagation

When propagateRecommendationRequestId is enabled (the default), the SDK captures the request_id from getRecommendations() or search() and automatically attaches it to subsequent trackEvent() calls. This links events back to the result set that produced them.

Manage catalog items

Create an item

The description field is used to generate embeddings for similarity matching. NSL generates item.id; persist that integer alongside your source record.

Update an item

Or use the convenience helper:

Delete items


Error handling

The SDK exports two error classes:
Transient errors (429, 5xx, timeouts) are retried automatically with exponential backoff up to maxRetries times.

Logging

Configure structured logging for debugging and observability:
Available log levels: TRACE, DEBUG, INFO (default), WARN, ERROR, FATAL. You can also provide a custom transport to send logs to your own logging infrastructure:

Session management

The SDK auto-generates a session UUID on initialization (when autoSessionId is true). All events include the session ID automatically. You can override or read it:

Next steps