Skip to main content

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

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

Metadata

Release files for backlex 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for backlex 0.1.0
File Size Uploaded
backlex-0.1.0.tar.gz 19.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for backlex 0.1.0
File Interpreter ABI Platform
backlex-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 38.0 kB

Release files / backlex-0.1.0.tar.gz

Download URL backlex-0.1.0.tar.gz
Size 19.1 kB
Tags Source
SHA-256 checksum
How to use checksums
19ef15711f072fa223de40cf17b3a0b5973df9bdc34bd251863f3c742a752aef
BLAKE2b-256 checksum
How to use checksums
2135ad9909c7c1eac44ffec84acf98ad8ed1caf466ed75028bd2d8edbc5f9338
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / backlex-0.1.0-py3-none-any.whl

Download URL backlex-0.1.0-py3-none-any.whl
Size 19.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f7648460bce5ab2594c91a6c592b5d54c7843d02881f0802bf59432b18aa9adb
BLAKE2b-256 checksum
How to use checksums
2c6e10359f6b0837d3f8ceb6b1927ad09a05925eceb2c8cb295eefcc85ab8b14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page