Live Account 401 Unauthorized Algo Trader Plus Active, Keys Correct

Hi everyone,

Im stuck on a 401 unauthorized error with my live Alpaca account and could really use some help. Paper trading works fine. My algo Trader Plus subscription is active and paid. My account is also funded. The problem accrued once i switched to live trading.

There are exactly three reasons a 401 error can occur

  • Incorrect API key
  • Incorrect secret
  • Incorrect or mismatched base URL

If you are getting a 401 error it is because of one of those three things.

The first two our self-explanatory, the base URL issue occurs when using a live account API key (which always begins with AK) to access a paper account endpoint havng a base URL of http://paper-api.alpaca.markets, or conversely using a paper account API key (which always begins with PK) to access a live account endpoint having a base URL of http://api.alpaca.markets. Make certain the account keys match the account endpoint.

To begin troubleshooting ensure your keys are correct. An easy way to do this is using the /accounts endpoint in the documentation. The documentation includes an interactive API builder. Go to the /accounts endpoint page, enter your API key and secret in the credentials section, and select the associated base URL. Then click “Try It”. If that works you know your API key and secret is correct. If not, they are not correct. Regenerate your keys and try again.

Once you have validated your keys, store them in a safe place. Never regenerate your keys unless you feel they have been compromised or you have lost your secret. Having multiple keys will only ever cause you confusion.

The next step in troubleshooting is to verify you are actually sending the API key, secret, and base URL you expect. Print out those values immediately before sending your request. Ensure you are actually using the API keys and base URL you believe to be true. The majority of reasons for a 401 error are because you believe you are sending the correct API keys but actually are not. This happens often if loading keys from an environment variable or system file. Those typically do not update immediately and can therefore contain old values.

If you have validated your keys, and are certain those are the keys and base URL in your requests, but still getting a 401 error, that implies your request is somehow being altered. This is very very very rare, but there are instances where header encryption or minimization tools will change these values. Deactivate any such tools.

If you are still having issues with a 401 error, Alpaca can look at the ingess logs for details which may help troubleshoot. Ideally provide the IP address of your request and an approximate time including time zone that you received the error.