Skip to Content
NotebooksAccountSign-in, refresh, 2FA and logout

Sign-in, refresh, 2FA and logout

Download notebook (.ipynb)

API keys convert files; account calls need a sign-in token. Each cell prints the request line (→) and the backend’s JSON reply, tokens masked. A 2FA account answers login with MfaRequired; the login cell then asks for the code.

Config

Add your settings to the repo .env (local) or Colab Secrets — this cell reads them with get(...): INDOX_API_KEY (your ak_… key) and INDOX_BASE_URL (https://indox.org; the SDK adds /api/v1/… itself). A missing key stops here with its name. This notebook also signs in: add INDOX_EMAIL and INDOX_PASSWORD the same way.

import json, logging, re, sys, tempfile from pathlib import Path REPO = Path.cwd().parents[1] sys.path[:0] = [str(REPO), str(REPO / "languages" / "python")] try: from google.colab import userdata get = userdata.get except ImportError: from tools.env import require_env as get from indox_client import Indox logging.basicConfig(format="→ %(message)s", force=True) logging.getLogger("urllib3.connectionpool").setLevel(logging.DEBUG) def _mask(record): record.msg = re.sub(r"/(public|sign)/[^/ ]+", r"/\1/***", record.getMessage()) record.args = () return True logging.getLogger("urllib3.connectionpool").addFilter(_mask) def show(data, limit=2500): print(json.dumps(data, indent=2, default=str)[:limit]) client = Indox(api_key=get("INDOX_API_KEY"), base_url=get("INDOX_BASE_URL")) start_credits = client.billing.credits_balance()["credit_amount"] SAMPLES = REPO / "colab" out = Path(tempfile.mkdtemp()) from indox_client import MfaRequired def masked(data): return {k: "***" if k.endswith(("token", "url")) else v for k, v in data.items()}
try: session = client.user.login(email=get("INDOX_EMAIL"), password=get("INDOX_PASSWORD")) except MfaRequired as challenge: session = client.user.complete_2fa(challenge.mfa_token, input("2FA code: ")) token = session["external_token"] assert session["token_type"] == "Bearer" and token show(masked(session))
refreshed = client.user.refresh() # the client holds the refresh cookie from login assert refreshed["external_token"] != token token = refreshed["external_token"] show(masked(refreshed))
profile = client.user.update_me(access_token=token) assert profile["email"] show(profile)
bye = client.user.logout() assert bye["message"] show(bye)

Usage

now = client.billing.credits_balance()["credit_amount"] print(f"credits used by this notebook: {start_credits - now} (balance {now})") show(client.billing.usage_by_feature())