$git clone https://github.com/zhangxiangliang/stock-apistock-api 是一个零运行时依赖的股票行情工具,支持 Node.js、浏览器、CLI 和 MCP。默认使用 stocks.auto,自动从可用数据源获取行情。
| 1 | <h1 align="center">stock-api</h1> |
| 2 | |
| 3 | <p align="center"> |
| 4 | 支持 A 股、港股、美股行情查询的 TypeScript 股票数据工具。 |
| 5 | </p> |
| 6 | |
| 7 | <p align="center"> |
| 8 | <a href="./README.EN.md">English</a> | |
| 9 | <a href="./README.md">简体中文</a> |
| 10 | </p> |
| 11 | |
| 12 | <p align="center"> |
| 13 | <a href="https://npmcharts.com/compare/stock-api?minimal=true"><img src="https://img.shields.io/npm/dm/stock-api.svg?sanitize=true" alt="Downloads"></a> |
| 14 | <a href="https://www.npmjs.com/package/stock-api"><img src="https://img.shields.io/npm/v/stock-api.svg?sanitize=true" alt="Version"></a> |
| 15 | <a href="https://www.npmjs.com/package/stock-api"><img src="https://img.shields.io/npm/l/stock-api.svg?sanitize=true" alt="License"></a> |
| 16 | <a href="https://www.npmjs.com/package/stock-api"><img src="https://img.shields.io/badge/language-typescript-blue" alt="TypeScript"></a> |
| 17 | </p> |
| 18 | |
| 19 | <p align="center"> |
| 20 | <img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2Fzhangxiangliang%2Fstock-api%2Fapi-status%2Findex.zh-CN.json&cacheSeconds=300" alt="接口状态"> |
| 21 | <img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2Fzhangxiangliang%2Fstock-api%2Fapi-status%2Ftencent.zh-CN.json&cacheSeconds=300" alt="腾讯状态"> |
| 22 | <img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2Fzhangxiangliang%2Fstock-api%2Fapi-status%2Fsina.zh-CN.json&cacheSeconds=300" alt="新浪状态"> |
| 23 | <img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2Fzhangxiangliang%2Fstock-api%2Fapi-status%2Feastmoney.zh-CN.json&cacheSeconds=300" alt="东方财富状态"> |
| 24 | </p> |
| 25 | |
| 26 | <p align="center"> |
| 27 | <a href="https://zhangxiangliang.github.io/stock-api/web-demo/"> |
| 28 | <img src="https://img.shields.io/badge/%E5%9C%A8%E7%BA%BF%E4%BD%93%E9%AA%8C-WEB%20DEMO-3b82f6?style=for-the-badge" alt="在线体验"> |
| 29 | </a> |
| 30 | <a href="https://zhangxiangliang.github.io/stock-api/"> |
| 31 | <img src="https://img.shields.io/badge/%E9%A1%B9%E7%9B%AE%E4%B8%BB%E9%A1%B5-HOME%20PAGE-24292f?style=for-the-badge" alt="项目主页"> |
| 32 | </a> |
| 33 | </p> |
| 34 | |
| 35 | `stock-api` 是一个零运行时依赖的股票行情工具,支持 Node.js、浏览器、CLI 和 MCP。默认使用 `stocks.auto`,自动从可用数据源获取行情。 |
| 36 | |
| 37 | ## 支持使用方式 |
| 38 | |
| 39 | <p> |
| 40 | <img src="https://img.shields.io/badge/Node.js-TypeScript%20API-22c55e?style=for-the-badge&logo=node.js&logoColor=white" alt="Node.js"> |
| 41 | <img src="https://img.shields.io/badge/Browser-CDN%20%2F%20Bundler-38bdf8?style=for-the-badge&logo=googlechrome&logoColor=white" alt="Browser"> |
| 42 | <img src="https://img.shields.io/badge/CLI-npx%20stock--api-f97316?style=for-the-badge&logo=gnubash&logoColor=white" alt="CLI"> |
| 43 | <img src="https://img.shields.io/badge/MCP-AI%20Tools-a855f7?style=for-the-badge&logo=openai&logoColor=white" alt="MCP"> |
| 44 | </p> |
| 45 | |
| 46 | ## 特性 |
| 47 | |
| 48 | - Node.js / Browser bundler API + TypeScript 类型 |
| 49 | - CLI 查询股票行情、K 线和搜索股票 |
| 50 | - MCP tools 给 AI 客户端直接调用股票接口 |
| 51 | - 默认自动兜底:`tencent -> sina -> eastmoney` |
| 52 | - 指定数据源:`stocks.tencent` / `stocks.sina` / `stocks.eastmoney` |
| 53 | - 支持 A 股、港股、美股代码格式 |
| 54 | - 零运行时依赖 |
| 55 | |
| 56 | ## Node.js |
| 57 | |
| 58 | ### 安装 |
| 59 | |
| 60 | Node.js 环境要求 `>=18`。 |
| 61 | |
| 62 | ```shell |
| 63 | npm install stock-api |
| 64 | ``` |
| 65 | |
| 66 | ### 使用 |
| 67 | |
| 68 | ```typescript |
| 69 | import { stocks } from "stock-api"; |
| 70 | |
| 71 | const stock = await stocks.auto.getStock("SH510500"); |
| 72 | const list = await stocks.auto.getStocks(["SH510500", "SZ000651"]); |
| 73 | const klines = await stocks.auto.getKlines("SH600519", { period: "day" }); |
| 74 | const results = await stocks.auto.searchStocks("格力电器"); |
| 75 | ``` |
| 76 | |
| 77 | 股票代码使用 `SH` / `SZ` / `HK` / `US` 前缀,例如 `SH510500`、`SZ000651`。 |
| 78 | |
| 79 | ## 浏览器 |
| 80 | |
| 81 | ### 引用 |
| 82 | |
| 83 | ```html |
| 84 | <script src="https://cdn.jsdelivr.net/npm/stock-api/dist/browser/stock-api.iife.min.js"></script> |
| 85 | ``` |
| 86 | |
| 87 | ### 使用 |
| 88 | |
| 89 | ```html |
| 90 | <script> |
| 91 | StockApi.stocks.auto.getStock("SH510500").then(console.log); |
| 92 | StockApi.stocks.auto.getStocks(["SH510500", "SZ000651"]).then(console.log); |
| 93 | StockApi.stocks.auto.getKlines("SH600519", { period: "day" }).then(console.log); |
| 94 | StockApi.stocks.auto.searchStocks("格力电器").then(console.log); |
| 95 | </script> |
| 96 | ``` |
| 97 | |
| 98 | 浏览器示例:[GitHub Pages](https://zhangxiangliang.github.io/stock-api/web-demo/) |
| 99 | |
| 100 | ## CLI |
| 101 | |
| 102 | ```shell |
| 103 | npx stock-api get-stock SH510500 |
| 104 | npx stock-api get-stocks SH510500 SZ000651 |
| 105 | npx stock-api get-klines SH600519 --period day --count 120 |
| 106 | np |