Skip to content

Python SDK

shoofai wraps the REST API for Python 3.9 and later. It uses only the standard library, so it adds no dependencies.

Terminal window
pip install shoofai

It has two clients:

  • ShoofClient, for searching, with a search key (shf_pub_…) or a secured key.
  • ShoofAdminClient, for indexing and settings, with an admin key (shf_adm_…).

Request fields keep the API’s own names, such as hitsPerPage and filters, so the API reference applies as written. Responses are the API’s JSON, as dictionaries.

from shoofai import ShoofClient
client = ShoofClient("my-store", "shf_pub_…")
res = client.search(
"running shoes",
lang="en",
filters={"brand": ["Nike", "Asics"], "price": {"lte": 150}},
facets=["brand", "categories", "price"],
hitsPerPage=24,
)
for hit in res["hits"]:
print(hit["title"], hit.get("price"))

When you search for a visitor from your server, see Searching from your server for the key and headers to use. Pass extra headers with headers={...}.

Option Default Does
host https://api.shoofai.com The API host.
user_token a random anon-… ID The visitor’s ID, sent with searches and events.
timeout 10 Seconds to wait for a response. Streaming answers have none. The admin client’s default is 120.
max_retries 2 How many times to retry. See Retries.
headers Extra headers on every request.
send urllib Your own HTTP function, for tests or proxies.
Method Returns API
search(q, **params) dict Search
multi_search(requests) list of dicts Multi-search
autocomplete(q, lang=, hits=, filters=, analytics=) dict Autocomplete
search_facet_values(field, **params) dict Search facet values
answer(q, on_sources=, on_delta=, **params) AnswerResult Answer
answer_stream(q, **params) iterator of AnswerEvent Answer
related(id, lang=, limit=) list Related items
trending(limit=, lang=, type=, filters=) list Trending items
trending_queries(limit=, lang=) list Trending queries
bought_together(id, limit=, lang=) list Bought together
config() dict Config
send_events(events, idempotency_key=) Events
result = client.answer(
"what is your return policy?",
lang="en",
on_delta=lambda text, full: print(text, end="", flush=True),
)
print(result.mode, [s["id"] for s in result.sources])

answer_stream() yields each event (sources, delta, done, error) as it arrives, if you’d rather handle them yourself.

import os
from shoofai import ShoofAdminClient
admin = ShoofAdminClient("my-store", os.environ["SHOOF_ADMIN_KEY"])
result = admin.upsert_all(records, on_progress=lambda done, total: print(f"{done}/{total}"))
print(result["indexed"], result["errors"])
Method Does
batch(upserts=, deletes=, reindex_id=, refresh=) Upserts up to 500 and deletes up to 1,000 records. Records
upsert_all(records, reindex_id=, batch_size=, on_progress=) Sends any number of records in batches.
get_record(id) / delete_record(id) Reads or deletes one record.
clear() Deletes every record.
start_reindex(copy=, restart=) / reindex_status() / commit_reindex(id) / cancel_reindex() A full resync.
status() Record counts, embeddings and any resync.
get_settings() / set_settings(s) / patch_settings(s) Read, replace or update settings.
apply_preset("store" or "blog", if_unchanged=) Sets the facets for a kind of site.
field_coverage() How many records have each field.
get_synonyms() / set_synonyms(list) Read or replace synonyms.
get_rules() / set_rules(list) Read or replace rules.

A full resync:

reindex_id = admin.start_reindex()["reindexId"]
admin.upsert_all(all_records, reindex_id=reindex_id)
admin.commit_reindex(reindex_id) # removes the records that weren't re-sent

Both clients retry a failed request up to max_retries times, waiting longer each time. A 429 is retried after its Retry-After, if that’s a minute or less. A 502, 503, 504 or a request that got no response is retried for reads, and for writes that carry an idempotency key. send_events() and the admin client’s batch(), clear(), start_reindex(), commit_reindex() and apply_preset() send a new key with each call. Pass idempotency_key= to make a write safe to repeat across processes.

from shoofai import ShoofApiError, ShoofConnectionError
try:
client.search("shoes", hitsPerPage=500)
except ShoofApiError as e:
print(e.status, e.code, e.message, e.details, e.retry_after)
except ShoofConnectionError:
print("The API couldn't be reached")

Both inherit from ShoofError. See Errors for every code.