The generated API clients are a work in progress, you can also find our stable clients on the Algolia documentation.

Skip to main content

Send data to Algolia

Algolia doesn’t search directly into your own data source. For data to be searchable, you need to send it to Algolia’s servers.

This happens right after retrieving your data from your data source and reformatting it. Once your data is ready, you can push it to Algolia using the batch method.

Required credentials

To push data to Algolia, you need an Application ID and a valid API key with the right access level. You can find them and create new ones in the API keys page.

Setting up the API client

Make sure to also read the installation page.

// for the default version
import { algoliasearch } from 'algoliasearch';

// you can also import the lite version, with search only methods
// import { liteClient } from 'algoliasearch/lite';

const client = algoliasearch('<YOUR_APP_ID>', '<YOUR_API_KEY>');

Sending your data

Before sending anything to Algolia, you need to retrieve your data. You can do this in several ways, in our case we will pick it from the source code directly.

// The records retrieved by any of your data sources
const recordsFromDataSource = [
{ name: 'Tom Cruise' },
{ name: 'Scarlett Johansson' },
];

// Here we construct the request to be sent to Algolia with the `batch` method
const requests: BatchRequest[] = recordsFromDataSource.map((record) => {
return {
// `batch` allows you to do many Algolia operations, but here we want to index our record.
action: 'addObject',
body: record,
};
});

const { taskID } = await client.batch({
indexName: '<YOUR_INDEX_NAME>',
batchWriteParams: {
requests,
},
});

// Wait for indexing to be finished
await client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID });

console.log('Ready to search!');