PHP code example of darknautica / volta-php

1. Go to this page and download the library: Download darknautica/volta-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

darknautica / volta-php example snippets


use DarkNautica\Volta\Facades\Volta;

// Check before calling AI
if (!Volta::hasAccess($user->id)) {
    return response()->json(['error' => 'Insufficient credits'], 402);
}

// Make your AI call here...

// Deduct after success
Volta::charge($user->id, 1);

// Requires 1 credit (default)
Route::post('/generate', [AIController::class, 'generate'])
    ->middleware('volta.gate');

// Requires 3 credits, specific model
Route::post('/analyze', [AIController::class, 'analyze'])
    ->middleware('volta.gate:3,gpt-4');

use DarkNautica\Volta\Exceptions\InsufficientCreditsException;
use DarkNautica\Volta\Exceptions\RateLimitExceededException;

try {
    Volta::charge($user->id, 1);
} catch (InsufficientCreditsException $e) {
    return response()->json(['error' => 'Not enough credits'], 402);
} catch (RateLimitExceededException $e) {
    return response()->json(['error' => 'Slow down', 'retry_after' => $e->retryAfter], 429);
}
bash
composer 
bash
php artisan vendor:publish --tag=volta-config
bash
php artisan volta:test