JavaScript API clients
This is documentation for v4 of the JavaScript API clients, which is not the latest version. To see the documentation for the latest version, see JavaScript v5.
Algolia’s JavaScript API client lets you use Algolia’s APIs from your JavaScript app. Compared to using the APIs directly, using the JavaScript API client has these benefits:
-
Network retry strategy. The API client automatically retries connecting to another Algolia server, if the connection fails. Thanks to that, using the API clients is covered by Algolia’s SLA.
-
Reindexing. The API clients let you reindex your records in a single operation.
-
Automatic batching. When you add records to your index, the API client automatically batches your records to make fewer API requests.
You can use the Algolia JavaScript API client in the browser, and in your backend (Node.js) with the same API.
The JavaScript snippets in the documentation use ES6 syntax. If you want to convert ES6 code to ES5, you can use projects like Babel.js.
For integrating Algolia into your user interface, you should use one of Algolia’s UI libraries instead:
Install the JavaScript API client
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 via npm (recommended)
The recommended method to install the JavaScript API client is via npm.
1
npm install algoliasearch@4
The npm package includes two versions of the API client:
algoliasearch-lite
: a search-only version in a small bundlealgoliasearch
: 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.24.0/dist/algoliasearch-lite.umd.js"
integrity="sha256-b2n6oSgG4C1stMT/yc/ChGszs9EY/Mhs6oltEjQbFCQ="
crossorigin="anonymous"
></script>
<!-- default version -->
<script
src="https://cdn.jsdelivr.net/npm/algoliasearch@4.24.0/dist/algoliasearch.umd.js"
integrity="sha256-xuCFeRamFnnQX5L73AAMtQnz1S1xwihimc5dVgI5Kq8="
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.24.0/dist/algoliasearch-lite.esm.browser.js';
// default version
import algoliasearch from 'https://cdn.jsdelivr.net/npm/algoliasearch@4.24.0/dist/algoliasearch.esm.browser.js';
</script>
jsDelivr is a third-party CDN. Algolia can’t provide support for such third-party services.
Test your installation
If you haven’t already, create an Algolia account and create a new Algolia app to get started.
To test whether you can connect to Algolia, run a simple program that adds a record to a new index, searches the index, and print the results.
-
Copy the following code sample into a text editor. If you’re signed in, the code samples below show your Algolia application ID. If you’re not signed in, replace
YourApplicationID
with your Algolia application ID andYourWriteAPIKey
with your Write API Key. You can find both in your Algolia account.Copy1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// hello_algolia.mjs import algoliasearch from "algoliasearch"; const appID = "YourApplicationID"; const apiKey = "YourWriteAPIKey"; // Connect and authenticate with your Algolia app const client = algoliasearch(appID, apiKey); // Create a new index and add a record const index = client.initIndex("test_index"); const record = { objectID: 1, name: "test_record" } await index.saveObject(record).wait(); // Search the index for "test" and print the results const { hits } = index.search("test"); console.log(JSON.stringify(hits[0]));
-
Save the file as
hello_algolia.mjs
. Go to the directory with the file you just created and run inside a terminal:Copy1
node hello_algolia.mjs
-
If the program ran successfully, you should see:
Copy1 2 3 4 5 6
{ name: 'test_record', objectID: '1', _highlightResult: { ... }
You can inspect your index now in the Algolia dashboard.
Next steps
Now you can interact with the Algolia Search API, you can look at the available methods, for example, for search or indexing.
Other APIs, for example for Algolia Recommend or Analytics, come with their own clients. To get an overview, see Initialize the JavaScript API client.