|
| 1 | +# Authentication |
| 2 | + |
| 3 | +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](https://www.marketdata.app/dashboard/). |
| 4 | + |
| 5 | +There are three ways to set your token when using the Java SDK: |
| 6 | + |
| 7 | +1. Set it from an environment variable _(recommended for production)_ |
| 8 | +2. Load it from a `.env` file _(recommended for local development)_ |
| 9 | +3. Pass it directly when creating the [client](./client.md) |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +> [!TIP] |
| 14 | +> 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. |
| 15 | +
|
| 16 | +## How To Set Up The Environment Variable |
| 17 | + |
| 18 | +### Set The Environment Variable In The Console |
| 19 | + |
| 20 | +### Mac / Linux |
| 21 | + |
| 22 | +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. |
| 23 | + |
| 24 | +```bash |
| 25 | +export MARKETDATA_TOKEN="your_api_token" |
| 26 | +``` |
| 27 | + |
| 28 | +#### Make The Variable Persistent |
| 29 | + |
| 30 | +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). |
| 31 | + |
| 32 | +### Windows |
| 33 | + |
| 34 | +`setx` sets the variable permanently, but it is not available in the current Command Prompt session — open a new one after running it. |
| 35 | + |
| 36 | +```bash |
| 37 | +setx MARKETDATA_TOKEN "your_api_token" |
| 38 | +``` |
| 39 | + |
| 40 | +### Using a .env File |
| 41 | + |
| 42 | +The SDK automatically loads a `.env` file from your working directory at startup. Create a file named `.env` in your project root: |
| 43 | + |
| 44 | +```env title=".env" |
| 45 | +MARKETDATA_TOKEN=your_api_token |
| 46 | +``` |
| 47 | + |
| 48 | +> [!WARNING] |
| 49 | +> Add `.env` to your `.gitignore` so the token is not committed to source control. |
| 50 | +
|
| 51 | +### Make A Test Request |
| 52 | + |
| 53 | +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. |
| 54 | + |
| 55 | +### Java |
| 56 | + |
| 57 | +```java |
| 58 | +import com.marketdata.sdk.MarketDataClient; |
| 59 | +import com.marketdata.sdk.stocks.StockQuoteRequest; |
| 60 | +import com.marketdata.sdk.exception.AuthenticationError; |
| 61 | + |
| 62 | +// No need to pass a token here — the SDK reads MARKETDATA_TOKEN automatically. |
| 63 | +try (MarketDataClient client = new MarketDataClient()) { |
| 64 | + var quote = client.stocks().quote(StockQuoteRequest.of("SPY")).values().get(0); |
| 65 | + System.out.println(quote.symbol() + " last=" + quote.last()); |
| 66 | +} catch (AuthenticationError e) { |
| 67 | + System.out.println("Authentication failed: " + e.getMessage()); |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### Kotlin |
| 72 | + |
| 73 | +```kotlin |
| 74 | +import com.marketdata.sdk.MarketDataClient |
| 75 | +import com.marketdata.sdk.stocks.StockQuoteRequest |
| 76 | +import com.marketdata.sdk.exception.AuthenticationError |
| 77 | + |
| 78 | +MarketDataClient().use { client -> |
| 79 | + try { |
| 80 | + val quote = client.stocks().quote(StockQuoteRequest.of("SPY")).values()[0] |
| 81 | + println("${quote.symbol()} last=${quote.last()}") |
| 82 | + } catch (e: AuthenticationError) { |
| 83 | + println("Authentication failed: ${e.message}") |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +## Passing the Token Directly |
| 89 | + |
| 90 | +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. |
| 91 | + |
| 92 | +### Java |
| 93 | + |
| 94 | +```java |
| 95 | +import com.marketdata.sdk.MarketDataClient; |
| 96 | + |
| 97 | +try (MarketDataClient client = |
| 98 | + new MarketDataClient("your_token_here", null, null, true)) { |
| 99 | + // ... make requests |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +### Kotlin |
| 104 | + |
| 105 | +```kotlin |
| 106 | +import com.marketdata.sdk.MarketDataClient |
| 107 | + |
| 108 | +MarketDataClient("your_token_here", null, null, true).use { client -> |
| 109 | + // ... make requests |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +> [!NOTE] |
| 114 | +> **Demo mode** |
| 115 | +> |
| 116 | +> 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()`). |
| 117 | +
|
| 118 | +## Next Steps |
| 119 | + |
| 120 | +After successful authentication, read the overview of how the [client](./client.md) works, then configure [Settings](./settings.md) to customize output format, date format, and other universal parameters. |
0 commit comments