T-Invest Node SDK Node.js
在此页面上

运行 CLI 命令

模块自带命令行界面(CLI)。可以直接在终端查询数据、管理订单和订阅数据流,无需编写代码。

通过 CLI 查询账户列表

安装软件包,并获取具有账户读取权限的生产 API 令牌。在 Bash 中,read 会隐藏令牌输入。安装 SDK · 获取 T-Bank 令牌

Shell
read -rsp 'T-Invest token: ' T_INVEST_TOKEN
printf '\n'
export T_INVEST_TOKEN
export T_INVEST_ENDPOINT=invest-public-api.tbank.ru:443

npm exec -- t-invest-node-sdk account list --format=json

此命令输出包含账户的 JSON 数组。以下是一个账户的示例数据。

JSON
[
  {
    "id": "account-id",
    "name": "Main account",
    "type": "ACCOUNT_TYPE_TINKOFF",
    "status": "ACCOUNT_STATUS_OPEN",
    "accessLevel": "ACCOUNT_ACCESS_LEVEL_FULL_ACCESS",
    "openedDate": "2026-06-19T00:00:00.000Z",
    "closedDate": ""
  }
]

如果令牌无权访问任何账户,CLI 将输出 [] 并以代码 0 退出。使用沙箱时,请将地址设为 sandbox-invest-public-api.tbank.ru:443,使用沙箱令牌,再运行 sandbox account list --format=json。

选择命令并获取帮助

命令路径以命令组开头,也可以包含子组。例如,instrument bond list 会列出债券。你可以查看整个 CLI、某个命令组或具体命令的帮助。

Shell
npm exec -- t-invest-node-sdk --help
npm exec -- t-invest-node-sdk market --help
npm exec -- t-invest-node-sdk market order-book --help

传入连接设置和参数

对于需要 SDK 的命令,请通过 --token 或 T_INVEST_TOKEN,以及 --endpoint 或 T_INVEST_ENDPOINT 传入连接参数。

用标志传入布尔值,例如 --raw,以及在支持时使用 --no-raw。不支持 --raw=true 和 --raw=false。正整数参数必须在 JavaScript 安全整数范围内。日期应采用 RFC 3339 格式,显式含有 Z 或数字时区偏移量。

检查退出码

CLI 将结果写入 stdout,将诊断信息写入 stderr。退出码表示执行结果。

  • 0 — 命令成功完成。
  • 2 — 调用错误,例如命令未知、参数无效、缺少必填的 CLI 参数或环境变量,或命令配置无效。
  • 1 — 运行、提供方、文件系统、输出或内部命令定义出错。

进程信号如何结束流,见流式 CLI 参考。

选择命令或兼容别名

新脚本使用主要命令名。查找旧脚本中的名称时,请展开对应分组的兼容别名。

账户和配额

查询可访问的账户、用户信息、保证金数据或资费配额。

命令 SDK 方法
account list sdk.users.getAccounts
account info sdk.users.getInfo
account margin sdk.users.getMarginAttributes
account tariff sdk.users.getUserTariff
账户命令别名
兼容别名 命令
account get-accounts, users get-accounts account list
account get-info, users get-info account info
account get-margin-attributes, users get-margin-attributes account margin
account get-user-tariff, users get-user-tariff account tariff

金融工具

按代码查找金融工具、获取资产类别数据或查询交易日程。

命令 SDK 方法
instrument search sdk.instruments.findInstrument
instrument show sdk.instruments.getInstrumentBy
instrument dividends sdk.instruments.getDividends
instrument schedules sdk.instruments.tradingSchedules
instrument favorite list sdk.instruments.getFavorites
instrument favorite edit sdk.instruments.editFavorites
instrument share list sdk.instruments.shares
instrument share show sdk.instruments.shareBy
instrument bond list sdk.instruments.bonds
instrument bond show sdk.instruments.bondBy
instrument bond coupons sdk.instruments.getBondCoupons
instrument bond accrued sdk.instruments.getAccruedInterests
instrument etf list sdk.instruments.etfs
instrument etf show sdk.instruments.etfBy
instrument currency list sdk.instruments.currencies
instrument currency show sdk.instruments.currencyBy
instrument future list sdk.instruments.futures
instrument future show sdk.instruments.futureBy
instrument future margin sdk.instruments.getFuturesMargin
instrument option list sdk.instruments.optionsBy
instrument option show sdk.instruments.optionBy
instrument asset list sdk.instruments.getAssets
instrument asset show sdk.instruments.getAssetBy
instrument brand list sdk.instruments.getBrands
instrument brand show sdk.instruments.getBrandBy
instrument country list sdk.instruments.getCountries
金融工具命令别名
兼容别名 命令
instrument find-instrument, instruments find-instrument instrument search
instrument get-instrument-by, instruments get-instrument-by instrument show
instrument get-dividends, instruments get-dividends instrument dividends
instrument trading-schedules, instruments trading-schedules instrument schedules
instrument get-favorites, instruments get-favorites instrument favorite list
instrument edit-favorites, instruments edit-favorites instrument favorite edit
instrument shares, instruments shares instrument share list
instrument share-by, instruments share-by instrument share show
instrument bonds, instruments bonds instrument bond list
instrument bond-by, instruments bond-by instrument bond show
instrument get-bond-coupons, instruments get-bond-coupons instrument bond coupons
instrument get-accrued-interests, instruments get-accrued-interests instrument bond accrued
instrument etfs, instruments etfs instrument etf list
instrument etf-by, instruments etf-by instrument etf show
instrument currencies, instruments currencies instrument currency list
instrument currency-by, instruments currency-by instrument currency show
instrument futures, instruments futures instrument future list
instrument future-by, instruments future-by instrument future show
instrument get-futures-margin, instruments get-futures-margin instrument future margin
instrument options-by, instruments options-by instrument option list
instrument option-by, instruments option-by instrument option show
instrument get-assets, instruments get-assets instrument asset list
instrument get-asset-by, instruments get-asset-by instrument asset show
instrument get-brands, instruments get-brands instrument brand list
instrument get-brand-by, instruments get-brand-by instrument brand show
instrument get-countries, instruments get-countries instrument country list

行情数据

查询 K 线、价格、逐笔成交、订单簿或交易状态。

命令 SDK 方法
market candles sdk.marketData.getCandles
market close-prices sdk.marketData.getClosePrices
market last-prices sdk.marketData.getLastPrices
market trades sdk.marketData.getLastTrades
market order-book sdk.marketData.getOrderBook
market status sdk.marketData.getTradingStatus
market statuses sdk.marketData.getTradingStatuses
行情命令别名
兼容别名 命令
market get-candles, marketdata get-candles market candles
market get-close-prices, marketdata get-close-prices market close-prices
market get-last-prices, marketdata get-last-prices market last-prices
market get-last-trades, marketdata get-last-trades market trades
market get-order-book, marketdata get-order-book market order-book
market get-trading-status, marketdata get-trading-status market status
market get-trading-statuses, marketdata get-trading-statuses market statuses

投资组合、操作和报告

查询持仓或投资组合,按游标读取操作记录,或请求报告。

命令 SDK 方法
operation list sdk.operations.getOperations
operation page sdk.operations.getOperationsByCursor
operation broker-report sdk.operations.getBrokerReport
operation foreign-dividends-report sdk.operations.getDividendsForeignIssuer
operation portfolio sdk.operations.getPortfolio
operation positions sdk.operations.getPositions
operation withdraw-limits sdk.operations.getWithdrawLimits
操作命令别名
兼容别名 命令
operation get-operations, operations get-operations operation list
operation get-operations-by-cursor, operations get-operations-by-cursor operation page
operation get-broker-report, operations get-broker-report operation broker-report
operation get-dividends-foreign-issuer, operations get-dividends-foreign-issuer operation foreign-dividends-report
operation get-portfolio, operations get-portfolio operation portfolio
operation get-positions, operations get-positions operation positions
operation get-withdraw-limits, operations get-withdraw-limits operation withdraw-limits

订单和止损订单

这些命令用于查询和管理订单。默认配置下,变更需要确认。确认命令

order show 和 sandbox order show 接受可选参数 --order-date,用于指定下单日期和时间。请使用 RFC 3339 格式,例如 --order-date 2026-11-30T09:30:00Z。

命令 SDK 方法
order list sdk.orders.getOrders
order show sdk.orders.getOrderState
order place sdk.orders.postOrder
order cancel sdk.orders.cancelOrder
order replace sdk.orders.replaceOrder
stop-order list sdk.stopOrders.getStopOrders
stop-order place sdk.stopOrders.postStopOrder
stop-order cancel sdk.stopOrders.cancelStopOrder
订单命令别名
兼容别名 命令
order get-orders, orders get-orders order list
order get-order-state, orders get-order-state order show
order post-order, orders post-order order place
order cancel-order, orders cancel-order order cancel
order replace-order, orders replace-order order replace
stop-order get-stop-orders, stoporders get-stop-orders stop-order list
stop-order post-stop-order, stoporders post-stop-order stop-order place
stop-order cancel-stop-order, stoporders cancel-stop-order stop-order cancel

沙箱

开立测试账户、查询账户数据或操作测试订单。沙箱示例

命令 SDK 方法
sandbox account list sdk.sandbox.getSandboxAccounts
sandbox account open sdk.sandbox.openSandboxAccount
sandbox account close sdk.sandbox.closeSandboxAccount
sandbox order list sdk.sandbox.getSandboxOrders
sandbox order show sdk.sandbox.getSandboxOrderState
sandbox order place sdk.sandbox.postSandboxOrder
sandbox order replace sdk.sandbox.replaceSandboxOrder
sandbox order cancel sdk.sandbox.cancelSandboxOrder
sandbox position list sdk.sandbox.getSandboxPositions
sandbox operation list sdk.sandbox.getSandboxOperations
sandbox operation page sdk.sandbox.getSandboxOperationsByCursor
sandbox portfolio sdk.sandbox.getSandboxPortfolio
sandbox withdraw-limits sdk.sandbox.getSandboxWithdrawLimits
sandbox pay-in sdk.sandbox.sandboxPayIn
沙箱命令别名
兼容别名 命令
sandbox get-sandbox-accounts sandbox account list
sandbox open-sandbox-account sandbox account open
sandbox close-sandbox-account sandbox account close
sandbox get-sandbox-orders sandbox order list
sandbox get-sandbox-order-state sandbox order show
sandbox post-sandbox-order sandbox order place
sandbox replace-sandbox-order sandbox order replace
sandbox cancel-sandbox-order sandbox order cancel
sandbox get-sandbox-positions sandbox position list
sandbox get-sandbox-operations sandbox operation list
sandbox get-sandbox-operations-by-cursor sandbox operation page
sandbox get-sandbox-portfolio sandbox portfolio
sandbox get-sandbox-withdraw-limits sandbox withdraw-limits
sandbox sandbox-pay-in sandbox pay-in

流、开发与帮助

在 JSON 中定义流式订阅。开发 SDK 时可生成契约。

命令 用途
stream run 按配置文件启动流
dev compile-proto 生成 TypeScript 契约
help 显示 CLI、命令组或命令的帮助
version 显示软件包版本
契约生成命令别名
兼容别名 命令
compile-proto dev compile-proto

确认有副作用的命令

修改订单、收藏夹或沙箱账户的命令默认需要 --confirm。这一行为由 defaultConfig.requireSideEffectConfirmation 控制。

请自行提供幂等键。order place 接受 --order-id,order replace 接受 --idempotency-key。

sandbox pay-in 接受 --currency=rub|usd。CLI 拒绝未知币种。如果提供方不支持 --currency=usd,命令会返回错误。

运行流式命令

流式 API 使用 stream run --config=PATH。长期订阅、进程退出和事件格式见流式 CLI 参考与配置参考。

请在公开 CLI 中使用当前方法。

  • sdk.instruments.options 在生成契约中已弃用。请使用 instrument option list / sdk.instruments.optionsBy。

--figi 和风险字段的兼容性见提供方的弃用字段。