Authentication
The Market Data API uses a Bearer Token for authentication. The token is required for almost every request. Your token should have been e-mailed to you when you first signed up for an account. If you do not have a token or have lost your sign-up email, request a new token from the Market Data Dashboard.
There are three ways to set your token when using the Java SDK:
- Set it from an environment variable (recommended for production)
- Load it from a
.envfile (recommended for local development) - Pass it directly when creating the client
On startup, the SDK looks for the MARKETDATA_TOKEN environment variable. If found, it uses that token for all requests. The SDK also loads a .env file automatically from the working directory if one is present.
When your code is running in a production environment, we recommend using an environment variable to ensure your token is not stored with your code. This is the most secure way to set your token.
How To Set Up The Environment Variable
Set The Environment Variable In The Console
- Mac / Linux
- Windows
This command sets the environment variable for the current session only. If you open a new terminal or restart your computer, it will not persist.
export MARKETDATA_TOKEN="your_api_token"
Make The Variable Persistent
Add the export line to your shell's profile script (~/.zshrc, ~/.bashrc, ~/.bash_profile, etc.), then restart your terminal or run source ~/.zshrc (adjusting for your shell).
setx sets the variable permanently, but it is not available in the current Command Prompt session — open a new one after running it.
setx MARKETDATA_TOKEN "your_api_token"
Using a .env File
The SDK automatically loads a .env file from your working directory at startup. Create a file named .env in your project root:
MARKETDATA_TOKEN=your_api_token
Add .env to your .gitignore so the token is not committed to source control.
Make A Test Request
Verify your authentication is working by making a test request against SPY (or any symbol that requires authentication). Do not use AAPL to test authentication — AAPL is a free test symbol and returns data even when you are not authenticated.
- Java
- Kotlin
import com.marketdata.sdk.MarketDataClient;
import com.marketdata.sdk.stocks.StockQuoteRequest;
import com.marketdata.sdk.exception.AuthenticationError;
// No need to pass a token here — the SDK reads MARKETDATA_TOKEN automatically.
try (MarketDataClient client = new MarketDataClient()) {
var quote = client.stocks().quote(StockQuoteRequest.of("SPY")).values().get(0);
System.out.println(quote.symbol() + " last=" + quote.last());
} catch (AuthenticationError e) {
System.out.println("Authentication failed: " + e.getMessage());
}
import com.marketdata.sdk.MarketDataClient
import com.marketdata.sdk.stocks.StockQuoteRequest
import com.marketdata.sdk.exception.AuthenticationError
MarketDataClient().use { client ->
try {
val quote = client.stocks().quote(StockQuoteRequest.of("SPY")).values()[0]
println("${quote.symbol()} last=${quote.last()}")
} catch (e: AuthenticationError) {
println("Authentication failed: ${e.message}")
}
}
Passing the Token Directly
If you prefer to pass the token explicitly (not recommended for production code), use the four-argument constructor (apiKey, baseUrl, apiVersion, validateOnStartup). Pass null for any slot you want resolved from the cascade or left at its default.
- Java
- Kotlin
import com.marketdata.sdk.MarketDataClient;
try (MarketDataClient client =
new MarketDataClient("your_token_here", null, null, true)) {
// ... make requests
}
import com.marketdata.sdk.MarketDataClient
MarketDataClient("your_token_here", null, null, true).use { client ->
// ... make requests
}
If no token is found anywhere in the cascade, the SDK runs in demo mode — startup validation is skipped and you can call the free, public endpoints (such as AAPL quotes and utilities().status()).
Next Steps
After successful authentication, read the overview of how the client works, then configure Settings to customize output format, date format, and other universal parameters.