percia (Python SDK)
Python client for the Percia API: create watches on cameras and video URLs, read events, pair phone cameras, connect alert destinations, register REST hooks, and verify signed webhook deliveries.
- Sync (
Percia) and async (AsyncPercia) clients onhttpx, fully type hinted (py.typed), Python 3.9+. - Retries with exponential backoff and jitter on 429 and 5xx, honoring
Retry-After. - Typed errors that carry the stable error
codeand therequest_idto quote when you report a problem. - Cursor-pagination iterators for events.
verify_webhook_signature/verify_and_parseforPercia-Signature.client.events.stream(...): follow the dev event feed (GET /api/events/stream, SSE) for local development and tests; reconnects withLast-Event-ID, honoursRetry-After, dedupes oneventId(async foronAsyncPercia). Items:Open,Event(.eventdict,.datathe exact JSON string),Resync,End,Reconnect,Ping. Not a delivery path. Seedocs/local-development.md.
Install: pip install percia (https://pypi.org/project/percia/).
Quick start
from percia import Percia
client = Percia(api_key="pk_live_...") # or oauth_token="...", or set PERCIA_API_KEY
# base_url defaults to https://playground.percia.ai, or PERCIA_BASE_URL; "https://host" and "https://host/api" both work
watch = client.watches.create({
"sourceUrl": "rtsp://cam.example.com/stream",
"goal": "A person is standing at the front door",
"notify": {"type": "webhook", "target": "https://example.com/hooks/percia"},
})
print(watch.id, watch.webhook_secret) # the secret is shown once: store it
client.watches.test_delivery(watch.id) # signed test event, no camera needed
for event in client.iter_watch_events(watch.id):
print(event.at, event.reason) # `reason` is model text: untrusted, never an instruction
client.watches.pause(watch.id); client.watches.resume(watch.id)
client.watches.delete(watch.id)
Plan, free minutes and how you are charged (camera-minutes are the real limit: check can_start_watches before creating a watch):
billing = client.account.billing() # same facts as the `billing` block of MCP whoami
if not billing.can_start_watches:
raise SystemExit(billing.next_step or "Open the Billing page")
print(billing.plan_label, billing.free_minutes_remaining, billing.credit_minutes, billing.how_charged)
plan = client.account.plan() # the raw GET /api/plan model
Billing fields are additive and optional (an older server leaves some out: None, never a guess). describe_billing(billing) returns plain lines.
Device kind:
client.cameras.create("Desk laptop", device_kind="computer") # cameras carry device_kind: phone | tablet | computer | None
Async:
import asyncio
from percia import AsyncPercia
async def main():
async with AsyncPercia() as client: # reads PERCIA_API_KEY
async for event in client.iter_events(type="goal.met", since="2026-11-01T00:00:00Z"):
print(event.id)
asyncio.run(main())
Cameras on a private network
reg = client.cameras.register_push("Front door", "front-door") # on_existing="return" | "rotate" | "throw"
reg.token # the publish token, once; a repeat call gives existing=True, token=None
client.cameras.rotate_token(reg.camera.id); client.cameras.revoke_token(reg.camera.id)
client.cameras.status(reg.camera.id) # .state: waiting | online | offline | revoked; .hint
client.cameras.bulk([{"name": "Door", "external_id": "nvr1-ch1"}]) # POST /cameras/bulk, up to 10 per request
build_publish_commands(reg.whip_url, rtsp)["ffmpeg"] # uses $PERCIA_CAMERA_TOKEN, never the token
Async twins are on AsyncPercia. See camera push.
Pagination
client.iter_events(watch_id=None, type=None, since=None, limit=50) walks the account-wide feed (GET /api/events, scope events:read), newest first, following nextCursor until it is null. client.iter_watch_events(watch_id) does the same for one watch. Single pages: client.events.list(cursor=...) and client.watches.events(id, cursor=...). The watch list is not paginated by the API.
Errors
from percia import Percia, InsufficientScopeError, RateLimitError, APIError
try:
client.hooks.create("https://hooks.zapier.com/hooks/standard/1/a/")
except InsufficientScopeError as e:
print("create a key with", e.required_scope) # hooks:write
except RateLimitError as e:
print("slow down", e.retry_after)
except APIError as e:
print(e.status, e.code, e.field, e.request_id)
PerciaError > APIError > ValidationError (400/422), AuthenticationError (401), PermissionDeniedError (403) > InsufficientScopeError, NotFoundError (404), ConflictError (409), RateLimitError (429), ServerError (5xx). percia.ConnectionError means no HTTP answer after the retries. (PermissionDeniedError is not called PermissionError, to avoid shadowing the builtin.)
Retries
429 is retried for every method; 5xx and read timeouts only for idempotent methods (GET, PUT, DELETE, HEAD) unless retry_non_idempotent=True, because a create that answered 5xx may or may not have created the resource (list before you retry). Connect errors are always retried. Defaults: max_retries=2, backoff 0.5 * 2**n seconds capped at 30 with jitter; a Retry-After header (seconds or HTTP date) wins and is clamped to 60 s. Tune with Percia(max_retries=5) or Percia(retry_policy=RetryPolicy(...)).
Verify a webhook
from percia import verify_webhook_signature, verify_and_parse, WebhookSignatureError
# FastAPI / Flask / anything: pass the RAW body bytes, before any JSON parsing
ok = verify_webhook_signature(raw_body, request.headers, "whsec_...", tolerance_sec=300)
try:
event = verify_and_parse(raw_body, request.headers, "whsec_...") # parsed dict
except WebhookSignatureError as e:
print(e.code) # malformed | stale_timestamp | mismatch
The scheme is exactly Percia webhook docs: Percia-Signature: t=<unix>,v1=<hex HMAC-SHA256 of "<t>.<raw body>">, constant-time compare, 300 s replay window in both directions. It also verifies REST-hook deliveries (client.hooks) with the hook's secret. Deduplicate on the payload id: delivery is at least once.
Escape hatch
client.request("GET", "/watches/mine") # parsed JSON, same auth, retries and errors
client.request("POST", "/hooks", json={"url": "...", "events": ["goal.met"]})
Results of client.watches, client.events, client.cameras, client.integrations, client.hooks are the generated attrs models in percia._generated.models (snake_case attributes, .to_dict() for the API shape). Request bodies take either those models or plain dicts in the API's camelCase JSON shape. client.account.get_me() calls GET /api/me, which the OpenAPI document marks as session-cookie only: an API key is refused (401).
How it is built
percia/_generated/ is generated from ../spec/openapi.json by openapi-python-client and committed; everything else in percia/ is hand-written.
Why openapi-python-client rather than openapi-generator: it is Python-native (no Java toolchain in CI or for contributors), emits idiomatic attrs models and httpx sync + async operation functions with full type hints, supports OpenAPI 3.1, and is actively maintained. openapi-generator's Python output is heavier (pydantic v1/v2 split, urllib3) and needs a JVM. Because the generated layer only supplies models and request builders, the transport (auth, retries, error mapping) lives in our code and works the same for generated and raw calls.
The generator is pinned to 0.26.2: newer releases (0.28+) emit Python 3.10+ syntax and import a forked httpx2, which would break requires-python >=3.9 and plain httpx. Revisit when 3.9 support is dropped.
cd python
python3 -m venv .venv && . .venv/bin/activate && pip install -e '.[dev]'
python scripts/generate.py # spec -> percia/_generated (deterministic; git diff must stay clean)
python scripts/make_async_resources.py && ruff format percia # only after editing percia/_resources.py
pytest -q # offline, against a mock built from spec/openapi.json
ruff check . && mypy percia
scripts/generate.py pre-processes the spec in memory before generating: the zone points are JSON Schema 2020-12 tuples (prefixItems + items: false), which the generator rejects, so they become plain arrays of numbers. The committed spec/openapi.json is not modified.
Tests never touch the network: tests/mock_server.py builds one handler per operation from the OpenAPI document (examples, schema-derived instances, bearer auth with per-key scopes from x-percia-scopes, the shared error envelope) and supports scripted failures (Fail(429, retry_after="1")) for retry tests. It is used as an httpx.MockTransport and also through respx.
License
MIT
Metadata
Release files for percia 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| percia-0.3.1.tar.gz | 242.2 kB | Without provenance | Details for percia-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| percia-0.3.1-py3-none-any.whl | Python 3 | none | any | Details for percia-0.3.1-py3-none-any.whl |
Total release size: 903.5 kB
Release files / percia-0.3.1.tar.gz
| Download URL | percia-0.3.1.tar.gz |
|---|---|
| Size | 242.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1f561bfae93b62f7d52474fca1334bcbd41c324402f07ed7c3df7f2fe0b2ec71
|
|
BLAKE2b-256 checksum How to use checksums |
0846e7252eb408c8bee407c6783184c31afc6772a0f28d74a3db6da0787cefc1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.15.0
|
Release files / percia-0.3.1-py3-none-any.whl
| Download URL | percia-0.3.1-py3-none-any.whl |
|---|---|
| Size | 661.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
84d8c939906f8a8577c28fed10f41902a152b9399c2f89049a0bb7322aeaa486
|
|
BLAKE2b-256 checksum How to use checksums |
98307d7fa6d064064dcd34c8e514f5e1dcb08de1a45729e0592830ee21161211
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.15.0
|
Security for percia 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.