Skip to content

Install the Python SDK

Install the package, hold your keys in the environment, build one client and reuse it. Five minutes, and the last two steps are the ones that matter later.

  1. Install the package

    Terminal
    pip install neuctra-authix
    
    # or, with the tool you actually use
    poetry add neuctra-authix
    uv add neuctra-authix
    pipenv install neuctra-authix

    Requires Python 3.8 or newer. The only runtime dependency is requests.

  2. Create an app and issue a key

    In the dashboard, create an app and copy its app id, then create a key on the API Keys page with its Type set to Secret — Python code that runs on your server uses a secret key (sk_live_…). Choose Publishable (pk_live_…) only for a program that runs on an end user's machine. Either kind can be limited to one app when you create it. Keys are created and revoked only in the dashboard.

  3. Put the keys in the environment

    Terminal
    # .env — never commit this file
    AUTHIX_APP_ID=cmsq8f2k10001na9x...
    AUTHIX_SECRET_KEY=sk_live_a1b2c3d4_...
    
    # A secret key has full account authority. Keep it on a trusted server.

    Keys in source control are the most common way APIs leak. If one reaches a commit, revoke it in the dashboard — rewriting history is not enough, because the value is already in every clone.

  4. Construct the client

    Python
    1import os
    2from neuctra_authix import Authix
    3
    4authix = Authix(
    5    app_id=os.environ["AUTHIX_APP_ID"],
    6    api_key=os.environ["AUTHIX_SECRET_KEY"],
    7)

    Both arguments are keyword-only. The same api_key argument takes either kind of key — the server knows which kind it is and what it may do.

  5. Verify it works

    Python
    1# Confirms the key, the app id and the network path in one call.
    2print(authix.key_prefix)   # "sk_live_a1b2c3d4" — safe to log
    3
    4page = authix.get_all_users_from_app(limit=1)
    5print(f"reachable, {len(page)} user(s) returned")

Constructor arguments

ParamTypeRequiredDescription
app_idstrYesThe app these calls belong to. Every request carries it.
api_keystrYessk_live_… secret key on a server (full account authority), or pk_live_… publishable key in a program that ships to end users. Legacy ak_live_… keys are secret keys.
base_urlstrNoAPI origin. Override only when pointing at a different deployment.
app_namestrNoOptional label sent with requests; useful when one account serves several services.
timeoutfloatNoPer-request timeout in seconds. Set one — the default is generous for a web request.
sessionrequests.SessionNoBring your own session to configure retries, proxies or TLS.

Acting as an end user

A client can sign someone in and then act as them. The session cookie lives on the client's requests.Session, so own-data calls resolve that user from the cookie. With a publishable key that is the only way to reach a user's data; a secret key can also name any user by id.

Python
1# A client that acts as an end user instead of as the account owner.
2authix = Authix(
3    app_id=os.environ["AUTHIX_APP_ID"],
4    api_key=os.environ["AUTHIX_SECRET_KEY"],
5)
6
7authix.login_user(email="[email protected]", password="…")
8
9# The cookie from that login lives on the client's requests.Session,
10# so this call is scoped to Ada and no user id is needed.
11session = authix.check_user_session()

One client per signed-in user

Because the cookie is stored on the client, a client shared across a web server's requests would serve one user's session to the next visitor. In a web application, either build a short-lived client per request, or use one shared secret-key client that never signs anyone in and pass the user id — taken from a session your server has verified — explicitly.

Key mistakes caught at startup

Python
1# Malformed keys fail locally, before a request is sent.
2Authix(app_id=APP_ID, api_key="not-a-valid-key")
3# ConfigurationError: Malformed API key.

The key is parsed locally before anything is sent, so a malformed or truncated key raises immediately with a message naming the actual problem instead of failing on the first request. Whether a key is publishable or secret is decided by the server: a publishable key on a secret-only call raises InsufficientScopeError.

One client per process

Python
1# authix_client.py — build once, import everywhere.
2import os
3from functools import lru_cache
4from neuctra_authix import Authix
5
6
7@lru_cache(maxsize=1)
8def get_authix() -> Authix:
9    """One client per process.
10
11    The client owns a requests.Session and therefore a connection pool;
12    constructing a new one per request throws that away and adds a TLS
13    handshake to every call.
14    """
15    return Authix(
16        app_id=os.environ["AUTHIX_APP_ID"],
17        api_key=os.environ["AUTHIX_SECRET_KEY"],
18    )

Timeouts and retries

Python
1authix = Authix(
2    app_id=APP_ID,
3    api_key=SECRET_KEY,
4    timeout=10.0,          # seconds; applies to every request
5)
6
7# Bring your own session to add retries, proxies or custom TLS.
8import requests
9from requests.adapters import HTTPAdapter
10from urllib3.util.retry import Retry
11
12session = requests.Session()
13session.mount(
14    "https://",
15    HTTPAdapter(
16        max_retries=Retry(
17            total=3,
18            backoff_factor=0.5,
19            # Only idempotent methods. A retried POST can create twice.
20            allowed_methods={"GET"},
21            status_forcelist=(502, 503, 504),
22        )
23    ),
24)
25
26authix = Authix(app_id=APP_ID, api_key=SECRET_KEY, session=session)

Never blanket-retry writes

A create that timed out may well have succeeded, with only the response lost. Retrying it produces two records. Restrict automatic retries to GET, and handle write failures where you can decide what the right answer is.

Keys never appear in logs

repr(authix) and authix.key_prefix expose only the non-API lookup handle, e.g. sk_live_a1b2c3d4. Use mask_key() if you need to display a key you hold elsewhere.

Related