CLI Usage

May 26, 2026 · View on GitHub

English | 简体中文

stock-api includes a CLI for quick quote lookup, shell scripts, CI checks, and provider verification.

Usage

npx stock-api <command> [...args] [--source auto|tencent|sina|eastmoney]

The default source is auto, which falls back in this order:

tencent -> sina -> eastmoney

Install first if you prefer:

npm install stock-api
npx stock-api --help

Commands

CommandDescription
get-stock <code>Get one quote
get-stocks <code...>Get multiple quotes
get-klines <code>Get daily / weekly / monthly K-lines
search-stocks <keyword>Search symbols and return quotes
mcpStart an MCP stdio server for AI clients
help / --helpShow help

Options

OptionAliasDescriptionDefault
--source-sauto / tencent / sina / eastmoneyauto
--period-pday / week / monthday
--count-cNumber of K-line rows120
--adjust-none / qfq / hfqnone

get-stock

npx stock-api get-stock SH510500

Use Sina only:

npx stock-api get-stock SH510500 --source sina

Use Eastmoney only:

npx stock-api get-stock SH600519 --source eastmoney

Output:

{
  "code": "SH510500",
  "name": "中证500ETF南方",
  "percent": -0.009791044776119473,
  "now": 8.293,
  "low": 8.242,
  "high": 8.365,
  "yesterday": 8.375,
  "source": "tencent"
}

source is the provider that returned the quote.

get-stocks

npx stock-api get-stocks SH510500 SZ000651

search-stocks

npx stock-api search-stocks 格力电器

Multiple words are joined into one keyword:

npx stock-api search-stocks 中证 500

Use one provider:

npx stock-api search-stocks 格力电器 -s sina
npx stock-api search-stocks 贵州茅台 -s eastmoney

search is kept as a backward-compatible alias. New code should use search-stocks.

get-klines

npx stock-api get-klines SH600519

Set period and count:

npx stock-api get-klines SH600519 --period week --count 20
npx stock-api get-klines SH600519 --period month --source sina

Output:

[
  {
    "date": "2026-05-22",
    "open": 1310.95,
    "close": 1290.2,
    "high": 1311.91,
    "low": 1290.12,
    "source": "tencent",
    "volume": 49157
  }
]

Source Selection

UsageBehavior
no --sourceUse auto, fallback through tencent -> sina -> eastmoney
--source tencentTencent only
--source sinaSina only
--source eastmoneyEastmoney only

MCP

stock-api mcp starts an MCP stdio server for AI clients such as Claude, Cursor, Codex, and Cherry Studio.

Example configuration:

{
  "mcpServers": {
    "stock-api": {
      "command": "npx",
      "args": ["-y", "stock-api", "mcp"]
    }
  }
}

Available tools:

ToolDescription
get_stockGet one quote
get_stocksGet multiple quotes
get_klinesGet K-line rows
search_stocksSearch symbols
inspect_stockInspect provider availability and fallback results

All tools use source: "auto" by default. You can also pass source: "tencent", "sina", or "eastmoney".

Output

The CLI always prints JSON.

npx stock-api get-stock SH510500 | jq .now
npx stock-api get-stocks SH510500 SZ000651 > stocks.json

Exit Codes

CaseExit code
Success0
Invalid arguments, unknown command, request failure1

Errors are printed to stderr.

Local CLI Testing

npm run build
node dist/cli.js --help
node dist/cli.js get-stock SH510500
node dist/cli.js get-stocks SH510500 SZ000651
node dist/cli.js get-klines SH600519 --period week --count 20
node dist/cli.js search-stocks 格力电器

Simulate published npx usage:

tmpdir=$(mktemp -d)
npm pack --pack-destination "$tmpdir"
mkdir "$tmpdir/app"
cd "$tmpdir/app"
npm init -y
npm install "$tmpdir"/stock-api-*.tgz
npx stock-api --help