Skip to main content
The Autocomplete module provides methods to retrieve autocomplete suggestions as users type their search queries.

Getting Started

The autocomplete module is available on your Constructor.io client instance:
const ConstructorioClient = require('@constructor-io/constructorio-client-javascript');

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

// Access the autocomplete module
constructorio.autocomplete.getAutocompleteResults('t-sh');

Methods

getAutocompleteResults

Retrieve autocomplete suggestions based on a partial query.
constructorio.autocomplete.getAutocompleteResults('t-sh')
  .then((response) => {
    console.log(response.sections);
  });

Parameters

query
string
required
The partial search term to get autocomplete suggestions for
parameters
object
Additional parameters to refine the result set
networkParameters
object
Parameters relevant to the network request

Response Structure

Autocomplete results are organized into sections:
{
  "sections": {
    "Search Suggestions": [
      {
        "value": "t-shirts for men",
        "data": {
          "total_num_results": 245
        },
        "result_id": "abc123-xyz789"
      },
      {
        "value": "t-shirts women",
        "data": {
          "total_num_results": 189
        },
        "result_id": "abc123-xyz789"
      }
    ],
    "Products": [
      {
        "matched_terms": ["t-shirt"],
        "data": {
          "id": "shirt-123",
          "title": "Blue T-Shirt",
          "url": "/products/shirt-123"
        },
        "result_id": "abc123-xyz789"
      }
    ]
  },
  "result_id": "abc123-xyz789",
  "request": {...}
}
Each item in every section includes a result_id field that should be used when tracking user interactions.

Common Use Cases

1

As-You-Type Suggestions

Call getAutocompleteResults on each keystroke with a debounce:
let debounceTimer;

function handleSearchInput(event) {
  clearTimeout(debounceTimer);
  
  debounceTimer = setTimeout(() => {
    const query = event.target.value;
    
    if (query.length >= 2) {
      constructorio.autocomplete.getAutocompleteResults(query)
        .then(displaySuggestions);
    }
  }, 300);
}
2

Display Suggestions by Section

Render different sections with appropriate styling:
function displaySuggestions(response) {
  const { sections } = response;
  
  // Display search suggestions
  if (sections['Search Suggestions']) {
    renderSearchSuggestions(sections['Search Suggestions']);
  }
  
  // Display product suggestions
  if (sections.Products) {
    renderProductSuggestions(sections.Products);
  }
}
3

Track User Selections

Track when users select a suggestion:
function handleSuggestionClick(suggestion) {
  // Navigate to the suggestion
  window.location.href = suggestion.data.url;
  
  // Track the selection using the tracker module
  constructorio.tracker.trackAutocompleteSelect(
    suggestion.value,
    { result_id: suggestion.result_id }
  );
}

Events

The autocomplete module dispatches custom events on the browser window when requests complete:
window.addEventListener('cio.client.autocomplete.getAutocompleteResults.completed', (event) => {
  // event.detail contains the response data
  console.log(event.detail);
}, false);

Query Handling

Queries starting with / are automatically converted to | for backend API compatibility.

Performance Tips

  • Implement debouncing (300ms recommended) to reduce API calls
  • Set a minimum query length (2-3 characters) before making requests
  • Use resultsPerSection to limit the number of results and improve response times
  • Cache responses for identical queries within a session

Build docs developers (and LLMs) love