@neuronsearchlab/nextjs/server and browser helpers from @neuronsearchlab/nextjs/react. The root export contains the server functions too.
Server configuration
SetNSL_CLIENT_ID, NSL_CLIENT_SECRET and NSL_CONTEXT_ID in your server environment. A Vercel integration can provision these; you can also create SDK credentials yourself. Never prefix a secret with NEXT_PUBLIC_.
nsl is a lazy shared client. For explicit configuration, call createNSL(config):
The client caches access tokens, refreshes before expiry and retries once after a
401. It does not expose the general JavaScript SDK’s buffering or retry configuration.
Server methods
Every method is available on both
nsl and the value returned by createNSL.
Several rails
key, contextId, limit, itemId, filters and scope. Keys are unique and match [A-Za-z0-9_.:-]{1,64}. Omitted context IDs use the configured default.
Explicit lists exceeding MAX_RECOMMENDATION_SETS_PER_BATCH (8) are split. An explicit request ID applies to the first chunk only. Send each rail’s returned request_id with its engagement events.
Event Route Handler
Createapp/api/nsl/events/route.ts in your application:
createEventRoute(options) returns a POST handler using the shared nsl client. It requires JSON, enforces a body-size limit (default 16,384 bytes), validates integer IDs and returns 202 after forwarding the event. A body with query and no itemId is routed as a search event. Error responses include 415 for content type, 413 for size and 400 for malformed input.
The optional path field does not mount a route: the file location determines the URL. This helper validates payload shape; add your application’s authentication, consent and rate controls around it. Derive the permitted user identity from your session when accepting signed-in events.
searchEventError(body) is an exported validator returning an error string or null; it does not send an event.
React helpers
These helpers call your application’s endpoints. They do not receive NSL credentials.
An item browser event has
userId, itemId, eventId, and optional contextId, requestId, sessionId. A browser search event has userId, query, optional resultItemIds, eventId, contextId, sessionId.
NSLTracker records mounting, not viewport visibility: use your own visibility logic when measuring actual impressions.
Recommendation component contract
NSLRecommendations<T> requires userId, contextId and a render function children. Optional props are endpoint (default /api/nsl/recommendations), limit (default 10) and fallback (default null). It calls your endpoint with userId, contextId and limit query parameters.
Implement that GET endpoint yourself, validate inputs and call nsl.recommend on the server. Return an item array or an object with data, items or recommendations. The component displays fallback while loading and calls children([]) after a failed request. It does not automatically track clicks or impressions, or pass the response’s request ID into the render callback. For exact attribution, fetch and preserve the full response in your own component.
Errors and exported types
The wrapper throws standardError objects for missing configuration, invalid input, token failures and failed API calls. A batch can also contain individual failed rails. syncContent permits a 409 response through to the caller: inspect the body rather than assuming every resolved call created all items.
Server types are NSLServerConfig, NSLTrackInput, NSLSearchEventInput, NSLSearchInput, NSLRecommendInput, NSLRecommendSetInput, NSLRecommendSetsInput, NSLRecommendationSetResult and NSLRecommendationSetsResponse. Browser types are BrowserEvent and BrowserSearchEvent.
This reference targets Next.js 1.2.0. To use context groups, pass their member context IDs as explicit sets in one batch. Group rules apply automatically. This published client version does not expose a
groupId helper.
