Skip to main content
The Swift SDK uses Swift concurrency and URLSession. It supports Swift 5.9+, iOS 15+, macOS 12+, tvOS 15+ and watchOS 8+.

Install

In Xcode, choose File → Add Package Dependencies and enter:
Select the NeuronSearchLabSDK product, then import NeuronSearchLab. With Swift Package Manager:

Authentication and configuration

The SDK accepts a static bearer token; it does not exchange client credentials or refresh tokens automatically. Keep workspace secrets on your server. For a distributed mobile application, prefer your own backend endpoints rather than giving every installation a workspace-wide token. Use the SDK directly only where the supplied token’s access is appropriate for that runtime.
token must come from your trusted application’s authentication setup; never hard-code it in source.
getRecommendations(_:) accepts RecommendationOptions: userId, optional contextId, scope, limit, startingAfter. It returns RecommendationsResponse. getAutoRecommendations(_:) takes AutoRecommendationsOptions with userId, optional contextId, scope, limit, cursor, windowDays, candidateLimit, servedCap. Read the section and nextCursor, and stop when done is true or no cursor is returned. search(_:) takes SearchOptions. Besides query, it supports userId, contextId, limit, filters, scope, resultItemIds, queryRetrievalEnabled, fusionMethod, semanticWeight, keywordWeight, keywordFields, requestId, debug, explain, includeSuppressed. SearchFilters represents string, string-list or structured filters. See the search contract.

Several rails

getRecommendationSets(_:) accepts RecommendationSetsOptions, including userId or anonymousId, sessionId, requestId and a non-empty sets array. Each RecommendationSetRequest supports key, contextId, limit, itemId, cartItemIds, pageType, filters, scope, query. Explicit lists larger than eight sets are split. An explicit request ID applies to the first chunk only; later chunks use separate IDs. Check each RecommendationSetResult.status and error, then preserve its requestId for events. The response also provides ordered results and usage.

Events

trackEvent(_:) accepts TrackEventPayload and returns JSONValue after its buffered send completes. Required item-event fields are userId, integer eventId and integer itemId; optional fields include contextId, occurredAt (Unix seconds), requestId, sessionId, metadata, query, resultItemIds and additionalFields. trackSearch(userId:query:resultItemIds:eventId:contextId:sessionId:) records a search with no item. Only userId and a non-empty query are required. createEvent(_:) is a compatibility alias for trackEvent. flushEvents() asynchronously attempts to send buffered events and does not throw. Observe each tracking task for success or failure. Flush while your process still has execution time; an app being suspended or terminated cannot guarantee delivery. For concurrent users or multiple rails, send explicit request and session IDs instead of relying on instance state.

Items

NSL generates integer IDs. Store the returned ID for subsequent updates and events. The catalogue’s metadata.source_item_id supports stable source mappings; see Catalogue.

Client state

Errors, JSON and transport

Catch SDKClientError for validation/configuration, SDKHTTPError for HTTP status and response details, and SDKTimeoutError for timeout failures. Network and decoding failures can also propagate. A token rejected with 401 needs replacement through your authentication flow. NeuronID accepts a string or integer for application-owned user IDs. Item, context and event-type fields remain integers. JSONValue represents objects, arrays, strings, numbers, booleans and null; JSONObject is [String: JSONValue]. Use typed accessors such as intValue when reading a raw response. Codable types implement normal init(from:) and encode(to:) round trips. HTTPDataLoading.data(for:) is the transport protocol for custom URLSession adapters and tests. Supply it as SDKConfig.urlSession.

Logging

configureLogger(_:) accepts LoggerConfiguration; logger and SDKLogger.shared expose the shared logger. Settings include level, enableNetworkBodyLogging, enablePerformanceLogging, transport and redactKeys. Levels are .trace, .debug, .info, .warn, .error, .fatal; normal logging defaults to .info with payload/performance logging off. Logger methods are configure, trace, debug, info, warn, error, fatal, shouldLog, canLogNetworkPayloads and isPerformanceLoggingEnabled. Logging methods accept a message and optional JSONObject context. A LoggerTransport receives StructuredLogEntry. Keep user data out of normal logs.

Response models

RecommendationResource, RecommendationsResponse (also aliased as SearchResponse), RecommendationSetResult, RecommendationSetsResponse, PatchItemResponse and DeleteItemsResponse model the result types. Swift properties use camel case, such as requestId; JSON wire fields use snake case. Read the Core API reference for resource semantics.
This reference targets Swift 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.