alpacahq / alpaca-py

The Official Python SDK for Alpaca API
https://alpaca.markets/sdks/python/getting_started.html
Apache License 2.0
505 stars 125 forks source link

feat: detailed error elated to API key and environment mismatch #391

Open hiohiohio opened 7 months ago

hiohiohio commented 7 months ago

Context:

Changes:

Examples:

trading api

from alpaca.trading.client import TradingClient
from alpaca.common.exceptions import APIError

api_key=BROKER_KEY
secret_key=BROKER_SECRET
# paper = True
paper = False

trader = TradingClient(api_key, secret_key, paper=paper)
trader.get_account()

output

APIError: {'payload': '{"code":40110000,"message":"request is not authorized"}', 'status_code': 401, 'reason': 'Unauthorized', 'method': 'GET', 'url': 'https://api.alpaca.markets/v2/account', 'tips': 'please check your API key and environment (paper/sandbox/live)'}

broker api

from alpaca.broker.client import BrokerClient
api_key = BROKER_API_KEY + "1"  # to raise error
client = BrokerClient(api_key=api_key, secret_key=BROKER_API_SECRET, sandbox=True)
client.list_accounts()

output

APIError: {'payload': '{"message": "forbidden."}\n', 'status_code': 403, 'reason': 'Forbidden', 'method': 'GET', 'url': 'https://broker-api.sandbox.alpaca.markets/v1/accounts', 'tips': 'please check your API key and environment (paper/sandbox/live)'}