Python SDK

Quickstart

Verify a Scute access token in a FastAPI route and check a permission.

This page assumes a frontend that already signs people in with a Scute SDK and sends the access token to your API.

  1. Install

    pip install "scute[fastapi] @ git+https://github.com/scuteai/scute-python" uvicorn
  2. Set the credentials

    export SCUTE_APP_ID=app_xxxexport SCUTE_SECRET=...

    SCUTE_SECRET is only needed for the permission check in step 4.

  3. Verify the token

    main.pypython
    from typing import Annotatedfrom fastapi import Depends, FastAPIfrom scute import Scute, Sessionfrom scute.contrib.fastapi import ScuteAuthapp = FastAPI()auth = ScuteAuth(Scute())@app.get("/me")def me(session: Annotated[Session, Depends(auth.session)]) -> dict:    return {"user_id": session.user_id, "impersonated": session.impersonated}

    auth.session reads the token from the X-Authorization header, then Authorization: Bearer, then the browser SDK's sc-access-token__<app id> cookie. It verifies the token against the app's published keys and returns a Session. A missing or invalid token gets 401 with {"detail": {"error": "...", "reason": "expired"}}.

  4. Check a permission

    main.pypython
    @app.post("/invoices/{id}/refund", dependencies=[Depends(auth.require("refund", "invoice:{id}"))])def refund(id: str) -> dict:    return {"refunded": id}

    auth.require verifies the session, fills {id} from the path and asks Scute whether the user may refund invoice:42. Anything other than a plain allow gets 403.

  5. Run it

    uvicorn main:appcurl localhost:8000/me -H "X-Authorization: $ACCESS_TOKEN"

Without the framework helper#

from scute import InvalidToken, Scutescute = Scute()try:    session = scute.tokens.verify(token)except InvalidToken as e:    print(e.reason)  # "missing", "malformed", "expired", "wrong_app", ...

See Verify tokens for every check and failure reason, and FastAPI for the full integration.