Guides / Getting started / Quick start

Quickstart with the JavaScript API client

Supported platforms

The Algolia JavaScript API client requires Node.js version 8.0 or later. The API client supports all popular browsers, including Internet Explorer 11 and newer. Older browsers require polyfills for: Promise, Object.entries, and Object.assign.

Install

The recommended method to install the JavaScript API client is via npm.

1
npm install algoliasearch

The npm package includes two versions of the API client:

  • algoliasearch-lite: a search-only version in a small bundle
  • algoliasearch: the default version for all operations, including indexing, search, and personalization

Both versions come as UMD and ESM modules.

Include from a content delivery network

For quick prototyping or exploration, you can also include the algoliasearch package in a <script> tag from a content delivery network.

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- search only version -->
<script
  src="https://cdn.jsdelivr.net/npm/algoliasearch@4.23.1/dist/algoliasearch-lite.umd.js"
  integrity="sha256-INx5X7QaA9vuDU9QHlk88MRP8tDLpeuTdBcNe4RYl58="
  crossorigin="anonymous"
></script>

<!-- default version -->
<script
  src="https://cdn.jsdelivr.net/npm/algoliasearch@4.23.1/dist/algoliasearch.umd.js"
  integrity="sha256-11cb3XL5KyiJM42noyXTttZe135v1CPm2EkJklQ4GHo="
  crossorigin="anonymous"
></script>

If you’re using JavaScript (ES6) modules, use these packages:

1
2
3
4
5
6
7
<script type="module">
// search only version
import algoliasearch from 'https://cdn.jsdelivr.net/npm/algoliasearch@4.23.1/dist/algoliasearch-lite.esm.browser.js';

// default version
import algoliasearch from 'https://cdn.jsdelivr.net/npm/algoliasearch@4.23.1/dist/algoliasearch.esm.browser.js';
</script>

jsDelivr is a third-party CDN. Algolia can’t provide support for such third-party services.

Quickstart sample app

To download and run a self-contained example, see the JavaScript quickstart on GitHub.

Initialize the client

To start, you need to initialize the client. To do this, you need your Application ID and API Key. You can find both on your Algolia account.

1
2
3
4
5
6
7
8
// Default version (all methods)
import algoliasearch from 'algoliasearch';

// Search-only version
// import algoliasearch from 'algoliasearch/lite';

const client = algoliasearch('YourApplicationID', 'YourWriteAPIKey');
const index = client.initIndex('your_index_name');

The API key displayed here is your Admin API key. To maintain security, never use your Admin API key on your frontend, nor share it with anyone. In your frontend, only use the search-only API key or any other key that has search-only rights.

Push data

Without any prior configuration, you can start indexing 500 contacts in the contacts index using the following code:

1
2
3
4
5
6
7
8
const index = client.initIndex('contacts');
const contactsJSON = require('./contacts.json');

index.saveObjects(contactsJSON, {
  autoGenerateObjectIDIfNotExist: true
}).then(({ objectIDs }) => {
  console.log(objectIDs);
});

Configure

You can customize settings to fine-tune the search behavior. For example, you can add a custom ranking by number of followers to further enhance the built-in relevance:

1
2
3
4
5
index.setSettings({
  'customRanking': ['desc(followers)']
}).then(() => {
  // done
});

You can also configure the list of attributes you want to index by order of importance (most important first).

Algolia suggests results as you type, which means you’ll generally search by prefix. In this case, the order of attributes is crucial in deciding which hit is the best.

1
2
3
4
5
6
7
8
9
10
11
12
index.setSettings({
  searchableAttributes: [
    'lastname',
    'firstname',
    'company',
    'email',
    'city',
    'address'
  ]
}).then(() => {
  // done
});

Once configured, you can search for contacts by attributes such as firstname, lastname, or company (even with typos):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Search for a first name
index.search('jimmie').then(({ hits }) => {
  console.log(hits);
});

// Search for a first name with typo
index.search('jimie').then(({ hits }) => {
  console.log(hits);
});

// Search for a company
index.search('california paint').then(({ hits }) => {
  console.log(hits);
});

// Search for a first name and a company
index.search('jimmie paint').then(({ hits }) => {
  console.log(hits);
});

Search UI

Use one of Algolia’s frontend libraries to build your search UI:

However, if you don’t want to use the libraries, you can build a custom UI:

  1. Design UI components with your preferred frontend system: a search box, results display, filters, and any other desired Algolia features
  2. Send search requests with the API client
  3. Parse the JSON response from the search request and display the results in your custom UI.
Did you find this page helpful?