Skip to main content
Algolia doesn’t search your data source directly. You send the parts of your data that matter for search to Algolia, which stores them as in an . This guide shows how to send records from your code with an Algolia API client. For other ways to send data, see Send and update your data.

Before you begin

You need:
  • The Algolia API client for your language, installed in a project. C#, Dart, Java, Kotlin, Scala, and Swift also need a build file to run the script. Start from these files:
  • An Algolia application ID and an API key with permission to write to your indices. Find both in the API Keys section of the Algolia dashboard.
    • If your application has a Write API key, use it. It can create, update, and delete indices.
    • If it doesn’t, create an API key with these permissions: addObject to add and update records, editSettings to change index settings, and search to verify the result.
    Don’t use the Admin API key for indexing, and don’t put write keys in source code. This guide reads them from environment variables.
  • Your application ID and API key set as environment variables. In Bash or Zsh, run these commands and replace ALGOLIA_APPLICATION_ID and ALGOLIA_API_KEY with your credentials:
    The variables last for the current shell session. Run the script in the same session.

Index sample records

The following script indexes a sample dataset so you can see the whole process before you adapt it to your own data. It fetches the dataset, checks that every record has an objectID, configures the index, sends the records in batches, and verifies the result with a lookup and a search. The dataset has 1,000 products, the same data used in Build your first search experience. Each record contains several attributes, including:
JSON
This script writes to quickstart-products. If the index exists, the script replaces records with matching object IDs and updates the specified settings. To preserve an existing index, change the index name in the script.
1

Save the script

Select your language and save the script at the path in its first line.
2

Run the script

Run the script from the project root in the shell session where you set the environment variables.
3

Check the output

The script prints the number of records and batches sent, the retrieved record, and the search results for t-shirt. The search output includes the number of matches and the titles of the first three matches. The exact output format depends on the language.

Index your records

To index your own data, update the following sections of the sample script.
1

Initialize the API client

Change the index name in the script to match your index.
In production, load the API key from your secrets manager.
2

Fetch your data

  • Replace the code that retrieves the sample dataset with code that reads your data from a database, an export file, or an API.
  • Send each record as a JSON object. Include only the attributes you need for searching, filtering, and displaying results.
For more information, see Prepare your data.
3

Check object IDs

Add an objectID to each record. Use your data source’s primary key, converted to a string. Check that each objectID contains at least one character.
  • Make each objectID unique and stable across imports. The preceding code checks that each objectID is a string containing at least one character. It doesn’t check whether the IDs are unique or stable. The same source item should always have the same objectID, so sending it again updates the existing record instead of creating a duplicate.
  • If your data doesn’t have a stable key, derive an objectID that you can reproduce on the next import.
For more information, see Unique record identifiers.
4

Configure the index

For best performance, apply settings before you add records.
  • Replace title, description, and product_type with attributes from your records.
  • searchableAttributes sets the attributes Algolia searches, attributesForFaceting makes an attribute available for filtering, and attributesToSnippet shortens an attribute’s text in the results.
  • Order searchable attributes with the most important first. For more information, see Searchable attributes.
  • Consider adding a custom ranking attribute, such as popularity or rating, to break ties between records with the same relevance. For more information, see Custom ranking.
For an overview of these and other settings, see Relevance overview.
5

Send the records

Send the records and wait for indexing to finish before verifying the results.
  • For larger datasets, read your records in chunks that fit in memory and call saveObjects for each chunk. The helper splits each chunk into batches of 1,000. The Dart example sends batches explicitly. For more information, see Sending records in batches.
  • Before verifying records, wait for indexing to finish. The example waits for each batch so the verification step can retrieve and search the updated records. If you wait separately, use each batch’s taskID with waitForTask. For more information, see Wait for indexing tasks.
  • If a batch fails, earlier batches can remain indexed. For more information, see Recover from ingestion failures.
  • To replace the entire dataset, use replaceAllObjects instead of saveObjects (available for all API clients except Dart). Pass the complete dataset in one call, because the method removes records that aren’t in it. It builds a temporary index and replaces the original only when that succeeds, so searches continue during indexing. If your API key restricts access to specific indices, also allow INDEX_NAME_tmp_*, and replace INDEX_NAME with your index name. To delete the temporary index after a failed run, give the key the deleteIndex permission.
6

Verify the result

  • Replace the sample objectID and query with values from your data.
  • Update the attributes to retrieve and the attributes printed by the script.
  • Check the search results. The search returns matching records and their titles. The number and order of results can vary with the records and settings in your index.
  • If a record you expect is missing, see Troubleshooting search.

Keep your records up to date

After the first import, send changes rather than the whole dataset. To change some attributes of a record, use partialUpdateObjects with the record’s objectID. Top-level attributes you don’t send stay as they are. If you update a nested attribute, Algolia replaces its entire top-level attribute. Include any nested values you want to keep. In the following code, replace the objectID with one from your index, and replace price and 26 with the attribute and value you want to update.
This example updates the price of an existing record. With createIfNotExists set to false, Algolia doesn’t create a record if the specified objectID is missing, and it doesn’t return an error.
For adding, replacing, and deleting records, including deletion by query, see Incremental updates. To decide between incremental updates and a full reindex, see Data synchronization strategies. If your data is in a database, a hosted file, or an ecommerce platform, you don’t have to write this code. A connector reads from the source and updates your index on a schedule.
Last modified on December 2, 2026