Typesense Python Client
Official Python client for Typesense — open source typo-tolerant search engine, alternative to Algolia/Elasticsearch. Current version: 2.0.0 (Mar 2026). v2.0 added AsyncClient. Client takes a config dict with 'nodes' list — not a URL string. Typesense Cloud uses port 443 and HTTPS. Self-hosted uses port 8108 and HTTP by default.
Warnings
- gotcha Client config requires 'nodes' as a list — not a URL string or flat host/port keys. Passing a URL string raises TypeError.
- gotcha Typesense Cloud uses port 443 and protocol 'https'. Self-hosted defaults to port 8108 and 'http'. Mixing these up causes connection errors.
- gotcha Collection schema is required before indexing — Typesense is schema-enforced unlike Elasticsearch. Indexing without creating collection first raises ObjectNotFound.
- gotcha document 'id' field must be a string — not an integer. Passing int raises validation error.
- gotcha AsyncClient (v2.0) requires await client.api_call.aclose() to close connections. Omitting this leaks HTTP connections.
- gotcha Client version should match Typesense server version. Python client v2.0 targets Typesense server v26+. Check compatibility matrix.
Install
-
pip install typesense
Imports
- Client (sync)
import typesense client = typesense.Client({ 'nodes': [{ 'host': 'localhost', 'port': '8108', 'protocol': 'http' }], 'api_key': 'your-api-key', 'connection_timeout_seconds': 2 }) # Create collection client.collections.create({ 'name': 'products', 'fields': [ {'name': 'name', 'type': 'string'}, {'name': 'price', 'type': 'float'} ], 'default_sorting_field': 'price' }) # Index document client.collections['products'].documents.create({ 'id': '1', 'name': 'Widget', 'price': 9.99 }) # Search result = client.collections['products'].documents.search({ 'q': 'widget', 'query_by': 'name' }) - AsyncClient (v2.0+)
import typesense import asyncio async def main(): client = typesense.AsyncClient({ 'nodes': [{ 'host': 'localhost', 'port': '8108', 'protocol': 'http' }], 'api_key': 'your-api-key', 'connection_timeout_seconds': 2 }) result = await client.collections.retrieve() await client.api_call.aclose() # must close return result asyncio.run(main())
Quickstart
# pip install typesense
import typesense
# Self-hosted
client = typesense.Client({
'nodes': [{'host': 'localhost', 'port': '8108', 'protocol': 'http'}],
'api_key': 'your-api-key',
'connection_timeout_seconds': 2
})
# Typesense Cloud
# client = typesense.Client({
# 'nodes': [{'host': 'xxx.a1.typesense.net', 'port': '443', 'protocol': 'https'}],
# 'api_key': 'your-cloud-api-key',
# 'connection_timeout_seconds': 2
# })
# Create collection with schema
client.collections.create({
'name': 'books',
'fields': [
{'name': 'title', 'type': 'string'},
{'name': 'author', 'type': 'string', 'facet': True},
{'name': 'year', 'type': 'int32'}
]
})
# Index
client.collections['books'].documents.create({
'id': '1', 'title': 'Dune', 'author': 'Herbert', 'year': 1965
})
# Search
result = client.collections['books'].documents.search({
'q': 'dune',
'query_by': 'title,author'
})
print(result['hits'])