Official Python client for the backlex API (CRUD, query builder, auth, realtime, storage).
Project description
backlex — Python SDK
Official Python client for the backlex API. A thin, typed wrapper over the same
REST + SSE surface the TypeScript SDK (@backlex/client) speaks — CRUD, a fluent
query builder, auth, realtime, and storage.
This package is the reference port for backlex's multi-language SDK effort (Python → Go → .NET/Java → Swift/Kotlin). It demonstrates the hybrid model: hand-written ergonomic layer on top, optional OpenAPI-generated models underneath (see Hybrid codegen).
pip install backlex # not yet published — for now: pip install -e sdks/python
Quickstart
from backlex import create_client
client = create_client("https://api.example.com", api_key="pak_...")
# CRUD
post = client.from_("posts").create({"title": "Hello"})["data"]
client.from_("posts").update(post["id"], {"title": "Edited"})
client.from_("posts").delete(post["id"])
# Fluent query builder → compiles to canonical JSON (same wire format as TS)
rows = (
client.from_("orders").query()
.where(lambda f: f.and_(
f.eq("status", "active"),
f.gte("total", 100),
f.rel("customer", lambda c: c.eq("tier", "gold")), # → "customer.tier"
f.gte("placed_at", f.now(sub={"months": 1})),
))
.select("id", "total", "customer.name")
.order_by("-placed_at", "id")
.limit(50)
.list()
)["data"]
Auth
# Server-to-server: pass api_key="pak_..." to create_client (bearer on every call).
# App mode — end-users of a workspace:
client = create_client("https://api.example.com", workspace="myapp")
res = client.auth.sign_in("user@example.com", "password") # token auto-captured
token = client.auth.get_token() # persist this
# later: create_client(..., workspace="myapp", token=token) to restore the session
client.auth.sign_out()
auth.providers() returns the public auth surface (provider list + policy flags)
for rendering a sign-in screen. auth.sign_in_social(provider) and
auth.sign_in_magic_link(email) are also available.
Realtime (SSE)
unsub = client.subscribe("items:posts", lambda ev: print(ev["event"], ev["data"]))
# ... runs on a background daemon thread, auto-reconnects ...
unsub()
Storage
client.storage.put("avatars/me.png", open("me.png", "rb").read(), "image/png")
data = client.storage.download("avatars/me.png").content
client.storage.list(prefix="avatars/")
client.storage.delete("avatars/me.png")
Errors
Every non-2xx response raises BacklexError with .status, .code, and
.details (the { "error": {...} } envelope), so you branch on codes, not
strings.
Hybrid codegen
The hand-written layer above is stable and small. For typed models of your collections and the system API, generate them from the OpenAPI spec the server already ships — no Python-specific wire format is introduced.
# 1. System API models from the static OpenAPI spec:
openapi-generator generate \
-i apps/web/src/server/lib/openapi-static.generated.json \
-g python -o sdks/python/_generated --skip-validate
# 2. Per-collection types: the `backlex gen-types` CLI emits these for the
# TS SDK today; the Python equivalent reads /api/collections and writes
# TypedDicts you pass as `client.from_("posts") # -> dict[Post]`.
Generated models live alongside (not inside) the hand-written package, so the ergonomic surface stays clean while models track the spec.
Develop
cd sdks/python
pip install -e ".[dev]"
pytest # offline: query-builder + normalization contract tests
mypy # strict
ruff check .
Parity with the TS SDK
TS (@backlex/client) |
Python (backlex) |
|---|---|
createClient(opts) |
create_client(url, ...) |
client.from(slug) |
client.from_(slug) |
.query().where(f => ...) |
.query().where(lambda f: ...) |
f.and / or / not / in |
f.and_ / or_ / not_ / in_ |
.orderBy().withMeta() |
.order_by().with_meta() |
client.subscribe(ch, cb) |
client.subscribe(ch, cb) → unsub() |
auth.signIn / getToken |
auth.sign_in / get_token |
BacklexError |
BacklexError |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file backlex-0.0.1.tar.gz.
File metadata
- Download URL: backlex-0.0.1.tar.gz
- Upload date:
- Size: 18.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d53ef2be6411c98d67578f70b9983a63a5e5e72f7a18ac0c0ad33c52a1024d6
|
|
| MD5 |
afc601cec42485aea6c4e9e11c8f896e
|
|
| BLAKE2b-256 |
dc00dbbc241ee09bda557cc5ed40c7a779c621e824c39c04015eae1a220feca6
|
File details
Details for the file backlex-0.0.1-py3-none-any.whl.
File metadata
- Download URL: backlex-0.0.1-py3-none-any.whl
- Upload date:
- Size: 18.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b5ed5d5333b57a5586f0d26c0f9f0bcc96aa2a9ab39c8091fea354c68940793
|
|
| MD5 |
52859263a00fb75951df63117bd4b923
|
|
| BLAKE2b-256 |
707547e2525444cf979b39bd77f68be650a3d33cb8f71ac850652389051ad3a0
|