Skip to main content

Backend SDK for torii — verify JWTs, manage users, react to events from your Python server.

Project description

torii-backend

Backend SDK for torii — verify end-user JWTs without a per-request round trip and manage users from your Python server.

v0.x — API may still change.

Setup

  1. Sign in to app.torii.so and from your dashboard copy:

    • your issuer URL (e.g. https://acme.torii.so)
    • a secret key (sk_test_… for development, sk_live_… for production)
  2. Install the SDK:

    pip install torii-backend
    # or, with the FastAPI dependency adapter:
    pip install "torii-backend[fastapi]"
    

    Python 3.9+.

  3. Verify an end-user JWT:

    from torii_backend import verify_token
    
    auth = verify_token(token, issuer="https://acme.torii.so")
    print(auth.user_id, auth.environment_id, auth.email_verified)
    

    The first call fetches the issuer's JWKS; subsequent calls reuse the cache and rotate keys automatically (handled by PyJWT). No round trip per request.

  4. Call the backend REST API:

    import os
    from torii_backend import create_torii_client
    
    torii = create_torii_client(secret_key=os.environ["TORII_SECRET_KEY"])
    user = torii.users.get(user_id)
    

FastAPI

from fastapi import Depends, FastAPI
from torii_backend.fastapi import require_auth

app = FastAPI()
auth_dep = require_auth(issuer="https://acme.torii.so")

@app.get("/me")
def me(auth = Depends(auth_dep)):
    return {"user_id": auth.user_id}

Backend API

page = torii.users.list(limit=50)
user = torii.users.create(email="x@y.com")
torii.users.ban(user.id)

sessions = torii.sessions.list_for_user(user.id)
torii.sessions.revoke_all_for_user(user.id)

PATCH semantics (users.update)

users.update is a tri-state PATCH: each updatable field can be set, cleared, or left alone. Pass only the kwargs you want on the wire — pydantic v2's model_fields_set tracks which fields were explicitly provided, and the SDK serializes via model_dump(exclude_unset=True) so untouched fields never appear in the request body.

torii.users.update(
    user_id,
    name="Ada",     # → server updates name
    phone=None,     # → server clears phone (sent as JSON null)
    # address not passed → server leaves alone
)

Wire body for the call above:

{ "name": "Ada", "phone": null }
Call Field on wire Server effect
users.update(id, name="Ada") "name": "Ada" update name
users.update(id, phone=None) "phone": null clear phone
users.update(id) (no field kwargs) omitted leave alone

You can also build a request explicitly — useful when assembling the patch dynamically:

from torii_backend import ToriiUpdateUserInput

patch = ToriiUpdateUserInput(name="Ada", phone=None)
torii.users.update(user_id, patch)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torii_backend-0.0.5.tar.gz (45.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torii_backend-0.0.5-py3-none-any.whl (58.1 kB view details)

Uploaded Python 3

File details

Details for the file torii_backend-0.0.5.tar.gz.

File metadata

  • Download URL: torii_backend-0.0.5.tar.gz
  • Upload date:
  • Size: 45.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for torii_backend-0.0.5.tar.gz
Algorithm Hash digest
SHA256 d01d0d286b8867d5176fc5abccef6e2d568610d850e2bc4d730f0d40fbd210b4
MD5 6975a16850b816e87171c2e096db6178
BLAKE2b-256 01cfc9f4a15f1e49a60c0695814a1b29c3077cbb8fcafada8e3d3cd4a438048a

See more details on using hashes here.

Provenance

The following attestation bundles were made for torii_backend-0.0.5.tar.gz:

Publisher: release.yml on Torii-ApS/torii-sdk-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torii_backend-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: torii_backend-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 58.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for torii_backend-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 467b135fc154baf3ff60c6a1c7f46122edcddc7e8b50d9d22c392ea95a487841
MD5 d6fb1c5c2b61963935fe4ea18da7b78d
BLAKE2b-256 aa52d5775a1e462352520548663279add5dbab93f78d60ff310ad46eba161120

See more details on using hashes here.

Provenance

The following attestation bundles were made for torii_backend-0.0.5-py3-none-any.whl:

Publisher: release.yml on Torii-ApS/torii-sdk-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page