Skip to main content

Authentication

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.

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

  1. Set it from an environment variable (recommended)
  2. Hardcode it in your code

On startup, the SDK will look for the environment variable. If found, it will create a new default client and use the token for all requests. If the environment variable is not found on start-up, you will need to create a client manually and set the token at runtime.

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 hardcode it in your code, 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, build and 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.

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.

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).

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.

import (
"fmt"
"log"

api "github.com/MarketDataApp/sdk-go"
)

func main() {
// No need to set a token here, the SDK will read it
// from the environment variable on startup.

// Initialize a new request, set the symbol parameter, make the request.
quotes, err := api.StockQuote().Symbol("SPY").Get()
if err != nil {
log.Fatalf("Failed to get stock quotes: %v", err)
}

// Loop over the quotes and print them out.
for _, quote := range quotes {
fmt.Println(quote)
}
}

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 Manually and Assign a Token

If you decide to create a client manually and assign a token at runtime, you simply embed the token in your code somewhere and pass it to the NewClient function. This is not recommended for production code.

Example Code


package main

import (
"log"
"fmt"

api "github.com/MarketDataApp/sdk-go"
)

func main() {
// replace with your token
token := "your_token_here"

// create a new client and set the token
client, err := api.NewClient(token)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}

// make a test request to SPY
quotes, err := api.StockQuote().Symbol("SPY").Get()
if err != nil {
log.Fatalf("Failed to get stock quotes: %v", err)
}

// print out the response
for _, quote := range quotes {
fmt.Println(quote)
}
}

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 works, so you can get familiar with interacting with the SDK. The SDK includes structs for each endpoint as well as methods that allow you to retrieve data. Use any of the endpoints in the SDK to make requests to the API.