mrabdelaziz/binance-api

A comprehensive Laravel package for Binance Spot API integration with account management

v1.0.0 2025-05-22 12:20 UTC

This package is auto-updated.

Last update: 2025-05-22 13:06:16 UTC


README

Latest Version on Packagist GitHub Code Style Action Status Total Downloads

A comprehensive Laravel package for Binance Spot API integration with full account management capabilities.

Features

  • 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

Installation

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

Quick Start

Account Information

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();

Order Management

// 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');

Market Data

// Get current price
$price = BinanceApi::market()->getPrice('BTCUSDT');

// Get 24hr ticker
$ticker = BinanceApi::market()->get24hrTicker('BTCUSDT');

// Get exchange info
$exchangeInfo = BinanceApi::market()->getExchangeInfo();

Position Tracking

// 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');

Advanced Usage

Error Handling

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()));
}

Rate Limiting

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,
],

Custom Configuration

// Using custom configuration
$customApi = new BinanceApi([
    'api_key' => 'custom_key',
    'api_secret' => 'custom_secret',
    'base_url' => 'https://testnet.binance.vision',
]);

$account = $customApi->account()->getAccountInfo();

Testing

composer test

Documentation

For detailed documentation visit the following files:

Security

  • 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

Contributing

Please see CONTRIBUTING.md for details.

License

The MIT License (MIT). Please see License File for more information.