adman9000 / laravel-binance
Laravel implementation of the Binance crypto exchange trading API
2.0.0
2026-06-07 19:00 UTC
Requires
- php: ^8.1
- illuminate/http: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0
- phpunit/phpunit: ^10.0|^11.0
This package is auto-updated.
Last update: 2026-06-07 19:09:20 UTC
README
Laravel implementation of the Binance Spot trading API.
Requirements
- PHP 8.1+
- Laravel 10, 11, or 12
Install
composer require adman9000/laravel-binance
Publish the config file:
php artisan vendor:publish --provider="adman9000\binance\BinanceServiceProvider"
Add your credentials to .env:
BINANCE_KEY=your-api-key
BINANCE_SECRET=your-api-secret
Usage
Via facade
use Binance; $balances = Binance::getBalances(); $price = Binance::getTickers('BTCUSDT');
Via dependency injection
use adman9000\binance\BinanceAPI; class TradingService { public function __construct(private BinanceAPI $binance) {} }
Direct instantiation
$binance = new BinanceAPI([ 'auth' => ['key' => 'your-key', 'secret' => 'your-secret'], 'urls' => ['api' => 'https://api.binance.com/api/', 'sapi' => 'https://api.binance.com/sapi/'], 'settings' => ['timing' => 5000, 'timeout' => 30, 'connect_timeout' => 10], ]);
Available Methods
Public (no authentication required)
| Method | Description |
|---|---|
getServerTime() |
Server timestamp in milliseconds |
getTickers(string $symbol = '') |
All symbol prices, or a single price |
getMarkets() |
Exchange trading rules and symbol info |
getOrderBook(string $symbol, int $limit = 100) |
Bids and asks for a symbol |
getPublicTrades(string $symbol, int $limit = 500) |
Recent trades for a symbol |
getAggTrades(string $symbol, int $limit = 500) |
Compressed/aggregate trade list |
getCandlesticks(string $symbol, string $interval = '1h', int $limit = 500) |
Kline/candlestick data |
getAvgPrice(string $symbol) |
Current average price |
getTickerChange(string $symbol = '') |
24hr rolling window price change |
getBookTicker(string $symbol = '') |
Best price/qty on the order book |
Valid $interval values: 1s, 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M
Private (API key + secret required)
| Method | Description |
|---|---|
getBalances() |
All account balances |
getBalance(string $asset) |
Balance for a single asset (e.g. 'BTC'), or null if not found |
getRecentTrades(string $symbol, int $limit = 500) |
Your trade history for a symbol |
getOpenOrders(string $symbol = '') |
Current open orders |
getAllOrders(string $symbol) |
All orders for a symbol |
marketBuy(string $symbol, string $quantity) |
Market buy order |
marketSell(string $symbol, string $quantity) |
Market sell order |
limitBuy(string $symbol, string $quantity, float $price) |
Limit buy order |
limitSell(string $symbol, string $quantity, float $price) |
Limit sell order |
trade(string $symbol, string $quantity, string $side, string $type, ?float $price) |
Raw order placement |
depositAddress(string $coin) |
Deposit address for an asset |
Configuration
// config/binance.php return [ 'auth' => [ 'key' => env('BINANCE_KEY', ''), 'secret' => env('BINANCE_SECRET', ''), ], 'urls' => [ 'api' => env('BINANCE_API_URL', 'https://api.binance.com/api/'), 'sapi' => env('BINANCE_SAPI_URL', 'https://api.binance.com/sapi/'), ], 'settings' => [ 'timing' => env('BINANCE_TIMING', 5000), // recvWindow in ms 'timeout' => env('BINANCE_TIMEOUT', 30), // request timeout in seconds 'connect_timeout' => env('BINANCE_CONNECT_TIMEOUT', 10), // connection timeout in seconds ], ];
Testnet
To use the Binance testnet, add to your .env:
BINANCE_API_URL=https://testnet.binance.vision/api/
BINANCE_SAPI_URL=https://testnet.binance.vision/sapi/
Binance US
BINANCE_API_URL=https://api.binance.us/api/
BINANCE_SAPI_URL=https://api.binance.us/sapi/
Error handling
All API errors throw adman9000\binance\Exceptions\BinanceApiException:
use adman9000\binance\Exceptions\BinanceApiException; try { $order = Binance::limitBuy('BTCUSDT', '0.001', 1.0); } catch (BinanceApiException $e) { // $e->getMessage() — Binance error message // $e->getResponse() — full response array including error code }
Upgrading from v1
getTicker($symbol)is deprecated — usegetTickers($symbol)insteadgetCurrencies()has been removed (it returnedfalse)depositAddress($symbol)now takes a coin ticker as$coin(same value, renamed parameter)wapiconfig URL removed — replaced bysapi(https://api.binance.com/sapi/)ext-curlis no longer required- Errors now throw
BinanceApiExceptioninstead of\Exception
Running tests
composer install ./vendor/bin/phpunit
Licence
MIT