The Perl and Raku Conference 2025: Greenville, South Carolina - June 27-29 Learn more

NAME

Binance::API -- Perl implementation for Binance API

DESCRIPTION

This module provides a Perl implementation for Binance API

Binance API documentation: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md.

ENUM definitions: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#enum-definitions

SYNOPSIS

my $api = Binance::API->new(
apiKey => 'my_api_key',
secretKey => 'my_secret_key',
);
my $ticker = $api->ticker( symbol => 'ETHBTC' );

METHODS

new

my $api = Binance::API->new(
apiKey => 'my_api_key',
secretKey => 'my_secret_key',
);

Instantiates a new Binance::API object

PARAMETERS

apiKey

[OPTIONAL] Your Binance API key.

secretKey

[OPTIONAL] Your Binance API secret key.

recvWindow

[OPTIONAL] Number of milliseconds the request is valid for. Applies only in signed requests.

baseUrl

[OPTIONAL] Base URL of Binance endpoint.

logger

[OPTIONAL] See "new" in Binance::API::Logger

RETURNS

A Binance::API object.

ping

$api->ping();

Test connectivity to the Rest API

PARAMETERS

Takes no parameters.

RETURNS 1 if successful, otherwise 0

time

$api->time();

Test connectivity to the Rest API and get the current server time.

PARAMETERS

Takes no parameters.

RETURNS Server (epoch) time in milliseconds

exchange_info

$api->exchange_info();

Current exchange trading rules and symbol information.

PARAMETERS

Takes no parameters.

RETURNS A HASHref

{
"timezone": "UTC",
"serverTime": 1508631584636,
"rateLimits": [{
"rateLimitType": "REQUESTS",
"interval": "MINUTE",
"limit": 1200
},
{
"rateLimitType": "ORDERS",
"interval": "SECOND",
"limit": 10
},
{
"rateLimitType": "ORDERS",
"interval": "DAY",
"limit": 100000
}
],
"exchangeFilters": [],
"symbols": [{
"symbol": "ETHBTC",
"status": "TRADING",
"baseAsset": "ETH",
"baseAssetPrecision": 8,
"quoteAsset": "BTC",
"quotePrecision": 8,
"orderTypes": ["LIMIT", "MARKET"],
"icebergAllowed": false,
"filters": [{
"filterType": "PRICE_FILTER",
"minPrice": "0.00000100",
"maxPrice": "100000.00000000",
"tickSize": "0.00000100"
}, {
"filterType": "LOT_SIZE",
"minQty": "0.00100000",
"maxQty": "100000.00000000",
"stepSize": "0.00100000"
}, {
"filterType": "MIN_NOTIONAL",
"minNotional": "0.00100000"
}]
}]
}

depth

$api->depth( symbol => 'ETHBTC' );

PARAMETERS

symbol

[REQUIRED] Symbol, for example ETHBTC.

limit

[OPTIONAL] Default 100; max 5000. Valid limits: 5, 10, 20, 50, 100, 500, 1000, 5000.

RETURNS A HASHref

{
"lastUpdateId": 1027024,
"bids": [
[
"4.00000000", // PRICE
"431.00000000", // QTY
[] // Can be ignored
]
],
"asks": [
[
"4.00000200",
"12.00000000",
[]
]
]
}

trades

$api->trades();

Get recent trades (up to last 1000).

PARAMETERS

symbol

[REQUIRED] Symbol, for example ETHBTC.

limit

[OPTIONAL] Default 500; max 1000.

RETURNS An ARRAYref of HASHrefs

[
{
"id": 28457,
"price": "4.00000100",
"qty": "12.00000000",
"time": 1499865549590,
"isBuyerMaker": true,
"isBestMatch": true
}
]

historical_trades

$api->historical_trades();

Get older trades.

PARAMETERS

symbol

[REQUIRED] Symbol, for example ETHBTC.

limit

[OPTIONAL] Default 500; max 1000.

fromId

[OPTIONAL] TradeId to fetch from. Default gets most recent trades.

RETURNS An ARRAYref of HASHrefs

[
{
"id": 28457,
"price": "4.00000100",
"qty": "12.00000000",
"time": 1499865549590,
"isBuyerMaker": true,
"isBestMatch": true
}
]

aggregate_trades

$api->aggregate_trades( symbol => 'ETHBTC' );

Gets compressed, aggregate trades. Trades that fill at the time, from the same order, with the same price will have the quantity aggregated.

PARAMETERS

symbol

[REQUIRED] Symbol, for example ETHBTC.

fromId

[OPTIONAL] ID to get aggregate trades from INCLUSIVE.

startTime

[OPTIONAL] timestamp in ms to get aggregate trades from INCLUSIVE.

endTime

[OPTIONAL] timestamp in ms to get aggregate trades until INCLUSIVE.

limit

[OPTIONAL] Default 500; max 1000.

RETURNS An ARRAYref of HASHrefs

[
{
"a": 26129, // Aggregate tradeId
"p": "0.01633102", // Price
"q": "4.70443515", // Quantity
"f": 27781, // First tradeId
"l": 27781, // Last tradeId
"T": 1498793709153, // Timestamp
"m": true, // Was the buyer the maker?
"M": true // Was the trade the best price match?
}
]

klines

$api->klines( symbol => 'ETHBTC', interval => '1M' );

Kline/candlestick bars for a symbol. Klines are uniquely identified by their open time.

PARAMETERS

symbol

[REQUIRED] Symbol, for example ETHBTC.

interval

[REQUIRED] ENUM (kline intervals), for example 1m, 1h, 1d or 1M.

limit

[OPTIONAL] Default 500; max 1000.

startTime

[OPTIONAL] timestamp in ms

endTime

[OPTIONAL] timestamp in ms

RETURNS An array of ARRAYrefs

[
[
1499040000000, // Open time
"0.01634790", // Open
"0.80000000", // High
"0.01575800", // Low
"0.01577100", // Close
"148976.11427815", // Volume
1499644799999, // Close time
"2434.19055334", // Quote asset volume
308, // Number of trades
"1756.87402397", // Taker buy base asset volume
"28.46694368", // Taker buy quote asset volume
"17928899.62484339" // Can be ignored
]
]

ticker

$api->ticker( symbol => 'ETHBTC', interval => '1M' );

24 hour price change statistics.

PARAMETERS

symbol

[OPTIONAL] Symbol, for example ETHBTC. Warning: Careful when accessing this with no symbol.

RETURNS A HASHref or an Array of HASHrefs if no symbol given

{
"priceChange": "-94.99999800",
"priceChangePercent": "-95.960",
"weightedAvgPrice": "0.29628482",
"prevClosePrice": "0.10002000",
"lastPrice": "4.00000200",
"bidPrice": "4.00000000",
"askPrice": "4.00000200",
"openPrice": "99.00000000",
"highPrice": "100.00000000",
"lowPrice": "0.10000000",
"volume": "8913.30000000",
"openTime": 1499783499040,
"closeTime": 1499869899040,
"fristId": 28385, // First tradeId
"lastId": 28460, // Last tradeId
"count": 76 // Trade count
}

ticker_price

$api->ticker_price();

Latest price for a symbol or symbols.

PARAMETERS

symbol

[OPTIONAL] Symbol, for example ETHBTC. If not given, returns prices of all symbols.

RETURNS A HASHref

{
"symbol": "LTCBTC",
"price": "4.00000200"
}
OR an ARRAY of HASHrefs
[
{
"symbol": "LTCBTC",
"price": "4.00000200"
},
{
"symbol": "ETHBTC",
"price": "0.07946600"
}
]

book_ticker

$api->book_ticker();

Best price/qty on the order book for all symbols.

PARAMETERS

symbol

[OPTIONAL] Symbol, for example ETHBTC. If not given, returns best price/qty of all symbols.

RETURNS

A HASHref
{
"symbol": "LTCBTC",
"bidPrice": "4.00000000",
"bidQty": "431.00000000",
"askPrice": "4.00000200",
"askQty": "9.00000000"
}
OR an ARRAY of HASHrefs
[
{
"symbol": "LTCBTC",
"bidPrice": "4.00000000",
"bidQty": "431.00000000",
"askPrice": "4.00000200",
"askQty": "9.00000000"
},
{
"symbol": "ETHBTC",
"bidPrice": "0.07946700",
"bidQty": "9.00000000",
"askPrice": "100000.00000000",
"askQty": "1000.00000000"
}
]

all_book_tickers

$api->all_book_tickers();

DEPRECATED: use book_ticker instead.

order

$api->order(
symbol => 'ETHBTC',
side => 'BUY',
type => 'LIMIT',
timeInForce => 'GTC',
quantity => 1
price => 0.1
);

Send in a new order.

PARAMETERS

symbol

[REQUIRED] Symbol, for example ETHBTC.

side

[REQUIRED] BUY or SELL.

type

[REQUIRED] LIMIT|STOP_LOSS|STOP_LOSS_LIMIT|TAKE_PROFIT|TAKE_PROFIT_LIMIT|LIMIT_MAKER|MARKET.

timeInForce

[OPTIONAL] GTC or IOC.

quantity

[OPTIONAL] Quantity (of symbols) in order.

quoteOrderQty

[OPTIONAL] MARKET orders using quoteOrderQty specifies the amount the user wants to spend (when buying) or receive (when selling) the quote asset; the correct quantity will be determined based on the market liquidity and quoteOrderQty.

E.g. Using the symbol BTCUSDT: BUY side, the order will buy as many BTC as quoteOrderQty USDT can. SELL side, the order will sell as much BTC needed to receive quoteOrderQty USDT.

price

[OPTIONAL] Price (of symbol) in order.

newClientOrderId

[OPTIONAL] A unique id for the order. Automatically generated if not sent.

stopPrice

[OPTIONAL] Used with stop orders.

icebergQty

[OPTIONAL] Used with iceberg orders.

newOrderRespType

[OPTIONAL] Set the response JSON. ACK, RESULT, or FULL; MARKET and LIMIT order types default to FULL, all other orders default to ACK.

test

[OPTIONAL] Test new order creation and signature/recvWindow long. Creates and validates a new order but does not send it into the matching engine.

RETURNS A HASHref

{
"symbol":"LTCBTC",
"orderId": 1,
"clientOrderId": "myOrder1" // Will be newClientOrderId
"transactTime": 1499827319559
}

order_test

$api->order_test();

Test new order creation and signature/recvWindow long. Creates and validates a new order but does not send it into the matching engine.

PARAMETERS

Same as C<order()>.

RETURNS An empty HASHref

{}

cancel_order

$api->cancel_order();

Cancel an active order.

PARAMETERS

symbol

[REQUIRED] Symbol, for example ETHBTC.

orderId

[OPTIONAL]

origClientOrderId

[OPTIONAL]

newClientOrderId

[OPTIONAL] Used to uniquely identify this cancel. Automatically generated by default.

recvWindow

[OPTIONAL]

RETURNS A HASHref

{
"symbol": "LTCBTC",
"origClientOrderId": "myOrder1",
"orderId": 1,
"clientOrderId": "cancelMyOrder1"
}

cancel_open_orders

$api->cancel_open_orders( symbol => 'ETHBTC' );

Cancel all active orders for a given symbol.

PARAMETERS

symbol

[REQUIRED] Symbol, for example ETHBTC.

recvWindow

[OPTIONAL]

RETURNS An ARRAYref of HASHrefs

[
{
"symbol": "ETHBTC",
"origClientOrderId": "myOrder1",
"orderId": 1,
"clientOrderId": "cancelMyOrder1"
}
]

open_orders

$api->open_orders();

Get all open orders on a symbol. Careful when accessing this with no symbol.

PARAMETERS

symbol

OPTIONAL] Symbol, for example ETHBTC.

recvWindow

[OPTIONAL]

RETURNS An ARRAYref of HASHrefs

[
{
"symbol": "LTCBTC",
"orderId": 1,
"clientOrderId": "myOrder1",
"price": "0.1",
"origQty": "1.0",
"executedQty": "0.0",
"status": "NEW",
"timeInForce": "GTC",
"type": "LIMIT",
"side": "BUY",
"stopPrice": "0.0",
"icebergQty": "0.0",
"time": 1499827319559,
"isWorking": trueO
}
]

all_orders

$api->all_orders();

Get all account orders; active, canceled, or filled.

PARAMETERS

symbol

[REQUIRED] Symbol, for example ETHBTC.

orderId

[OPTIONAL]

startTime

[OPTIONAL] Start time

endTime

[OPTIONAL] End time

limit

[OPTIONAL] Default 500; max 1000.

recvWindow

[OPTIONAL] The value cannot be greater than 60000.

RETURNS An ARRAYref of HASHrefs

[
{
"symbol": "LTCBTC",
"orderId": 1,
"clientOrderId": "myOrder1",
"price": "0.1",
"origQty": "1.0",
"executedQty": "0.0",
"status": "NEW",
"timeInForce": "GTC",
"type": "LIMIT",
"side": "BUY",
"stopPrice": "0.0",
"icebergQty": "0.0",
"time": 1499827319559,
"isWorking": true
}
]

account

$api->account();

Get current account information.

PARAMETERS

recvWindow

[OPTIONAL]

RETURNS A HASHref

{
"makerCommission": 15,
"takerCommission": 15,
"buyerCommission": 0,
"sellerCommission": 0,
"canTrade": true,
"canWithdraw": true,
"canDeposit": true,
"updateTime": 123456789,
"balances": [
{
"asset": "BTC",
"free": "4723846.89208129",
"locked": "0.00000000"
},
{
"asset": "LTC",
"free": "4763368.68006011",
"locked": "0.00000000"
}
]
}

my_trades

$api->my_trades();

Get trades for a specific account and symbol.

PARAMETERS

symbol

[REQUIRED] Symbol, for example ETHBTC.

limit

[OPTIONAL] Default 500; max 500.

fromId

[OPTIONAL] TradeId to fetch from. Default gets most recent trades.

recvWindow

[OPTIONAL]

RETURNS An ARRAYref of HASHrefs

[
{
"id": 28457,
"orderId": 100234,
"price": "4.00000100",
"qty": "12.00000000",
"commission": "10.10000000",
"commissionAsset": "BNB",
"time": 1499865549590,
"isBuyer": true,
"isMaker": false,
"isBestMatch": true
}
]

start_user_data_stream

$api->start_user_data_stream();

Start a new user data stream. The stream will close after 60 minutes unless a keepalive is sent.

PARAMETERS

Takes no parameters.

RETURNS A HASHref

{
"listenKey": "pqia91ma19a5s61cv6a81va65sdf19v8a65a1a5s61cv6a81va65sdf19v8a65a1"
}

keep_alive_user_data_stream

$api->keep_alive_user_data_stream();

Keepalive a user data stream to prevent a time out. User data streams will close after 60 minutes. It's recommended to send a ping about every 30 minutes.

PARAMETERS

listenKey

[REQUIRED]

RETURNS An empty HASHref

{}

delete_user_data_stream

$api->delete_user_data_stream();

Close out a user data stream.

PARAMETERS

listenKey

[REQUIRED]

RETURNS An empty HASHref

{}

log

$api->log->warn("This is a warning");

PARAMETERS

Takes no parameters.

RETURNS

An instance of Binance::API::Logger.

ua

$api->ua->get('/binance/endpoint');

PARAMETERS

Takes no parameters.

RETURNS

An instance of Binance::API::Request.