Skip to main content
This guide will help you create a working search interface in just a few minutes. We’ll build a simple product search with a search box, results display, and filtering.
Make sure you’ve installed InstantSearch before following this guide.

Choose Your Framework

Select the framework you’re using:

JavaScript Quick Start

Build a search UI with vanilla JavaScript.
1

Import the required packages

Create your JavaScript file and import InstantSearch:
import { liteClient as algoliasearch } from 'algoliasearch/lite';
import instantsearch from 'instantsearch.js';
import { searchBox, hits, refinementList, pagination, configure } from 'instantsearch.js/es/widgets';
import 'instantsearch.css/themes/satellite.css';
We’re using algoliasearch/lite which is a lighter version of the client optimized for search-only operations.
2

Initialize the search client

Create an Algolia search client with your credentials:
const searchClient = algoliasearch(
  'YourApplicationID',
  'YourSearchOnlyAPIKey'
);
Replace YourApplicationID and YourSearchOnlyAPIKey with your actual Algolia credentials. Never use your Admin API Key in client-side code!
3

Create the InstantSearch instance

Initialize InstantSearch with your index name:
const search = instantsearch({
  indexName: 'your_index_name',
  searchClient,
  insights: true,
});
4

Add widgets to your search

Add UI widgets to display the search interface:
search.addWidgets([
  // Search box
  searchBox({
    container: '#searchbox',
    placeholder: 'Search for products...',
  }),

  // Search results
  hits({
    container: '#hits',
    templates: {
      item: (hit, { html, components }) => html`
        <article>
          <h2>${components.Highlight({ hit, attribute: 'name' })}</h2>
          <p>${components.Highlight({ hit, attribute: 'description' })}</p>
        </article>
      `,
    },
  }),

  // Configure search parameters
  configure({
    hitsPerPage: 12,
  }),

  // Category filter
  refinementList({
    container: '#categories',
    attribute: 'categories',
  }),

  // Pagination
  pagination({
    container: '#pagination',
  }),
]);
5

Start the search

Call start() to render the search UI:
search.start();
6

Add HTML containers

Create the HTML structure with container elements:
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>InstantSearch.js App</title>
</head>
<body>
  <header>
    <h1>Search Products</h1>
    <div id="searchbox"></div>
  </header>

  <main>
    <aside>
      <h3>Categories</h3>
      <div id="categories"></div>
    </aside>

    <div>
      <div id="hits"></div>
      <div id="pagination"></div>
    </div>
  </main>

  <script type="module" src="/src/app.js"></script>
</body>
</html>

Complete Example

Here’s the complete JavaScript code:
import { liteClient as algoliasearch } from 'algoliasearch/lite';
import instantsearch from 'instantsearch.js';
import { 
  searchBox, 
  hits, 
  refinementList, 
  pagination, 
  configure 
} from 'instantsearch.js/es/widgets';
import 'instantsearch.css/themes/satellite.css';

const searchClient = algoliasearch(
  'YourApplicationID',
  'YourSearchOnlyAPIKey'
);

const search = instantsearch({
  indexName: 'your_index_name',
  searchClient,
  insights: true,
});

search.addWidgets([
  searchBox({
    container: '#searchbox',
    placeholder: 'Search for products...',
  }),

  hits({
    container: '#hits',
    templates: {
      item: (hit, { html, components }) => html`
        <article>
          <h2>${components.Highlight({ hit, attribute: 'name' })}</h2>
          <p>${components.Highlight({ hit, attribute: 'description' })}</p>
        </article>
      `,
    },
  }),

  configure({
    hitsPerPage: 12,
  }),

  refinementList({
    container: '#categories',
    attribute: 'categories',
  }),

  pagination({
    container: '#pagination',
  }),
]);

search.start();
Once you’ve implemented the code:
1

Start your development server

Run your development server to see the search UI:
npm run dev
# or
yarn dev
# or
pnpm dev
2

Verify the search works

Open your browser and try:
  • Typing in the search box - results should update instantly
  • Clicking category filters - results should filter accordingly
  • Using pagination - navigate through result pages
3

Check the browser console

Open your browser’s developer console to check for any errors or warnings.

What’s Happening?

Let’s break down the key parts:
  1. Search Client - Connects to your Algolia application using the lite client (optimized for search)
  2. InstantSearch Instance - The main container that manages the search state
  3. Widgets/Components - UI elements that render and interact with the search:
    • SearchBox - Input field for search queries
    • Hits - Displays search results
    • RefinementList - Faceted filter for attributes
    • Pagination - Navigate through pages
    • Configure - Set search parameters
  4. Highlight - Highlights matching terms in results

Customization

Now that you have a basic search working, you can customize it:

Add More Widgets

Explore the widget library to add features like:
  • Range Sliders - Filter by price or numeric ranges
  • Sort By - Let users change result ordering
  • Current Refinements - Show active filters with clear buttons
  • Stats - Display result count and search time
  • Clear Refinements - Reset all filters at once
Customize the appearance:
  • Override CSS classes (all widgets use BEM naming)
  • Create custom templates for results
  • Use your own CSS framework
  • Build custom widgets

Configure Search Behavior

Adjust how search works:
  • Set ranking rules
  • Configure typo tolerance
  • Add custom ranking
  • Set searchable attributes

Next Steps

Now that you have a working search, explore these topics:

Widget Showcase

Browse all available widgets and their options

Customization Guide

Learn how to style and customize your search UI

Server-Side Rendering

Implement SSR for better SEO and performance

Routing

Sync search state with the URL for shareable links
Using the demo API keys? They’re limited to 10,000 records and read-only access. For production apps, create your own Algolia account and use your own credentials.

Troubleshooting

Search Results Not Appearing

  • Verify your Application ID and API Key are correct
  • Check that your index name matches exactly
  • Ensure your index has data
  • Check the browser console for errors

Widgets Not Rendering

  • Verify HTML containers exist (JavaScript version)
  • Check that you called search.start() (JavaScript version)
  • Ensure the InstantSearch component wraps all widgets (React/Vue)

TypeScript Errors

  • Install the correct type packages for your Algolia client version
  • Import the Hit type from instantsearch.js
  • Type your hit data properly

Need Help?

Check out these resources:

Build docs developers (and LLMs) love