Python API clients
The Python API clients let you interact with Algolia’s APIs from your Python backend.
If you’re building a Django app, see Setting up Algolia for Django for more information.
Install the library
All API clients are part of the algoliasearch
Python package,
which you can install with pip
.
You’ll need Python version 3.8 or later.
1
pip install 'algoliasearch>=4,<5'
Test your installation
To test your installation, try running a short program that adds a record to a test index, searches your index, and prints the results.
-
If you haven’t already, create an Algolia account.
-
Copy the following code into a new file
hello_algolia.py
. ReplaceALGOLIA_APPLICATION_ID
andALGOLIA_API_KEY
with values from your account. Make sure to use an API key withaddObject
andsearch
permissions.Copy1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
# File: hello_algolia.py from algoliasearch.search.client import SearchClientSync app_id = "ALGOLIA_APPLICATION_ID" # API key with `addObject` and `search` ACL api_key = "ALGOLIA_API_KEY" index_name = "test-index" if __name__ == "__main__": client = SearchClientSync(app_id, api_key) record = {"objectID": "object-1", "name": "test record"} # Add record to an index save_resp = client.save_object( index_name=index_name, body=record, ) # Wait until indexing is done client.wait_for_task( index_name=index_name, task_id=save_resp.task_id, ) # Search for 'test' results = client.search( { "requests": [ { "indexName": index_name, "query": "test" } ] } ) print(results.to_json())
In production, don’t include your credentials in your code. Use environment variables instead.
-
Run:
python hello_algolia.py
If the command is successful, you’ll see the API response in JSON format.
Next steps
You can view your new index in the Algolia dashboard.