Binance Pro API Reference

Copy Markdown View Source

This reference describes the supported Elixir surface for the Binance-first CCXT Pro preview. The canonical implementation remains generated from ts/src/pro/binance.ts; this document explains how to call the generated target from Elixir code.

Entrypoints

Ccxt.Pro.binance/1 creates a CCXT-style Binance Pro instance:

binance =
  Ccxt.Pro.binance(%{
    apiKey: "...",
    secret: "...",
    binanceEnv: "prod",
    options: %{recvWindow: 5_000}
  })

Ccxt.Pro.Binance.new/1 is the exchange-specific constructor behind Ccxt.Pro.binance/1.

Accepted config keys:

KeyMeaning
apiKey / api_keyBinance API key for signed Pro methods
secret / api_secretBinance API secret for signed Pro methods
binanceEnv / binance_env"prod", "testnet", "sandbox", or "demo"
optionsDefault call options merged into generated methods

Per-call options override instance options.

Watch Semantics

Generated watch_* methods follow CCXT Pro's one-message async model. In Elixir they return {:ok, value} or {:error, reason}:

{:ok, ticker} =
  Ccxt.Pro.Binance.watch_ticker("BTC/USDT",
    binance_env: "prod",
    timeout: 30_000
  )

The websocket subscription remains open on the shared connection after a successful watch_* call. Call the same watch_* method again to wait for the next update.

Most public methods also accept an instance as their first argument:

{:ok, ticker} = Ccxt.Pro.Binance.watch_ticker(binance, "BTC/USDT")

Stream Semantics

Elixir stream_* methods are convenience wrappers over repeated watch_* calls. They return an Enumerable that yields {:ok, value} or {:error, reason}:

Ccxt.Pro.Binance.stream_ticker("BTC/USDT",
  binance_env: "prod",
  timeout: 30_000,
  unwatch_on_halt: true
)
|> Enum.take(10)

unwatch_on_halt: true sends the matching public unwatch request when the enumerable halts. Private stream wrappers do not imply private unwatch support; close the connection explicitly when private user-data streams should stop.

Connection Lifecycle

Ccxt.Pro owns the runtime websocket topology:

FunctionPurpose
Ccxt.Pro.ensure_started/0Starts the Pro supervisor if needed
Ccxt.Pro.connection/2Returns one supervised websocket process per URL
Ccxt.Pro.connections/0Lists active Pro websocket connections
Ccxt.Pro.connection_info/1Returns connection state, waiters, channels, and caches
Ccxt.Pro.close_connection/1Terminates a connection by URL or pid
Ccxt.Pro.attach_debug_logger/0Prints Pro telemetry events in IEx
Ccxt.Pro.detach_debug_logger/0Detaches the debug logger

unwatch_* methods only send Binance unsubscribe messages and wait for ACK. They do not close the underlying websocket connection. Use Ccxt.Pro.close_connection/1 for shutdown.

Production Worker Pattern

For an OTP-style integration, keep blocking stream consumption out of business process callbacks. The included examples/pro_ticker_worker.exs demonstrates the public stream shape:

children = [
  {Task.Supervisor, name: Ccxt.Pro.Examples.BinanceTickerWorker.TaskSupervisor},
  {Ccxt.Pro.Examples.BinanceTickerWorker,
   symbol: "BTC/USDT", binance_env: "prod", print?: false}
]

Supervisor.start_link(children, strategy: :one_for_one)

The worker uses a GenServer to own the latest ticker snapshot and a Task.Supervisor child to consume Ccxt.Pro.Binance.stream_ticker/2. On stop, the worker terminates the stream task and calls Ccxt.Pro.close_connection/1.

Run the standalone example from ccxt/elixir:

CCXT_PRO_WORKER_SECONDS=10 mix run examples/pro_ticker_worker.exs

examples/pro_order_event_worker.exs uses the same GenServer + Task.Supervisor shape for private order events. It authenticates once, reuses the returned ws_auth for Ccxt.Pro.Binance.stream_orders/4, and requests incremental private windows with newUpdates: true:

CCXT_PRO_ORDER_EVENT_WORKER_SECONDS=30 mix run examples/pro_order_event_worker.exs

Because stream consumption runs in a Task.Supervisor.async_nolink/2 child, a production worker must handle both the task result message ({ref, result}) and the matching :DOWN monitor message. The example demonitor-flushes the matched task result, closes the private websocket connection, records the last error, and restarts through the normal retry path.

Public Market Methods

Public market methods do not require credentials.

MethodReturn shape
watch_ticker/2ticker map
watch_tickers/2map of ticker maps
watch_bids_asks/2map of bid/ask ticker maps
watch_mark_price/2ticker map
watch_mark_prices/2map of ticker maps
watch_trades/4list of trade maps
watch_trades_for_symbols/4list of trade maps
watch_order_book/3order book map
watch_order_book_for_symbols/3order book map
watch_ohlcv/5list of OHLCV arrays
watch_ohlcv_for_symbols/4list of OHLCV arrays
watch_liquidations/4list of liquidation maps
watch_liquidations_for_symbols/4list of liquidation maps
fetch_ticker_ws/2ticker map via websocket API
fetch_trades_ws/4list of trade maps via websocket API
fetch_order_book_ws/3order book map via websocket API
fetch_ohlcv_ws/5list of OHLCV arrays via websocket API

Public stream wrappers:

StreamUnderlying watch
stream_ticker/2watch_ticker/2
stream_trades/4watch_trades/4
stream_order_book/3watch_order_book/3
stream_ohlcv/5watch_ohlcv/5

Public unwatch methods:

MethodScope
unwatch_ticker/2single ticker
unwatch_tickers/2multi ticker
unwatch_mark_price/2single mark price
unwatch_mark_prices/2multi mark price
unwatch_trades/2single trade stream
unwatch_trades_for_symbols/2multi trade stream
unwatch_order_book/2single order book
unwatch_order_book_for_symbols/2multi order book
unwatch_ohlcv/3single OHLCV stream
unwatch_ohlcv_for_symbols/2multi OHLCV stream

Private Websocket API Methods

Signed read-only methods:

MethodReturn shape
fetch_balance_ws/1CCXT balance map
fetch_position_ws/2position map
fetch_positions_ws/2list of position maps
fetch_order_ws/3order map
fetch_orders_ws/4list of order maps
fetch_open_orders_ws/4list of order maps
fetch_closed_orders_ws/4list of order maps
fetch_my_trades_ws/4list of trade maps

Signed order mutation methods:

MethodRisk policy
create_order_ws/6Requires explicit live gate for production tests
edit_order_ws/7Requires explicit live gate for production tests
cancel_order_ws/3Safe only for known test-created order ids
cancel_all_orders_ws/2Manual-confirmed only

Order mutation tests use non-marketable production orders and immediate cleanup where possible. They are not part of normal mix test.

Private User Data Stream Methods

Private event-driven methods:

MethodReturn shape
watch_balance/1CCXT balance map
watch_orders/4list of order maps
watch_positions/4list of position maps
watch_my_trades/4list of trade maps
watch_my_liquidations/4list of liquidation maps
watch_my_liquidations_for_symbols/4list of liquidation maps

Private stream wrappers:

StreamUnderlying watch
stream_balance/1watch_balance/1
stream_orders/4watch_orders/4
stream_positions/4watch_positions/4
stream_my_trades/4watch_my_trades/4

User-data stream helpers:

FunctionPurpose
authenticate/1Authenticates a private websocket connection
user_data_stream_status/1Calls session.status
user_data_stream_subscriptions/1Calls session.subscriptions
unsubscribe_user_data_stream/2Calls userDataStream.unsubscribe
renew_listen_token/1Renews listen-token based private sessions where applicable
keep_alive_listen_key/3Keeps listen-key based sessions alive

Options

Common options:

OptionMeaning
:binance_env"prod", "testnet", "sandbox", or "demo"
:timeoutReceive timeout in milliseconds
:verboseEnables per-connection websocket logging
:cache_limitPer-message-hash cache limit
:unwatch_on_haltStream wrapper sends public unwatch on halt
:api_keyPer-call API key
:api_secretPer-call API secret
:recvWindow / :recv_windowBinance signed request receive window
:typeMarket type hint such as "spot", "future", "delivery", or "papi"

Private snapshot options:

OptionMeaning
:fetch_balance_snapshot / :fetchBalanceSnapshotPreload private balance state
:fetch_positions_snapshot / :fetchPositionsSnapshotPreload private position state
:snapshot_fetcherInjectable snapshot function for tests or custom integration
:rest_fetcherInjectable REST fallback function

Unified Structures

Generated parsers return CCXT-style maps for ticker, trade, order, balance, position, order book, OHLCV, and liquidation structures. Raw exchange payloads remain available under each structure's :info field.

The structure-to-database planning layer is documented in:

  • doc/ccxt-structure-schema.md
  • doc/ccxt-pro-structure-coverage.md

The persistence helper modules are intentionally adapter-like. They normalize CCXT structures into stable row maps; they do not require a specific Ecto schema or database migration.

Environment Variables

Production Pro credentials:

BINANCE_PROD_API_KEY=...
BINANCE_PROD_API_SECRET=...
BINANCE_PRO_ENV=prod

Testnet Pro credentials:

BINANCE_TESTNET_API_KEY=...
BINANCE_TESTNET_API_SECRET=...
BINANCE_PRO_ENV=testnet

Demo Pro credentials:

BINANCE_DEMO_API_KEY=...
BINANCE_DEMO_API_SECRET=...
BINANCE_PRO_ENV=demo

REST/manual smoke credentials:

BINANCE_API_KEY=...
BINANCE_API_SECRET=...
BINANCE_ENV=prod

Production, testnet, and demo keys are separate Binance key families.

Validation Commands

Normal gate:

npm run checkElixir

Generate API documentation:

npm run docsElixir

Build a local Hex package dry-run:

npm run buildElixirPackage

Run the path dependency consumer smoke:

npm run testElixirConsumer

Run the packaged consumer smoke:

npm run testElixirPackageConsumer

Run the release preview gate:

npm run releaseElixirPreviewCheck

Short public soak:

CCXT_PRO_SOAK_SECONDS=5 CCXT_PRO_SOAK_MIN_UPDATES=1 npm run testElixirProSoak

Default long public soak:

npm run testElixirProLongSoak