Architecture
All API requests pass through a gateway that validates credentials before forwarding to backend services. The gateway delegates authentication and quota checking to an internal auth & billing service, ensuring every request is verified in a single hop. When authentication fails, the gateway returns an error directly (401 Unauthorized, or 402 Payment Required when x402 is enabled) without hitting the backend.Three Authentication Methods
ChainStream supports three credential types, evaluated in this order:| Priority | Method | Header | Best For |
|---|---|---|---|
| 1 | Wallet Signature (SIWX) | Authorization: SIWX <token> | AI agents with on-chain wallets (x402 subscribers) |
| 2 | API Key | X-API-KEY: <key> | Applications, scripts, CLI, MCP Server |
| 3 | JWT Bearer Token | Authorization: Bearer <jwt> | Dashboard apps using OAuth 2.0 Client Credentials |
If no valid credentials are found and x402 is enabled, the gateway returns HTTP 402 Payment Required with a pointer to
/x402/purchase. This allows AI agents to auto-purchase a subscription.Method 1: API Key (Recommended)
The simplest authentication method. Create an API Key in the Dashboard and pass it in theX-API-KEY header.
Get an API Key
Login to Dashboard
Visit ChainStream Dashboard and login
Use the API Key
- cURL
- SDK
- CLI
- MCP Server
How It Works
- The gateway extracts the
X-API-KEYheader - The auth service validates the key against the database
- On success, the request is forwarded with the associated organization and permission context
- The key must be
activeand not expired
Method 2: JWT Bearer Token (OAuth 2.0)
For applications that use the OAuth 2.0 Client Credentials flow. Exchange your Client ID and Client Secret for a JWT access token.Generate Access Token
- cURL
- JavaScript
- Python
Use the Token
How It Works
- The gateway extracts the
Authorization: Bearer <jwt>header - The auth service validates the JWT signature, issuer, and audience
- The
client_idclaim is resolved to an organization for quota tracking
Token Details
- Validity: 24 hours by default
- Algorithm: RS256
- Issuer:
https://dex.asia.auth.chainstream.io/ - Audience:
https://api.dex.chainstream.io
Scope Permissions
Certain endpoints require specific scopes:| Scope | Description | Applicable Endpoints |
|---|---|---|
webhook.read | Webhook read access | Query Webhook configuration |
webhook.write | Webhook write access | Create/modify/delete Webhooks |
kyt.read | KYT read access | Query risk assessment results |
kyt.write | KYT write access | Submit transactions/addresses for risk assessment |
If no scope is specified, the token can access all general API endpoints. Scope is only required for Webhook and KYT endpoints.
Method 3: Wallet Signature (SIWX)
For AI agents with on-chain wallets that have purchased a subscription via x402 payment. Uses the Sign-In with X (SIWX) standard (EIP-4361 for EVM, equivalent for Solana).How It Works
- The agent constructs a standard sign-in message with domain, address, nonce, and expiration
- The agent signs the message with their wallet private key
- The signed token is sent as
Authorization: SIWX base64(message).signature - The auth service verifies the signature and checks for a valid x402 subscription
- If a valid, non-expired subscription exists, authentication succeeds
Token Format
Supported Chains
| Chain | Address Format | Signature Type |
|---|---|---|
| EVM (Base, Ethereum) | 0x prefixed, 40 hex chars | EIP-191 personal_sign |
| Solana | Base58 encoded, 32-44 chars | Ed25519 |
SDK Usage
SIWX authentication requires an active x402 subscription. If the subscription has expired, the request will be rejected. See x402 Payment for purchasing a subscription.
WebSocket Authentication
WebSocket connections use the same three authentication methods. The gateway:- Detects WebSocket upgrade requests
- Validates credentials before allowing the handshake
- Tracks the session for usage metering
- Reports usage metrics (bytes transferred, duration) on disconnect
Authentication Priority
When multiple credentials are present in a single request, they are evaluated in this order:- SIWX — if
Authorizationheader starts withSIWXand x402 is configured - API Key — if
X-API-KEYheader is present - JWT Bearer — if
Authorizationheader starts withBearer - 402 Payment Required — if no credentials match and x402 is enabled
API Endpoints
| Service | URL |
|---|---|
| Mainnet API | https://api.chainstream.io/ |
| WebSocket | wss://realtime-dex.chainstream.io/connection/websocket |
| Auth Service (OAuth) | https://dex.asia.auth.chainstream.io/ |
| x402 Pricing | https://api.chainstream.io/x402/pricing |
| x402 Purchase | https://api.chainstream.io/x402/purchase |
Choosing an Auth Method
API Key
Best for: Applications, scripts, CLI, MCP ServerSimplest setup. Create in Dashboard, pass as header. No token refresh needed.
JWT Bearer
Best for: Dashboard apps, server-to-serverStandard OAuth 2.0 flow. Supports scoped permissions. 24h token TTL.
SIWX Wallet
Best for: AI agents with on-chain walletsWallet-native auth via x402 subscription. No API key management needed.
FAQ
Which method should I use?
Which method should I use?
API Key is recommended for most use cases. It’s the simplest to set up and works with all ChainStream products (SDK, CLI, MCP Server). Use JWT if you need OAuth 2.0 integration with scoped permissions. Use SIWX if you’re building an AI agent with its own wallet and want to pay via x402.
What if the token expires?
What if the token expires?
For JWT: generate a new token using your Client ID and Client Secret. For SIWX: sign a new message with a future expiration time. API Keys do not expire unless you set an expiration date in the Dashboard.
Can I use multiple auth methods?
Can I use multiple auth methods?
Only one method is evaluated per request. If you send both
X-API-KEY and Authorization: Bearer, the API Key takes priority (SIWX > API Key > JWT).What is the 402 Payment Required response?
What is the 402 Payment Required response?
When no valid credentials are found and x402 is enabled, the gateway returns HTTP 402 with instructions to purchase a subscription at
/x402/purchase. This enables AI agents to auto-purchase access. See x402 Payment.How do I revoke credentials?
How do I revoke credentials?
API Key: Delete the App in the Dashboard. The key is invalidated immediately. JWT: Revoke the Client ID/Secret in the Dashboard. SIWX: The subscription expires naturally; there is no manual revocation.

