API Reference / API Methods / Initialize the Python API client

Initialize the Python API client

Before you can interact with Algolia’s APIs, for example, to index your data or search your indices, you need to authenticate with Algolia with your Application ID and API key by initializing a client. You can find both in your Algolia account.

Initialize the search client

The search client lets you manage your indices, add data to your indices, and search your indices.

1
2
3
4
from algoliasearch.search_client import SearchClient

client = SearchClient.create('YourApplicationID', 'YourWriteAPIKey')
index = client.init_index('your_index_name')

Replace your_index_name with the name of the index you want to use. You can find your existing indices in the Algolia dashboard, or by using the listIndices method. If the index doesn’t exist, a new, empty index is created locally. It’s created on Algolia’s servers only if you add records to the index.

Don’t use any sensitive or personally identifiable information as your index name, including usernames, IDs, or email addresses. Index names are publicly shared.

Operations, that are scoped to your Algolia application via the client are:

Operations scoped to an index are:

The Recommend, Personalization, Insights, and A/B testing APIs come with their own clients.

Usage notes

You can use the Python API client in asynchronous environments. You need to install these additional dependencies:

1
pip install 'asyncio>=3.4,<4.0' 'aiohttp>=2.0,<4.0' 'async_timeout>=2.0,<4.0'

The asynchronous methods are available using a _async suffix:

Synchronous Asynchronous
search search_async
save_objects save_objects_async
set_settings set_settings_async
save_synonyms save_synonyms_async

The following example creates a new index and adds two test records to it asynchronously.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import asyncio

from algoliasearch.search_client import SearchClient

app_id = 'YourApplicationID'
api_key = 'YourWriteAPIKey'

async def main():
  async with SearchClient.create(app_id, api_key) as client:
      index = client.init_index('test_index')

      response = await index.save_objects_async([
          {'objectID': 1, 'name': 'foo'},
          {'objectID': 2, 'name': 'bar'}
      ])

      results = await index.search_async('')

      print(results)

  asyncio.run(main())
Did you find this page helpful?