A comprehensive Laravel package for Binance Spot API integration with full account management capabilities.
- Account Management: Get account information, balances, and trading status
- Order Management: Place, cancel, and query orders with full order lifecycle support
- Position Tracking: Real-time position monitoring and historical data
- Market Data: Real-time prices, 24h tickers, and market information
- Security: HMAC-SHA256 signature authentication for all account endpoints
- Rate Limiting: Built-in rate limiting and retry mechanisms
- Error Handling: Comprehensive error handling with detailed logging
- Laravel Integration: Service provider, facades, and configuration
- Testable: Full test suite with mocking capabilities
You can install the package via composer:
composer require MrAbdelaziz/binance-api
You can publish the config file with:
php artisan vendor:publish --tag="binance-api-config"
Add your Binance API credentials to your .env
file:
BINANCE_API_KEY=your_api_key_here
BINANCE_API_SECRET=your_api_secret_here
BINANCE_BASE_URL=https://api.binance.com
BINANCE_TESTNET=false
use MrAbdelaziz\BinanceApi\Facades\BinanceApi;
// Get account information
$account = BinanceApi::account()->getAccountInfo();
// Get account balances
$balances = BinanceApi::account()->getAccountBalances();
// Get account trading status
$status = BinanceApi::account()->getAccountStatus();
// Place a market buy order
$order = BinanceApi::orders()->marketBuy('BTCUSDT', 0.001);
// Place a limit sell order
$order = BinanceApi::orders()->limitSell('BTCUSDT', 0.001, 50000);
// Cancel an order
$cancelled = BinanceApi::orders()->cancelOrder('BTCUSDT', $orderId);
// Get order status
$orderStatus = BinanceApi::orders()->getOrder('BTCUSDT', $orderId);
// Get all orders for a symbol
$orders = BinanceApi::orders()->getAllOrders('BTCUSDT');
// Get current price
$price = BinanceApi::market()->getPrice('BTCUSDT');
// Get 24hr ticker
$ticker = BinanceApi::market()->get24hrTicker('BTCUSDT');
// Get exchange info
$exchangeInfo = BinanceApi::market()->getExchangeInfo();
// Get open positions
$positions = BinanceApi::positions()->getOpenPositions();
// Get position for specific symbol
$position = BinanceApi::positions()->getPosition('BTCUSDT');
// Get position history
$history = BinanceApi::positions()->getPositionHistory('BTCUSDT');
use MrAbdelaziz\BinanceApi\Exceptions\BinanceApiException;
try {
$order = BinanceApi::orders()->marketBuy('BTCUSDT', 0.001);
} catch (BinanceApiException $e) {
// Handle Binance API errors
Log::error('Binance API Error: ' . $e->getMessage());
Log::error('Error Code: ' . $e->getCode());
Log::error('Error Data: ' . json_encode($e->getData()));
}
The package automatically handles rate limiting. You can configure the rate limits in the config file:
'rate_limits' => [
'requests_per_minute' => 1200,
'orders_per_second' => 10,
'orders_per_day' => 200000,
],
// Using custom configuration
$customApi = new BinanceApi([
'api_key' => 'custom_key',
'api_secret' => 'custom_secret',
'base_url' => 'https://testnet.binance.vision',
]);
$account = $customApi->account()->getAccountInfo();
composer test
For detailed documentation visit the following files:
- All account endpoints use HMAC-SHA256 signature authentication
- API keys are never logged or exposed in error messages
- Timestamps are automatically synchronized with Binance servers
- All requests use HTTPS
Please see CONTRIBUTING.md for details.
The MIT License (MIT). Please see License File for more information.