Skip to content
Nov 3, 2026 - Version 2026.10 has been released
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Refresh the user token automatically

A standalone script that keeps a valid Datasentinel user token cached for scheduled and scripted runs.

A person using an assistant signs in once with OAuth and never handles a token. A script or a scheduled job has nobody to sign in, so it authenticates with the platform's user token instead, sent as an Authorization: Bearer header by the MCP client: the --header option of Claude Code, the headers entry of a Python agent, the equivalent field of any other client. See How authentication works.

The script on this page is an example of how to use Datasentinel AI Connector from your own code. Use it as a starting point. Review it and adapt it to your environment before you run it in production.

The platform issues these tokens with a 24-hour lifetime and does not renew them. The script below keeps the cached token in ~/.datasentinel_token permanently valid, so a job that reads the header value from that file never fails on an expired credential.

It reads the cached token, checks the expiry (exp) claim inside the JWT, and mints a fresh token from the platform whenever the cached one is missing, malformed, or about to expire. It then prints the valid token on stdout, so a caller can capture the output or read the cache file.

It is a standalone version of the token handling built into the agent from Build a Python agent with deepagents, and the companion to the scheduling guidance in Run recurring analyses.

Save this as refresh_token.py:

#!/usr/bin/env python3
"""Print a valid Datasentinel user-token, minting a fresh one when the
cached token is missing or expired. Cache: ~/.datasentinel_token (0600)."""

import base64
import json
import os
import pathlib
import stat
import sys
import time

import httpx

TOKEN_PATH = pathlib.Path.home() / ".datasentinel_token"

# Refresh this many seconds before the real expiry so a token that is valid
# now but would lapse mid-run is treated as expired.
TOKEN_EXPIRY_MARGIN = 60


# The exp claim is a Unix timestamp; the platform issues tokens with a 24h
# life. A token that cannot be decoded or carries no exp is treated as
# expired so a fresh one is minted rather than sending a bad credential.
def token_expired(token: str) -> bool:
    try:
        payload = token.split(".")[1]
        # JWT uses base64url without padding; restore it before decoding.
        payload += "=" * (-len(payload) % 4)
        exp = json.loads(base64.urlsafe_b64decode(payload))["exp"]
    except (IndexError, ValueError, KeyError, TypeError):
        return True
    return time.time() >= exp - TOKEN_EXPIRY_MARGIN


def mint_token() -> str:
    host = os.environ.get("DATASENTINEL_HOST")
    user = os.environ.get("DATASENTINEL_USER")
    password = os.environ.get("DATASENTINEL_PASSWORD")
    if not (host and user and password):
        sys.exit(
            "Datasentinel user-token is missing or expired and cannot be "
            "renewed.\n"
            "Set DATASENTINEL_HOST, DATASENTINEL_USER and "
            "DATASENTINEL_PASSWORD so a new token can be minted."
        )
    # verify=False: the platform serves a self-signed certificate, so the
    # connection is encrypted but the server is not authenticated.
    resp = httpx.post(
        f"https://{host}/ds-api/user-token",
        auth=(user, password),
        verify=False,
        timeout=30.0,
    )
    resp.raise_for_status()
    token = resp.json()["user-token"]

    TOKEN_PATH.write_text(token)
    TOKEN_PATH.chmod(stat.S_IRUSR | stat.S_IWUSR)  # 0600, owner only
    print(f"Minted a new Datasentinel user-token in {TOKEN_PATH}", file=sys.stderr)
    return token


if __name__ == "__main__":
    token = TOKEN_PATH.read_text().strip() if TOKEN_PATH.exists() else ""
    if not token or token_expired(token):
        token = mint_token()
    print(token)

It needs Python 3.10 or later and the httpx package (pip install httpx), plus the three environment variables so it can mint:

Run it at the start of each scheduled analysis, or on its own schedule shorter than the 24-hour lifetime (for example every 12 hours):

The scheduled job then reads the token from ~/.datasentinel_token (or captures the script's stdout) and passes it to its MCP client as the Authorization: Bearer header, for example --header "Authorization: Bearer $(python refresh_token.py)" with Claude Code. Keep the minting credentials (DATASENTINEL_USER / DATASENTINEL_PASSWORD) in environment variables or your scheduler's secret store, never in the script itself.

Last updated