> ## Documentation Index
> Fetch the complete documentation index at: https://docs.axioniclabs.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Authenticate the Mechanex SDK with API keys and JWT

> Set up API key and JWT authentication for the Mechanex SDK, persist credentials in the config file, and manage key rotation and token refresh.

## API Keys

API keys authenticate inference requests and SDK calls to the Axionic backend.

**Format**: `ax_` followed by 48 lowercase hex characters (e.g., `ax_a3f9...`).

Get a key from [Spectra Settings → API Keys](/products/spectra/settings) or via the CLI:

```bash theme={null}
mechanex create-api-key
```

Set the key in code:

<ParamField body="api_key" type="string" required>
  Your Axionic API key. Must start with `ax_`.
</ParamField>

<ParamField body="persist" type="boolean" default="false">
  If `true`, saves the key to `~/.mechanex/config.json` so it is auto-loaded on every future SDK init and CLI start.
</ParamField>

```python theme={null}
import mechanex as mx

mx.set_key("ax_your_key_here", persist=True)
```

<Note>
  `persist=True` writes the key to `~/.mechanex/config.json`. Omit or pass `False` to keep the key in memory only for the current process.
</Note>

## Config File

The SDK and CLI auto-load credentials from `~/.mechanex/config.json` at startup.

Fields stored in the config file:

| Field           | Description                             |
| --------------- | --------------------------------------- |
| `api_key`       | Your API key (`ax_...`)                 |
| `access_token`  | JWT access token (set by `mx.login()`)  |
| `refresh_token` | JWT refresh token (set by `mx.login()`) |

You can edit this file directly, but prefer using `mx.set_key(persist=True)` or `mx.login()` to populate it correctly.

## JWT Authentication

Some operations — billing, account management, and organization access — require a JWT rather than an API key. Obtain one by logging in:

<ParamField body="email" type="string" required>
  Your Axionic account email address.
</ParamField>

<ParamField body="password" type="string" required>
  Your account password.
</ParamField>

```python theme={null}
mx.login(email="you@example.com", password="your_password")
```

Tokens are stored automatically in the config file. When a JWT expires, the SDK silently refreshes it via `POST /auth/refresh` and retries the original request — no manual intervention needed.

For key creation, rotation, and account management (balance, user info), use the [CLI](/products/mechanex/cli).
