Before you start
- An email address and a phone for two-factor authentication
- Nothing to fund: paper trading needs no deposit
- For live keys only: a completed and approved brokerage application
Create your Alpaca API key
- 1
Create an Alpaca account
Go to alpaca.markets and sign up with your email. Verify the address from the confirmation email. You can stop here for paper trading; the brokerage application is only needed for live keys.
- 2
Switch to your paper account
Open the dashboard at
app.alpaca.markets. Use the account switcher at the top left to select your Paper account (its id starts withPA). - 3
Generate the key pair
On the paper account home page find the API Keys panel on the right and click Generate New Keys (or Regenerate if a pair exists; that revokes the old one).
- 4
Copy both values immediately
Copy the Key (key id, starts with
PKfor paper) and the Secret. The secret is shown only once; if you lose it, regenerate the pair and update everywhere it is used. - 5
Repeat for live trading later
Once your brokerage account is approved and funded, switch to the Live account and generate a separate pair there (key ids start with
AK). Live keys only work againstapi.alpaca.markets.
Test the key
Before wiring the key into a strategy, make one request by hand. It separates “the key is wrong” from “my code is wrong”.
curl -s https://paper-api.alpaca.markets/v2/account \
-H "APCA-API-KEY-ID: $ALPACA_API_KEY_ID" \
-H "APCA-API-SECRET-KEY: $ALPACA_API_SECRET_KEY"status: "ACTIVE", cash and equity means the key works. 401/403 usually means a live key against the paper URL or vice versa.Add the key to your AlgoGP project
- 1
Open your project’s APIs & keys tab
Open the strategy project, select APIs & keys, and under Platforms choose Alpaca.
- 2
Paste each value
Paste the values into
ALPACA_API_KEY_IDandALPACA_API_SECRET_KEYand click Save. Values are encrypted at rest, never shown again and never written into code or exports. - 3
Run locally with the same names
Exported projects read the same names from
.env(copy.env.example). Keep.envout of version control.
ALPACA_API_KEY_ID=paste-your-value-here
ALPACA_API_SECRET_KEY=paste-your-value-here
# ALPACA_BASE_URL=Keep the key safe
- Never paste the secret into code, a notebook you share, or
config.json. Store it in AlgoGP Keys & secrets or a local.envthat is git-ignored. - Use paper keys for development. Only put live keys on the machine that actually runs
live.py. - If a secret leaks, click Regenerate in the dashboard at once: the old pair stops working immediately.
Troubleshooting
401 Unauthorized- Wrong key id or secret, or a regenerated pair. Copy both again.
403 Forbidden- Paper key against the live URL (or the reverse), or the account is not yet approved for live trading.
422on an order- The order itself is invalid: e.g. a fractional short, a crypto order with
daytime-in-force, or insufficient buying power. - No data for a stock
- The free data plan uses the IEX feed; thinly traded symbols can have gaps. Request
feed=iexexplicitly.
Frequently asked questions
Why can I not see my Alpaca secret key again?
Alpaca shows the secret once, at generation time, and does not store a retrievable copy. Regenerate the pair to get a new secret.
Do paper and live trading use the same Alpaca API key?
No. Each account has its own key pair and base URL. Paper key ids start with PK, live with AK.
Does the Alpaca key also work for market data?
Yes. The same headers authenticate data.alpaca.markets, within the limits of your data plan.
Build an Alpaca trading bot in minutes
Describe the strategy in plain English, backtest it on years of data, then export Python that already talks to Alpaca: paper, sandbox and live modes, keys handled for you.
- Generated adapter for Alpaca orders and balances
- Backtests with costs, slippage and walk-forward checks
- Paper first; live trading only after you opt in
Free plan, no card required.
AlgoGP is not affiliated with Alpaca. Menus get renamed from time to time; if a label differs, the official documentation has the current path.