Installation:
composer require ewertondaniel/bitfinex-php-sdk
Add the service provider to config/app.php:
'providers' => [
// ...
EwertonDaniel\Bitfinex\BitfinexServiceProvider::class,
],
Publish Config (if needed):
php artisan vendor:publish --provider="EwertonDaniel\Bitfinex\BitfinexServiceProvider"
Configure API keys in .env:
BITFINEX_API_KEY=your_api_key
BITFINEX_API_SECRET=your_api_secret
First Use Case: Fetch platform status (public endpoint):
use EwertonDaniel\Bitfinex\Facades\Bitfinex;
$status = Bitfinex::public()->platformStatus();
dd($status->content->status); // "operative" or "maintenance"
Bitfinex::public() for read-only operations (e.g., market data).Bitfinex::authenticated() for trading/private operations (requires config).Public Data Fetching:
// Get ticker for BTC/USD
$ticker = Bitfinex::public()->ticker('BTC/USD');
$lastPrice = $ticker->content->last_price;
// Fetch historical candles (OHLCV)
$candles = Bitfinex::public()->candles('BTC/USD', '1m', 100);
Authenticated Operations:
// Generate a token (for session management)
$token = Bitfinex::authenticated()->generateToken();
// Place a new order
$order = Bitfinex::authenticated()->orders()->newOrder([
'symbol' => 'BTC/USD',
'amount' => 0.1,
'price' => 50000,
'type' => 'exchange limit',
]);
Webhook Integration:
Use Laravel's queue:work to process Bitfinex webhook payloads:
// In a controller/middleware
$payload = json_decode(request()->getContent(), true);
Bitfinex::authenticated()->webhooks()->validate($payload);
Cache::remember).try {
$data = Bitfinex::public()->ticker('INVALID');
} catch (\EwertonDaniel\Bitfinex\Exceptions\BitfinexException $e) {
Log::error('Bitfinex API error: ' . $e->getMessage());
// Fallback logic
}
dispatch(new FetchCandlesJob('BTC/USD', '1h', 1000));
| Use Case | Pattern |
|---|---|
| Market Data | Bitfinex::public()->ticker('symbol') or candles('symbol', 'timeframe', count) |
| Order Management | Bitfinex::authenticated()->orders()->newOrder() + cancelOrder() |
| Wallet Balances | Bitfinex::authenticated()->wallets()->get() |
| Webhooks | Validate payloads via validate() and dispatch events |
API Key Leaks:
.env and validate with:
if (!config('bitfinex.api_key')) {
throw new \RuntimeException('Bitfinex API key not configured.');
}
Time Synchronization:
use Carbon\Carbon;
$timestamp = Carbon::now()->timestamp;
Webhook Validation:
$isValid = Bitfinex::authenticated()->webhooks()->validate($payload);
if (!$isValid) abort(403, 'Invalid webhook signature');
Rate Limit Headers:
X-RateLimit-Remaining in responses. Implement exponential backoff:
$remaining = $response->header('X-RateLimit-Remaining');
if ($remaining <= 10) sleep(1); // Throttle
Bitfinex::setDebug(true); // Logs raw requests/responses
Custom Endpoints: Extend the SDK by creating a new facade method:
// app/Providers/BitfinexServiceProvider.php
public function boot()
{
Bitfinex::extend('custom', function () {
return new CustomBitfinexClient();
});
}
Then use:
Bitfinex::custom()->yourMethod();
Middleware: Add request/response filters:
Bitfinex::authenticated()->getClient()->getMiddleware()->push(
function ($request) {
$request->withHeader('X-Custom-Header', 'value');
}
);
Testing:
Use the BitfinexMock class for unit tests:
$mock = new BitfinexMock();
$mock->shouldReceive('platformStatus')->andReturn(['status' => 'operative']);
Bitfinex::swap($mock);
BITFINEX_ (e.g., BITFINEX_API_KEY).
Example .env:
BITFINEX_API_KEY=your_key
BITFINEX_API_SECRET=your_secret
BITFINEX_TESTNET=false
Bitfinex::setTestnet(true);
Or in .env:
BITFINEX_TESTNET=true
How can I help you explore Laravel packages today?