@alpacahq/alpaca-trade-api

中文文档
5.0.0发布于 2026/10/01Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 5.3万

TypeScript SDK for the Alpaca Trading and Market Data APIs.

原始使用文档
文档目录10 节
  1. Requirements
  2. Runtime compatibility
  3. Install
  4. Quick start
  5. Server-Sent Events
  6. Documentation
  7. Build from source
  8. Releases
  9. Support
  10. Contributing

npm version Forum Slack

@alpacahq/alpaca-trade-api

A Node.js TypeScript SDK for the Alpaca Trading API and Market Data API. Both APIs live under their own namespace (trading / marketData) in one package, fronted by a unified Alpaca client with typed errors, resilience (retry / timeout / rate limiting), pagination helpers, ergonomic order builders, and real-time streaming.

Upgrading from 3.x? See the migration guide (also on the docs site) — it maps common 3.x calls and workflows to their 4.x equivalents and ships a codemod that automates most of the work.

Requirements

  • SDK consumers: Node.js >= 20 — the REST transport uses the platform-global fetch, Headers, URL, and AbortController. (Node 18 reached end-of-life in April 2025; the package declares engines.node >=20.)
  • Repository contributors: Node.js >= 24 (see .nvmrc). Build, docs, generation, and release tooling run on Node 24; CI separately executes the packed SDK on Node 20 to preserve the consumer compatibility floor.
  • Strict Node TypeScript projects may omit DOM libs; the REST declarations are portable and do not require "dom" in the consumer tsconfig.

Runtime compatibility

RuntimeREST / fetch-based SSEWebSocketsNotes
Node.js >= 20✅✅Primary target.
Bun✅✅Node-compatible (ws runs).
Deno✅❌Root auto-resolves to the REST build via the deno export condition.
Cloudflare Workers / workerd✅❌Root auto-resolves to the REST build (workerd / worker).
Vercel Edge✅❌Root auto-resolves to the REST build (edge-light).
Browser✅❌Resolves to the REST build (browser). Not recommended — see caveat.

Legend: ✅ supported · ❌ not supported.

  • WebSocket streaming is Node/Bun only. Those clients use Node-compatible streaming modules, which don't run on edge or in the browser. On those targets the package's export conditions transparently resolve the root import to the WebSocket-free REST build, so REST and fetch-based SSE work while the stream factories (stockStream, stream, …) plus submitAndWait throw if called. For WebSocket streaming, run on Node or Bun.
  • Browser: technically works, but discouraged. Calling Alpaca directly from a browser ships your APCA_API_SECRET_KEY to the client. Prefer a server or proxy (see examples/marketdata-backend.ts) rather than embedding credentials in front-end code.

ESM and CJS module formats, edge export conditions, and the REST-only entrypoint are documented on the docs site: Runtime & module compatibility.

Install

npm install @alpacahq/alpaca-trade-api

Quick start

import { Alpaca } from "@alpacahq/alpaca-trade-api";

const alpaca = new Alpaca({
  keyId: process.env.APCA_API_KEY_ID,
  secret: process.env.APCA_API_SECRET_KEY,
  paper: true, // default; set false for live trading
});

const account = await alpaca.trading.account.getAccount();
const price = await alpaca.marketData.getLatestPrice("AAPL");

const bars = alpaca.marketData.stockStream({ feed: "iex" });
bars.onBar((b) => console.log(b.symbol, b.close));
bars.onConnect(() => bars.subscribeForBars(["AAPL", "MSFT"]));
bars.connect();

Server-Sent Events

The facade provides typed async SSE subscriptions with cancellation, reconnection, and Last-Event-ID resumption:

const controller = new AbortController();
const activities = await alpaca.trading.subscribeActivities(
  {},
  { signal: controller.signal },
);

try {
  for await (const activity of activities) {
    console.log(activity.activityType, activity.details);
  }
} finally {
  activities.close();
}

Corporate-action mutations are available from alpaca.marketData.subscribeCorporateActions(). The longer generated operation names remain available under trading.events and marketData.corporateActions; see the streaming guide.

Documentation

The documentation site is the canonical source for workflows, conventions, and curated API discovery. Installed TypeScript declarations remain authoritative for exact signatures and models. Key guides:

Build from source

npm install      # also builds via the `prepare` script
npm run build    # tsup -> dual ESM + CJS in dist/
npm run typecheck
npm run lint
npm test

Runnable examples live in examples/. To preview the docs site locally:

npm --prefix docs install   # first time only
npm --prefix docs start     # http://localhost:3000/alpaca-trade-api-js/

Releases

Stable versions are published to npm on the latest dist-tag. See CHANGELOG.md for release notes.

Support

  • Library / SDK issues: Bugs, feature requests, or questions specific to this TypeScript library → GitHub Issues.
  • General Alpaca support & API discussion: Account questions, platform issues, or broader API topics → Alpaca Community Forum.
  • Slack community: Chat with other developers and the Alpaca community on Slack.

Contributing

Contributions are welcome. See CONTRIBUTING.md for setup, generated-code boundaries, documentation workflow, and release notes conventions.

版本与发布记录

@alpacahq/alpaca-trade-api 共发布 59 个版本,首次发布于 2018/12/05;过去 12 个月发布了 10 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
5.0.0latest2026/10/011 个月前
4.0.42026/09/251 个月前
4.0.32026/09/231 个月前
4.0.22026/09/022 个月前
4.0.12026/07/173 个月前
4.0.02026/07/173 个月前

其他发布通道:next → 2.7.0-rc2,alpha → 4.0.0-alpha.3。

关于 @alpacahq/alpaca-trade-api 的常见问题

@alpacahq/alpaca-trade-api 是什么?

TypeScript SDK for the Alpaca Trading and Market Data APIs.

如何安装 @alpacahq/alpaca-trade-api?

在项目目录运行 npm install @alpacahq/alpaca-trade-api,或使用 pnpm add / yarn add / bun add。

@alpacahq/alpaca-trade-api 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@alpacahq/alpaca-trade-api 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@alpacahq/alpaca-trade-api 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@alpacahq/alpaca-trade-api 可以商用吗?

@alpacahq/alpaca-trade-api 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@alpacahq/alpaca-trade-api 的下载量有多少?

根据 npm 官方下载统计,@alpacahq/alpaca-trade-api 在 2026/10/06 – 2026/11/04 这 30 天内被下载 53,008 次,日均约 1,767 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@alpacahq/alpaca-trade-api 还在维护吗?

最新版本 5.0.0 发布于 2026/10/01(1 个月前);至今共发布 59 个版本,其中过去 12 个月发布了 10 个;首次发布于 2018/12/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。