---
title: Authentication
sidebar_position: 2
---

import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

The Market Data API uses a **Bearer Token** for authentication. The token is required for each request you make to the API. 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/). 

There are two ways to set your token when using the Python SDK:

1. Set it from an environment variable _(recommended)_
2. Pass it directly when creating the [client](/sdk/py/client)

On startup, the SDK will look for the environment variable. If found, it will use the token for all requests. If the environment variable is not found, you will need to pass the token when creating the [client](/sdk/py/client).

:::tip
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 token in the environment variable `MARKETDATA_TOKEN`. Alternatively, you can pass it directly when creating the client, but please be aware that this is not secure and could pose a risk if your code is shared.

### Set The Environment Variable In The Console

After setting the variable, run your code to make a test request. If you have set the variable correctly, you should see the output of the test request. If you see an error, double-check that you have set the variable correctly.

<Tabs>
<TabItem value="MacLinux" label="Mac / Linux">

This command should be run in the terminal. It sets the environment variable for the current session only. If you open a new terminal window or restart your computer, the environment variable will not persist.

```bash
export MARKETDATA_TOKEN="your_api_token"
```

#### Verify the Variable

To verify that the MARKETDATA_TOKEN environment variable has been set properly on Mac OS or Linux, follow these steps:

1. Open a terminal.
2. Type the following command and press Enter: `echo $MARKETDATA_TOKEN`
3. If the environment variable has been set correctly, this command will output the value of MARKETDATA_TOKEN to the terminal. If the variable has not been set or an error occurred, the output will be blank, indicating that the variable is not set.

#### Make The Variable Persistent

To make the environment variable persistent across sessions, you need to add the export command to your shell's profile script (e.g., `~/.bash_profile`, `~/.bashrc`, `~/.zshrc`, etc.) depending on the shell you are using.

:::tip

To find out what shell you are using, you can open your terminal and enter the following command: `echo $SHELL` This command will output the path to the current shell being used, such as `/bin/bash`, `/bin/zsh`, or another shell. The last part of the path indicates the shell type (e.g., bash, zsh).

:::

After adding the command to the appropriate profile script, you will need to either restart your terminal or source the profile script with a command like `source ~/.bash_profile` (adjusting the file name as necessary for your shell) to apply the changes.

##### Verify Variable Persistence

If you have made the environment variable persistent by adding it to your shell's profile script (e.g., ~/.bash_profile, ~/.bashrc, ~/.zshrc), you can verify that the variable persists across sessions by:

1. Closing the current terminal.
2. Opening a new terminal.
3. Running the `echo $MARKETDATA_TOKEN` command again to check if the variable is still set.

If the variable is not present after restarting the terminal, ensure you have correctly added the export command to the appropriate profile script and that you have either restarted the terminal or sourced the profile script with a command like `source ~/.bash_profile` (adjusting the file name as necessary for your shell).

</TabItem>
<TabItem value="Windows" label="Windows">

This command should be run in the Command Prompt on Windows. Note that after using setx, the environment variable will be set permanently but will not be available in the current Command Prompt session. You will need to start a new session to see the change.

```bash
setx MARKETDATA_TOKEN "your_api_token"     # Windows
```

#### Verify The Variable

To verify that the MARKETDATA_TOKEN environment variable has been set properly on Windows, the follow these steps:

1. Open a new Command Prompt session (since the setx command does not affect the current session where it was run).
2. Type the following command and press Enter: `echo %MARKETDATA_TOKEN%`
3. If the environment variable has been set correctly, this command will output the value of MARKETDATA_TOKEN to the console. If the variable has not been set or an error occurred, it will likely output `%MARKETDATA_TOKEN%` indicating that the variable substitution did not happen.
4. The next step is to ensure the SDK is correctly utilizing the token. To do this, the use the provided test code to make a request to the Market Data API. If the request is successful and returns the expected data, it confirms that the SDK is correctly reading and using the MARKETDATA_TOKEN environment variable.

</TabItem>
</Tabs>

### Using a .env File

You can also create a `.env` file in your project root:

```env
MARKETDATA_TOKEN=your_api_token
```

The SDK will automatically load this file if you have `python-dotenv` installed.

### Make A Test Request

Use the following test code to verify that your authentication is working by making a test request to SPY or any other symbol that requires authentication. Do not use AAPL to test your authentication because AAPL is a free test symbol and will return data even if you are not authenticated properly.

```python
from marketdata.client import MarketDataClient
from marketdata.exceptions import MarketDataClientErrorResult

# No need to set a token here, the SDK will read it
# from the environment variable automatically.

# Initialize the client (see client documentation for details)
client = MarketDataClient()

# Make a test request
quotes = client.stocks.quotes("SPY")

# Check if the result is an error
if isinstance(quotes, MarketDataClientErrorResult):
    print(f"Error occurred: {quotes.error}")
else:
    # Print the quotes
    print(quotes)
```

If your token is set correctly, you should see the output of the test request. If you see an error, double-check that you have set the token correctly.

## How To Create a Client and Assign a Token Directly

If you decide to pass the token directly when creating the client, you can do so by passing it as a parameter. This is not recommended for production code.

### Example Code

```python
from marketdata.client import MarketDataClient
from marketdata.exceptions import MarketDataClientErrorResult

# Replace with your token
token = "your_token_here"

# Create a new client and set the token
client = MarketDataClient(token=token)

# Make a test request to SPY
quotes = client.stocks.quotes("SPY")

# Check if the result is an error
if isinstance(quotes, MarketDataClientErrorResult):
    print(f"Error occurred: {quotes.error}")
else:
    # Print the quotes
    print(quotes)
```

## Next Steps

After successful authentication, you will be able to begin making requests to the Market Data API. We recommend reading the brief overview of how the [client](/sdk/py/client) works, so you can get familiar with interacting with the SDK. The SDK includes methods for each endpoint that allow you to retrieve data. Use any of the endpoints in the SDK to make requests to the API.

You may also want to configure [Settings](/sdk/py/settings) to customize output format, date format, and other universal parameters.

