When to Use
Invoke this whenever a bot connects to the Alpaca Trading API (or any broker with distinct paper/live environments). Connecting a paper strategy to Alpaca's live endpoint (https://api.alpaca.markets) using live API keys — or passing live order signals into a paper endpoint — is a catastrophic operational error. Pinning the base URL per environment, inspecting the key prefix, probing /v2/account for tradability, and requiring an explicit ALLOW_LIVE_TRADING environment variable are all mandatory before any order is submitted.
The base URL is the control that actually separates the environments. Alpaca serves paper accounts from https://paper-api.alpaca.markets and live accounts from https://api.alpaca.markets; a live account is not reachable through the paper host. Every other check in this skill is defence-in-depth layered on that pin — treat them as corroboration, never as a substitute.
When NOT to Use
- Brokers without separate paper/live endpoints: If a single API base URL serves both environments and differentiation is done purely via credentials, this skill's URL-matching logic does not apply. Use broker-specific auth patterns instead (see
headless-broker-auth-patterns).
- Backtesting or simulation engines: When running historical replays or simulated fills with no network calls to a real broker API, environment segregation is irrelevant.
- Non-Alpaca brokers with different credential schemes: The
PK.../AK... prefix convention is Alpaca-specific. Brokers using different key formats (e.g., IBKR account IDs DU.../U...) require their own validation logic. For a broker-agnostic host allow-list, see sandbox-credential-leakage-prevention.
- Read-only market data access: If the integration only consumes market data endpoints (not order routing), live capital loss is not a risk and the order guard is unnecessary.
Prerequisites
- Distinct environment variable names for paper vs live credentials (e.g.
ALPACA_PAPER_KEY_ID vs ALPACA_LIVE_KEY_ID).
- Base URL configuration (
https://paper-api.alpaca.markets for paper vs https://api.alpaca.markets for live).
- Explicit
ALLOW_LIVE_TRADING=true environment flag for live execution mode.
Workflow
Load & Normalise the Environment Mode:
- Coerce the configured environment into a known
PAPER/LIVE value before any comparison. A value matching neither must raise, never fall through — an unrecognised mode that skips both branches is an authorisation, not a no-op.
Inspect Credential Prefixes:
PK... indicates Alpaca paper credentials; AK... indicates live credentials.
- Use this to reject a credential carrying the opposite environment's prefix. Do not require a positive prefix match: the convention is widely observed but is not documented by Alpaca, so an unrecognised key format must not be treated as proof of anything (see
references/standards.md).
Base URL & Mode Verification:
- Match configuration mode against endpoint URLs:
PAPER mode → https://paper-api.alpaca.markets
LIVE mode → https://api.alpaca.markets
- Match the exact URL against an allow-list, case-normalised. Anything else — including a look-alike host such as
https://api.alpaca.markets.attacker.example — is rejected. Never use a substring or startswith test here.
Live Execution Safety Gate (ALLOW_LIVE_TRADING):
- Block initialization in
LIVE mode unless ALLOW_LIVE_TRADING=true is explicitly set. Strip the value before comparing — a trailing newline from a .env loader should not silently block a legitimate live deployment.
- Emit a WARNING when live trading is authorised, so the transition is visible in the log record.
Account API Probe:
- Issue GET
/v2/account on startup and reject a response that is not a mapping.
- Verify the account is tradable:
status must be ACTIVE (or PAPER_ONLY, which is valid only in paper mode). A missing status is a veto, not an assumed ACTIVE.
- Veto if any order-blocking flag is set:
trading_blocked, account_blocked, or trade_suspended_by_user. Alpaca documents the first and last as "the account is not allowed to place orders" — a guard that ignores them authorises orders the broker will reject.
- Resolve the environment only from signals that actually exist. GET
/v2/account does not return an is_paper field (verified against Alpaca's account schema and the official alpaca-py TradeAccount model). Use, in order: an is_paper bool if your SDK wrapper injects one; status == "PAPER_ONLY"; an account_number beginning PA (observed, unofficial — treat as a positive paper signal only, never as proof an account is live).
- If none of those resolve, the environment is undeterminable — log it and fall back on the already-verified base URL. Do not treat "undeterminable" as live: that vetoes every legitimate paper deployment, because the real API never supplies the field.
Order Submission Veto Guard:
- Wrap order routing calls in
AlpacaEnvironmentManager.guard_order(), vetoing any outbound order if environment checks fail or key/URL mismatches occur.
- Validate the order itself at the gate — reject a non-positive, non-finite, or non-numeric
qty, an empty symbol, and any side outside buy/sell.
Full step-by-step procedure with broker-specific detail: see references/workflows.md.
Broker/framework coverage table for this skill: see references/standards.md.
Printable pre-flight checklist: see assets/checklist.md.
Common Pitfalls
- Hardcoding Base URLs: Hardcoding
https://api.alpaca.markets in code and relying only on switching API keys in .env.
- Shared Credential Variable Names: Using generic
ALPACA_KEY_ID for both paper and live testing, leading to accidental live deployment.
- Trusting a nonexistent
is_paper field: Alpaca's /v2/account response has no is_paper key. Code written against it reads None on every call — so a guard that treats a missing value as live bricks paper trading, and one that treats it as paper waves live accounts through. Resolve the environment from fields the API really returns, and treat "unknown" as unknown.
- Letting an unrecognised environment fall through:
if mode == PAPER … elif mode == LIVE … with no else returns success for any third value, so a typo'd or foreign enum member authorises a live order with no ALLOW_LIVE_TRADING check. Normalise the mode up front and raise on anything unknown.
- Defaulting a missing
status to ACTIVE: A truncated or error-shaped account payload then reads as a healthy account. An unreadable account is a veto.
- Ignoring the blocked flags:
trading_blocked / account_blocked / trade_suspended_by_user mean the broker will refuse the order. Catching that locally turns a confusing broker rejection into a clear pre-trade veto.
- Strict
is True on a loosely-typed payload: An adapter that hands back "true"/"false" as strings defeats if account_data.get("trading_blocked") is True, so an explicitly blocked account reads as unblocked. Accept the string forms and fail closed on a value you cannot interpret.
- Substring URL matching:
base_url.startswith("https://api.alpaca.markets") also accepts https://api.alpaca.markets.attacker.example. Compare against an exact allow-list.
- Missing Live Confirmation Flag: Allowing live trading without an explicit boolean environment variable guard (
ALLOW_LIVE_TRADING=true).
Verification
- Configure paper keys with the live URL and confirm
AlpacaEnvironmentManager raises EnvironmentMismatchError.
- Attempt live mode initialization without
ALLOW_LIVE_TRADING=true and confirm execution is blocked.
- Construct a config with an unrecognised environment value and confirm it raises rather than validating.
- Simulate an account probe returning
is_paper=False when configured in paper mode and confirm the startup veto.
- Simulate a realistic paper payload with no
is_paper field (status=ACTIVE, account_number=PA…) and confirm it is accepted, not vetoed.
- Simulate
trading_blocked=true and confirm the order guard vetoes.
- Simulate a probe response with no
status field and confirm the veto.
- Submit
qty=0, qty=NaN, and side="long" through guard_order() and confirm each is rejected.
- Run the unit test suite
python -m unittest discover -s skills/alpaca-paper-live-key-separation/scripts and confirm a 100% pass rate.
Related Skills
paper-to-live-promotion-checklist
headless-broker-auth-patterns
kill-switch-and-drawdown-circuit-breakers
sandbox-credential-leakage-prevention
sandbox-vs-production-endpoint-drift
1---2name: alpaca-paper-live-key-separation3description: >-4 Use when a bot connects to the Alpaca Trading API and must never cross paper keys with5 the live endpoint; pins the base URL per environment, checks the key prefix, probes6 /v2/account and requires an explicit ALLOW_LIVE_TRADING flag.7license: Apache-2.08metadata:9 source: https://skillmd.com/skills/himanshuj16/alpaca-paper-live-key-separation10 domain: algorithmic-trading11 subdomain: broker-integration12 tags: broker-integration, alpaca-api, paper-trading, live-capital-guard, credential-security13 brokers_frameworks: Alpaca Trading API v214 version: "2.0.0"15 author: algo-trading-skills-contributors16---1718## When to Use1920Invoke this whenever a bot connects to the Alpaca Trading API (or any broker with distinct paper/live environments). Connecting a paper strategy to Alpaca's live endpoint (`https://api.alpaca.markets`) using live API keys — or passing live order signals into a paper endpoint — is a catastrophic operational error. Pinning the base URL per environment, inspecting the key prefix, probing `/v2/account` for tradability, and requiring an explicit `ALLOW_LIVE_TRADING` environment variable are all mandatory before any order is submitted.2122**The base URL is the control that actually separates the environments.** Alpaca serves paper accounts from `https://paper-api.alpaca.markets` and live accounts from `https://api.alpaca.markets`; a live account is not reachable through the paper host. Every other check in this skill is defence-in-depth layered on that pin — treat them as corroboration, never as a substitute.2324## When NOT to Use2526- **Brokers without separate paper/live endpoints**: If a single API base URL serves both environments and differentiation is done purely via credentials, this skill's URL-matching logic does not apply. Use broker-specific auth patterns instead (see `headless-broker-auth-patterns`).27- **Backtesting or simulation engines**: When running historical replays or simulated fills with no network calls to a real broker API, environment segregation is irrelevant.28- **Non-Alpaca brokers with different credential schemes**: The `PK...`/`AK...` prefix convention is Alpaca-specific. Brokers using different key formats (e.g., IBKR account IDs `DU...`/`U...`) require their own validation logic. For a broker-agnostic host allow-list, see `sandbox-credential-leakage-prevention`.29- **Read-only market data access**: If the integration only consumes market data endpoints (not order routing), live capital loss is not a risk and the order guard is unnecessary.3031## Prerequisites3233- Distinct environment variable names for paper vs live credentials (e.g. `ALPACA_PAPER_KEY_ID` vs `ALPACA_LIVE_KEY_ID`).34- Base URL configuration (`https://paper-api.alpaca.markets` for paper vs `https://api.alpaca.markets` for live).35- Explicit `ALLOW_LIVE_TRADING=true` environment flag for live execution mode.3637## Workflow38391. **Load & Normalise the Environment Mode**:40 - Coerce the configured environment into a known `PAPER`/`LIVE` value before any comparison. A value matching neither must raise, never fall through — an unrecognised mode that skips both branches is an authorisation, not a no-op.41422. **Inspect Credential Prefixes**:43 - `PK...` indicates Alpaca paper credentials; `AK...` indicates live credentials.44 - Use this to *reject* a credential carrying the opposite environment's prefix. Do **not** require a positive prefix match: the convention is widely observed but is not documented by Alpaca, so an unrecognised key format must not be treated as proof of anything (see `references/standards.md`).45463. **Base URL & Mode Verification**:47 - Match configuration mode against endpoint URLs:48 - `PAPER` mode → `https://paper-api.alpaca.markets`49 - `LIVE` mode → `https://api.alpaca.markets`50 - Match the *exact* URL against an allow-list, case-normalised. Anything else — including a look-alike host such as `https://api.alpaca.markets.attacker.example` — is rejected. Never use a substring or `startswith` test here.51524. **Live Execution Safety Gate (`ALLOW_LIVE_TRADING`)**:53 - Block initialization in `LIVE` mode unless `ALLOW_LIVE_TRADING=true` is explicitly set. Strip the value before comparing — a trailing newline from a `.env` loader should not silently block a legitimate live deployment.54 - Emit a WARNING when live trading is authorised, so the transition is visible in the log record.55565. **Account API Probe**:57 - Issue GET `/v2/account` on startup and reject a response that is not a mapping.58 - Verify the account is tradable: `status` must be `ACTIVE` (or `PAPER_ONLY`, which is valid **only** in paper mode). A *missing* `status` is a veto, not an assumed `ACTIVE`.59 - Veto if any order-blocking flag is set: `trading_blocked`, `account_blocked`, or `trade_suspended_by_user`. Alpaca documents the first and last as "the account is not allowed to place orders" — a guard that ignores them authorises orders the broker will reject.60 - **Resolve the environment only from signals that actually exist.** GET `/v2/account` does **not** return an `is_paper` field (verified against Alpaca's account schema and the official `alpaca-py` `TradeAccount` model). Use, in order: an `is_paper` bool if your SDK wrapper injects one; `status == "PAPER_ONLY"`; an `account_number` beginning `PA` (observed, unofficial — treat as a positive *paper* signal only, never as proof an account is live).61 - If none of those resolve, the environment is **undeterminable** — log it and fall back on the already-verified base URL. Do not treat "undeterminable" as live: that vetoes every legitimate paper deployment, because the real API never supplies the field.62636. **Order Submission Veto Guard**:64 - Wrap order routing calls in `AlpacaEnvironmentManager.guard_order()`, vetoing any outbound order if environment checks fail or key/URL mismatches occur.65 - Validate the order itself at the gate — reject a non-positive, non-finite, or non-numeric `qty`, an empty `symbol`, and any `side` outside `buy`/`sell`.6667> Full step-by-step procedure with broker-specific detail: see `references/workflows.md`.68> Broker/framework coverage table for this skill: see `references/standards.md`.69> Printable pre-flight checklist: see `assets/checklist.md`.7071## Common Pitfalls7273- **Hardcoding Base URLs**: Hardcoding `https://api.alpaca.markets` in code and relying only on switching API keys in `.env`.74- **Shared Credential Variable Names**: Using generic `ALPACA_KEY_ID` for both paper and live testing, leading to accidental live deployment.75- **Trusting a nonexistent `is_paper` field**: Alpaca's `/v2/account` response has no `is_paper` key. Code written against it reads `None` on every call — so a guard that treats a missing value as live bricks paper trading, and one that treats it as paper waves live accounts through. Resolve the environment from fields the API really returns, and treat "unknown" as unknown.76- **Letting an unrecognised environment fall through**: `if mode == PAPER … elif mode == LIVE …` with no `else` returns success for any third value, so a typo'd or foreign enum member authorises a live order with no `ALLOW_LIVE_TRADING` check. Normalise the mode up front and raise on anything unknown.77- **Defaulting a missing `status` to `ACTIVE`**: A truncated or error-shaped account payload then reads as a healthy account. An unreadable account is a veto.78- **Ignoring the blocked flags**: `trading_blocked` / `account_blocked` / `trade_suspended_by_user` mean the broker will refuse the order. Catching that locally turns a confusing broker rejection into a clear pre-trade veto.79- **Strict `is True` on a loosely-typed payload**: An adapter that hands back `"true"`/`"false"` as strings defeats `if account_data.get("trading_blocked") is True`, so an explicitly blocked account reads as unblocked. Accept the string forms and fail closed on a value you cannot interpret.80- **Substring URL matching**: `base_url.startswith("https://api.alpaca.markets")` also accepts `https://api.alpaca.markets.attacker.example`. Compare against an exact allow-list.81- **Missing Live Confirmation Flag**: Allowing live trading without an explicit boolean environment variable guard (`ALLOW_LIVE_TRADING=true`).8283## Verification8485- Configure paper keys with the live URL and confirm `AlpacaEnvironmentManager` raises `EnvironmentMismatchError`.86- Attempt live mode initialization without `ALLOW_LIVE_TRADING=true` and confirm execution is blocked.87- Construct a config with an unrecognised environment value and confirm it raises rather than validating.88- Simulate an account probe returning `is_paper=False` when configured in paper mode and confirm the startup veto.89- Simulate a realistic paper payload with **no** `is_paper` field (`status=ACTIVE`, `account_number=PA…`) and confirm it is accepted, not vetoed.90- Simulate `trading_blocked=true` and confirm the order guard vetoes.91- Simulate a probe response with no `status` field and confirm the veto.92- Submit `qty=0`, `qty=NaN`, and `side="long"` through `guard_order()` and confirm each is rejected.93- Run the unit test suite `python -m unittest discover -s skills/alpaca-paper-live-key-separation/scripts` and confirm a 100% pass rate.9495## Related Skills9697- `paper-to-live-promotion-checklist`98- `headless-broker-auth-patterns`99- `kill-switch-and-drawdown-circuit-breakers`100- `sandbox-credential-leakage-prevention`101- `sandbox-vs-production-endpoint-drift`