spacexai-subscription-client
spacexai-subscription-client is a small, typed async client for SpaceXAI OAuth
device login and subscription inference. It is designed for applications such
as Home Assistant that own OAuth token storage and refresh.
This is an unofficial community package, not xAI's official Python SDK. Authentication uses the SpaceXAI OAuth flow; API-key authentication is outside this package's scope. Internally, the OAuth access token is supplied to the OpenAI SDK's bearer-credential field because the subscription endpoint uses its Responses API transport.
Installation
python -m pip install spacexai-subscription-client
Python 3.12 or newer is required.
Usage
The caller owns both HTTP sessions and is responsible for closing them. Device
authorization returns a code and URL that the application should present to the
user while async_poll_device_token waits for approval.
When retrying a transient polling failure, reuse the same DeviceAuthorization.
It retains the original expiry and the current polling interval, including
server-requested slow-down and exponential timeout backoff.
import aiohttp
import httpx
from spacexai_subscription_client import SpaceXAISubscriptionClient, Message
async def connect() -> None:
async with (
aiohttp.ClientSession() as websession,
httpx.AsyncClient() as http_client,
):
client = SpaceXAISubscriptionClient(websession, http_client)
authorization = await client.async_request_device_authorization()
print(authorization.verification_uri_complete)
print(authorization.user_code)
token = await client.async_poll_device_token(authorization)
access_token = token.data["access_token"]
account = await client.async_get_account(access_token)
models = await client.async_list_models(access_token)
if not models:
raise RuntimeError("No Grok models are available for this account")
response = await client.async_create_response(
access_token,
model=models[0],
input_data=[Message("user", f"Hello, {account.display_name}")],
tools=[],
)
print(response.text)
OAuthToken.as_dict() returns a mutable copy suitable for caller-owned
persistence. Refresh is intentionally not implemented by this package; the
host application should refresh tokens through its OAuth framework and pass the
current access token to each request.
API boundary
The package exposes provider-neutral dataclasses for messages, tools, tool
calls, tool results, accounts, and completions. Provider SDK types do not cross
the public boundary. Network failures are translated into the stable exception
hierarchy rooted at SpaceXAISubscriptionError.
Model discovery and response generation use explicit timeouts. SDK retries are disabled so callers receive a single stable failure and can apply their own retry policy without duplicating a response request.
The OAuth client identity and provider endpoints are centralized in
spacexai_subscription_client.const so an upstream identity decision can be
adopted without changing the public client API.
Protocol provenance
The OAuth device flow, scopes, client identity, subscription proxy, and Responses transport track the current public Grok Build authentication guide and Grok Build source. Keeping this reference explicit makes upstream protocol changes reviewable without depending on or executing the Grok CLI.
Development
uv sync --locked
uv run --no-sync ruff check .
uv run --no-sync ruff format --check .
uv run --no-sync mypy --strict src/spacexai_subscription_client
uv run --no-sync pytest --cov=spacexai_subscription_client --cov-fail-under=95
Releases are built and published from the public GitHub Actions workflow using PyPI trusted publishing. See RELEASING.md for the release checklist and CHANGELOG.md for release notes.
Metadata
Release files for spacexai-subscription-client 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| spacexai_subscription_client-0.1.0.tar.gz | 117.2 kB | With provenance | Details for spacexai_subscription_client-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| spacexai_subscription_client-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for spacexai_subscription_client-0.1.0-py3-none-any.whl |
Total release size: 131.2 kB
Release files / spacexai_subscription_client-0.1.0.tar.gz
| Download URL | spacexai_subscription_client-0.1.0.tar.gz |
|---|---|
| Size | 117.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6f6a44acc6de85f27d2a2fba1a07106b39802da6e75b0102c42a8e0e7b09915b
|
|
BLAKE2b-256 checksum How to use checksums |
9148ff3d7bbc24d5c00212eb0fe5571b47e37a7a549b9f176a49f21e27dce273
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 8, 2026.
Transparency logRelease files / spacexai_subscription_client-0.1.0-py3-none-any.whl
| Download URL | spacexai_subscription_client-0.1.0-py3-none-any.whl |
|---|---|
| Size | 14.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2d7e8d92087afc3e72ec5839e93631284c378acd4e21121cbc60dc473c767b32
|
|
BLAKE2b-256 checksum How to use checksums |
54ec46786012bd138b4c25a8be9987c08f652fb42849c411990ddf1c74b26ed2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 8, 2026.
Transparency logSecurity for spacexai-subscription-client 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for spacexai_subscription_client-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 8, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.