Skip to content

Latest commit

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

ShreAI Python SDK

POS / BOS event SDK for Python. Single file, stdlib only — no dependencies. Same wire contract as the JavaScript and Swift v2 SDKs.

fromshreaiimportShreAIsa=ShreAI(
endpoint="https://apiauth.shre.ai",
events_endpoint="https://events.shre.ai",
tenant_id="merchant_123",
app="rapid_pos",
mode="read_only",
)
sa.start()
sa.track("price_updated", entity_type="item", entity_id="UPC_012345678905",
metadata={"old": 10.49, "new": 10.99})

Install

From PyPI (when published)

pip install shreai

From git

pip install git+https://github.com/Shreai/sdk-python.git@v2.0.0

Single-file copy

curl -O https://raw.githubusercontent.com/Shreai/sdk-python/main/shreai.py

Drop shreai.py next to your code and import shreai.

Quick start

fromshreaiimportShreAI# Initialize once at app startsa=ShreAI(
endpoint="https://apiauth.shre.ai",
events_endpoint="https://events.shre.ai",
tenant_id="<your_tenant>",
app="rapid_pos", # or rapid_bosmode="read_only", # default; no API key neededon_error=lambdae, ctx: print(f"[ShreAI] {ctx}: {e}"),
on_flush=lambdasent, failed: print(f"[ShreAI] flushed {sent}/{failed}"),
)
sa.start()
# Track events anywheresa.track("screen_viewed", entity_type="screen", entity_id="ItemEdit")
sa.track("item_scanned", entity_type="item", entity_id=barcode,
metadata={"price": item_price, "department": dept})
sa.track("transaction_complete", entity_type="transaction", entity_id=txn_id,
metadata={"total": total, "item_count": n})
# Optional — graceful shutdown drains the queuesa.stop()

start() bootstraps a session, fetches kill-switch config, starts the flush + config-refresh threads. The SDK auto-drains on interpreter exit via atexit.

Configuration

FieldRequiredDefaultNotes
endpointyeshttps://apiauth.shre.ai
events_endpointno= endpointhttps://events.shre.ai
tenant_idyesYour store / tenant identifier
appyes^[a-z][a-z0-9_-]{0,31}$
modeno"read_only"or "read_write" (needs bootstrap_key)
bootstrap_keyiff read_writePublic bootstrap key from Shre AI ops
store_id, user_id, rolenoOptional context
flush_interval_secondsno10Server may override
batch_sizeno50Server may override
max_queue_sizeno5000Local queue cap
timeout_sno8.0Per-request HTTP timeout
on_error, on_flushnoCallbacks

Behavior

  • Fire-and-forgettrack() returns immediately, never blocks
  • Local queue up to 5000 events; oldest dropped at cap
  • Batches every 10 s (server-tunable)
  • Idempotent — every event has a UUID; server upserts on (tenant_id, event_id)
  • Backoff on failure: 5 → 15 → 30 → 60 → 300 s
  • Drains on exitatexit hook calls stop() automatically

Error handling

HTTPSDK reaction
200Drain accepted events from queue
401Re-bootstrap session (refresh JWT, read_write only)
403Set local kill-switch — stop tracking until next config refresh
429 / 5xxExponential backoff: 5 → 15 → 30 → 60 → 300 s
Network offlineStay queued; flush on next interval after recovery

Forbidden hosts

The SDK refuses to initialize against any hostname starting with downloads. (it's the asset host, not the API). You'll get a ShreAIError at construction time.

Threading model

The SDK starts two daemon threads:

  • shreai-flush — every flush_interval_seconds, attempts to drain the queue
  • shreai-config — every 5 min, refreshes the remote kill-switch config

All daemon threads. No process-keepalive contribution. Clean exit via stop() or atexit.

Cloudflare WAF note

The SDK sets User-Agent: shreai-sdk/python/2.0.0 on every request. Default Python urllib UA is blocked by Cloudflare's bot detection (error 1010); we sidestep by always sending the SDK identity.

Platforms

  • Python 3.9+
  • Works on macOS, Linux, Windows
  • No GIL-sensitive code; safe to use from multi-threaded apps
  • Compatible with WSGI / ASGI web frameworks (Flask, FastAPI, Django, etc.)

License

MIT. See LICENSE.

Spec

This SDK implements the Shre AI Shared SDK Spec. Drift between language SDKs is a release blocker.

About

Shre AI Python SDK — POS/BOS event SDK. Single-file, stdlib only.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages