Authentication
End-to-end guide for authenticating GraphQL subscriptions with the GoldRush Streaming API.
This document provides a comprehensive overview of the authentication process for GraphQL subscriptions with the Streaming API. It covers the rationale for authentication, how to obtain and use an API key, client-side implementation examples, server-side validation, error handling, and frequently asked questions.
Why is Authentication Required?
Authentication is essential to ensure that only authorized users can access premium or rate-limited real-time data streams. All clients must present a valid GoldRush API key to interact with the GraphQL subscription endpoints.
1. Obtaining a GoldRush API Key
To begin, register for an API key at GoldRush API Dashboard. This key will be required for all authenticated requests to the Streaming API.
2. Supplying the API Key from the Client
Use the GoldRush Client SDK (recommended)
The recommended approach is to use the official GoldRush TypeScript Client SDK which handles authentication automatically and provides a simplified interface for managing stream subscriptions.
The SDK automatically handles:
- WebSocket connection management
- API key authentication
- Connection retry logic
- Error handling
- Type safety for parameters
Use a GraphQL WebSocket Client
For direct WebSocket connections, you can use a package like graphql-ws library. Below is an example implementation in Node.js:
Note: The API key must be provided as
GOLDRUSH_API_KEY
within theconnectionParams
object.
Use websocat
for Manual WebSocket Testing
For manual testing or debugging, websocat can be used to interact with the WebSocket endpoint directly from the command line.
Step 1: Initiate the WebSocket Connection
Step 2: Send the Connection Initialization Payload
Send the following JSON as your first message, replacing <YOUR_API_KEY>
with your actual API key:
Wait for a connection_ack
response from the server before proceeding.
Step 3: Submit a Subscription Query
You may now send a subscription query, for example:
If authentication is successful, you will begin receiving real-time data for your subscription.
3. Server-Side Authentication Process
- The server does not validate the API key at the time of WebSocket connection establishment.
- Instead, authentication is enforced at the start of each GraphQL subscription resolver.
- If the API key is missing or invalid, the subscription will immediately terminate with an error message.
4. Error Handling
If authentication fails, the client will receive a GraphQL error with one of the following codes:
MISSING_TOKEN
: No API key was provided in theconnection_init
payload.INVALID_TOKEN
: The provided API key is malformed or invalid.AUTH_SYSTEM_ERROR
: An internal server error occurred during authentication.
Example Error Response:
5. Frequently Asked Questions (FAQ)
- What is the client experience if the key is invalid?
- The client will always receive a
connection_ack
response, even if the API key is invalid. Authentication errors are only reported when a subscription is initiated.
- The client will always receive a
- Where is the API key expected?
- The API key is expected in the
payload
of theconnection_init
message with the keyGOLDRUSH_API_KEY
.
- The API key is expected in the
- What API key formats are supported?
cqt_wF[26 base58 chars]
orcqt_rQ[26 base58 chars]
- Why should I use the Client SDK?
- The Client SDK provides automatic authentication, connection management, retry logic, type safety, and a simplified API for all streaming operations.