# Authentication

The Market Data API uses a **Bearer Token** for authentication. The token is a programmatic representation of your username and password credentials, so you must keep it secret just as you would your username and password. The token is required for each request you make to the API.

> [!CAUTION]
> **Accept HTTP 203 as success**
>
> Any endpoint may return HTTP `203 Non-Authoritative Information` instead of `200 OK` when the response is served from our caching tier. The body is identical in shape — treat 203 exactly the same as 200. Code samples below that use raw HTTP libraries (rather than our SDKs) must accept both status codes; checking only `status == 200` will silently drop valid responses. See [Troubleshooting](https://www.marketdata.app/docs/api/troubleshooting) for the full list of status codes.

## Obtaining a Token

To obtain it, sign-in to your customer dashboard using your username and password and request a token. It will be delivered by email to the address you used to sign-in.  

## Using the Token

There are two ways to pass this token to the API with your requests:

1. Header Authentication
2. URL Parameter Authentication

> [!TIP]
> We recommend using header-based authentication to ensure your token is not stored or cached. While Market Data makes a conscientious effort to delete tokens from our own server logs, we cannot guarantee that your token will not be stored by any of our third party cloud infrastructure partners.

## Header Authentication

Add the token to the ```Authorization``` header using the word ```Bearer```. 

### Code Examples

### HTTP

```http
GET /v1/stocks/quotes/SPY/ HTTP/1.1
Host: api.marketdata.app
Accept: application/json
Authorization: Bearer {token}
```

> [!TIP]
> The curly braces around token are a placeholder for this example. Do not actually wrap your token with curly braces.

### JavaScript

```js title="app.js"
import { MarketDataClient } from "@marketdata/sdk";

// The SDK reads your token from the MARKETDATA_TOKEN environment variable.
// Alternatively, pass it directly: new MarketDataClient({ token: "your_token_here" })
const client = new MarketDataClient();

try {
  const quotes = await client.stocks.quotes("SPY");
  console.log(quotes);
} catch (error) {
  console.error(error);
}
```

For more information about using the JavaScript SDK, see our [JavaScript SDK documentation](https://www.marketdata.app/docs/sdk/js) and [authentication guide](https://www.marketdata.app/docs/sdk/js/authentication).

### TypeScript

```typescript title="app.ts"
import { MarketDataClient } from "@marketdata/sdk";
import type { StockQuote } from "@marketdata/sdk";

// The SDK reads your token from the MARKETDATA_TOKEN environment variable.
// Alternatively, pass it directly: new MarketDataClient({ token: "your_token_here" })
const client = new MarketDataClient();

try {
  const quotes: StockQuote[] = await client.stocks.quotes("SPY");
  console.log(quotes);
} catch (error) {
  console.error(error);
}
```

For more information about using the JavaScript SDK, see our [JavaScript SDK documentation](https://www.marketdata.app/docs/sdk/js) and [authentication guide](https://www.marketdata.app/docs/sdk/js/authentication).

### Python

```python
from marketdata import MarketDataClient

# Initialize the client (token is read from MARKETDATA_TOKEN environment variable)
# Or pass it directly: client = MarketDataClient(token="your_token_here")
client = MarketDataClient()

# Get stock quotes for SPY
quotes = client.stocks.quotes("SPY")
print(quotes)
```

For more information about using the Python SDK, see our [Python SDK documentation](https://www.marketdata.app/docs/sdk/py) and [authentication guide](https://www.marketdata.app/docs/sdk/py/authentication).

### Go

```go
// Import the Market Data SDK
import api "github.com/MarketDataApp/sdk-go"

func main() {
    // Create a new Market Data client instance
    marketDataClient := api.New()

    // Set the token for authentication
    // Replace "your_token_here" with your actual token
    marketDataClient.Token("your_token_here")

    // Now the client is ready to make authenticated requests to the Market Data API
    
    // Use the client to create a StockQuoteRequest
	sqr, err := api.StockQuote(marketDataClient).Symbol("SPY").Get()
    if err != nil {
		fmt.Println("Error fetching stock quotes:", err)
		return
	}

	// Process the retrieved quote
	for _, quote := range quotes {
		fmt.Printf(quote)
	}
}
```

### Java

```java title="App.java"
import com.marketdata.sdk.MarketDataClient;
import com.marketdata.sdk.stocks.StockQuoteRequest;

public class App {
  public static void main(String[] args) {
    // The no-arg constructor reads your token from the MARKETDATA_TOKEN
    // environment variable (or a .env file in the working directory).
    // Alternatively, pass it directly:
    //   new MarketDataClient("your_token_here", null, null, true)
    try (MarketDataClient client = new MarketDataClient()) {
      client.stocks().quote(StockQuoteRequest.of("SPY")).values().forEach(System.out::println);
    }
  }
}
```

### Kotlin

```kotlin title="App.kt"
import com.marketdata.sdk.MarketDataClient
import com.marketdata.sdk.stocks.StockQuoteRequest

fun main() {
    // The no-arg constructor reads your token from the MARKETDATA_TOKEN
    // environment variable (or a .env file in the working directory).
    // Alternatively, pass it directly:
    //   MarketDataClient("your_token_here", null, null, true)
    MarketDataClient().use { client ->
        client.stocks().quote(StockQuoteRequest.of("SPY")).values().forEach(::println)
    }
}
```

### C#

```csharp title="Program.cs"
using MarketDataApp;

// With no options supplied, the SDK reads your token from the MARKETDATA_TOKEN
// environment variable (or a .env file / user secrets) and validates it on startup.
// Alternatively, pass it directly:
//   await MarketDataClient.CreateAsync(new MarketDataClientOptions { ApiToken = "your_token_here" })
using var client = await MarketDataClient.CreateAsync();

foreach (var quote in (await client.Stocks.GetQuoteAsync("SPY")).Values)
{
    Console.WriteLine(quote);
}
```

## URL Parameter Authentication

Add the token as a variable directly in the URL using the format ```token=YOUR_TOKEN_HERE```. For example:

```
https://api.marketdata.app/v1/stocks/quotes/SPY/?token={token}
```

> [!TIP]
> The curly braces around token are a placeholder for this example. Do not actually wrap your token with curly braces.

## Demo The API With No Authentication

You can try stock and option endpoints with several different symbols that are unlocked and do not require a token. Please be aware that only historical data for these tickers is available without a token.

- Try any stock endpoint with **AAPL**, no token required.
- Try any option endpoint with any AAPL contract, for example: **AAPL271217C00250000**. No token required.

## IP Address Restrictions

Each account may only connect from one IP address at a time. You can switch devices, but you cannot use two devices simultaneously. Back-and-forth switching between IP addresses within a 5-minute window triggers a temporary block.

See the [Single IP Address Policy](https://www.marketdata.app/docs/docs/account/data-policies/single-ip/) for full details, or [403: Multiple IP Addresses](https://www.marketdata.app/docs/docs/api/troubleshooting/multiple-ip-addresses/) if your account is blocked.
