Skip to content

Laravel Integration

Igor Sazonov edited this page Jul 21, 2026 · 1 revision

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

Configuration

After installing the package via Composer, you need to set your API token in your Laravel .env file.

COINQUANT_TOKEN=your_access_token_here

Publishing the Configuration File (Optional)

By 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-config

This 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'),
];

Using the Facade

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

Dependency Injection

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...
    }
}

Manual Registration (Legacy/Disabled Auto-discovery)

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

References

[1] CoinQuant PHP SDK README

Clone this wiki locally