Skip to main content
The ConstructorIO class is the main entry point for the Constructor.io JavaScript Client SDK. It provides access to all SDK modules including search, browse, autocomplete, recommendations, tracking, quizzes, and agent functionality.

Constructor

Creates a new instance of the ConstructorIO client.
import ConstructorIO from '@constructor-io/constructorio-client-javascript';

const constructorio = new ConstructorIO({
  apiKey: 'YOUR_API_KEY',
  userId: 'user-123',
  segments: ['desktop', 'premium'],
  testCells: {
    'test-1': 'variant-a',
    'test-2': 'variant-b'
  }
});

Parameters

options
object
required
Configuration options for the client

Returns

A ConstructorIO instance with the following module properties:
  • search - Interface to the Search module
  • browse - Interface to the Browse module
  • autocomplete - Interface to the Autocomplete module
  • recommendations - Interface to the Recommendations module
  • tracker - Interface to the Tracker module
  • quizzes - Interface to the Quizzes module
  • agent - Interface to the AI Shopping Agent module
  • assistant - Interface to the Assistant module (deprecated, use agent instead)

Example

import ConstructorIO from '@constructor-io/constructorio-client-javascript';

// Basic initialization
const constructorio = new ConstructorIO({
  apiKey: 'YOUR_API_KEY'
});

// Advanced initialization with custom options
const constructorio = new ConstructorIO({
  apiKey: 'YOUR_API_KEY',
  userId: 'user-123',
  segments: ['premium', 'mobile'],
  testCells: {
    'homepage-redesign': 'variant-b',
    'checkout-flow': 'control'
  },
  sendTrackingEvents: true,
  trackingSendDelay: 500,
  networkParameters: {
    timeout: 5000
  },
  eventDispatcher: {
    enabled: true,
    waitForBeacon: true
  }
});

// Use the client modules
constructorio.search.getSearchResults('red shoes');
constructorio.autocomplete.getAutocompleteResults('shoe');

DOM-less Environment Example

import ConstructorIO from '@constructor-io/constructorio-client-javascript';

// In Node.js or other DOM-less environments, clientId and sessionId are required
const constructorio = new ConstructorIO({
  apiKey: 'YOUR_API_KEY',
  clientId: 'client-abc123',
  sessionId: 12345678,
  userId: 'user-456',
  fetch: customFetchImplementation // Optional custom fetch
});

setClientOptions

Updates the client options after initialization. This method allows you to modify certain client settings without creating a new instance.
constructorio.setClientOptions({
  userId: 'new-user-id',
  segments: ['vip', 'desktop'],
  sendTrackingEvents: true
});

Parameters

options
object
required
Client options to update

Returns

void

Example

import ConstructorIO from '@constructor-io/constructorio-client-javascript';

const constructorio = new ConstructorIO({
  apiKey: 'YOUR_API_KEY'
});

// Update user information after login
constructorio.setClientOptions({
  userId: 'authenticated-user-123',
  segments: ['premium', 'logged-in']
});

// Update test cells based on experiment assignment
constructorio.setClientOptions({
  testCells: {
    'new-feature-test': 'variant-a'
  }
});

// Enable tracking events
constructorio.setClientOptions({
  sendTrackingEvents: true
});

// Clear user ID on logout
constructorio.setClientOptions({
  userId: null
});

// Update session ID in DOM-less environment
constructorio.setClientOptions({
  sessionId: 98765432
});

Notes

  • Only the specified options will be updated; other options remain unchanged
  • The sessionId parameter will only be updated in DOM-less environments
  • To clear the userId, pass null or undefined as the value
  • The sendTrackingEvents option also updates the tracker module’s configuration

Build docs developers (and LLMs) love