-
Notifications
You must be signed in to change notification settings - Fork 0
Laravel Integration
The CoinQuant PHP SDK is designed to work seamlessly with Laravel 10, 11, 12, and 13. It leverages Laravel's package auto-discovery to register its Service Provider and Facade automatically, requiring minimal configuration on your end [1].
After installing the package via Composer, you need to set your API token in your Laravel .env file.
COINQUANT_TOKEN=your_access_token_hereBy default, the SDK connects to https://api.coinquant.ai. If you need to override the base URL or explicitly manage the configuration, you can publish the package's configuration file:
php artisan vendor:publish --tag=coinquant-configThis will create a config/coinquant.php file in your application:
<?php
return [
'token' => env('COINQUANT_TOKEN', ''),
'base_url' => env('COINQUANT_BASE_URL', 'https://api.coinquant.ai'),
];The easiest way to interact with the API in Laravel is by using the CoinQuant facade. The facade provides static access to all methods available on the CoinQuantClient.
<?php
namespace App\Http\Controllers;
use CoinQuant\Laravel\Facades\CoinQuant;
use Illuminate\Http\JsonResponse;
class TradingController extends Controller
{
public function checkCredits(): JsonResponse
{
$credits = CoinQuant::getCredits();
return response()->json([
'available' => $credits['available_credits_total']
]);
}
public function generateStrategy(): JsonResponse
{
$result = CoinQuant::prompt('Research BTC volatility this week.');
return response()->json([
'type' => $result->type,
'content' => $result->text
]);
}
}The CoinQuantClient is bound to Laravel's service container as a singleton. This means you can type-hint it in your controllers, jobs, console commands, or any other class resolved by the container, and Laravel will automatically inject the configured instance [1].
<?php
namespace App\Jobs;
use CoinQuant\CoinQuantClient;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class RunBacktestJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(
private string $strategyVersionId
) {}
public function handle(CoinQuantClient $coinquant): void
{
// The client is automatically injected with the token from your config
$outcome = $coinquant->createBacktestAndWait($this->strategyVersionId);
// Process the outcome...
}
}If you are using a Laravel version without package auto-discovery, or if you have disabled it for this package, you must register the service provider and facade manually in your config/app.php file:
'providers' => [
// ...
CoinQuant\Laravel\CoinQuantServiceProvider::class,
],
'aliases' => [
// ...
'CoinQuant' => CoinQuant\Laravel\Facades\CoinQuant::class,
],