PHP code example of fluxtokens / fluxtokens-php

1. Go to this page and download the library: Download fluxtokens/fluxtokens-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/ */

    

fluxtokens / fluxtokens-php example snippets




luxTokens\FluxTokens;

$client = new FluxTokens('sk-flux-your-api-key');

$response = $client->chat()->completions()->create([
    'model' => 'gpt-4.1-mini',
    'messages' => [
        ['role' => 'system', 'content' => 'You are a helpful assistant.'],
        ['role' => 'user', 'content' => 'Hello!'],
    ],
]);

echo $response['choices'][0]['message']['content'];

$response = $client->chat()->completions()->create([
    'model' => 'gpt-4.1-mini',
    'messages' => [
        ['role' => 'user', 'content' => 'What is the capital of France?'],
    ],
    'temperature' => 0.7,
    'max_tokens' => 256,
]);

echo $response['choices'][0]['message']['content'];
// Output: "The capital of France is Paris."

$stream = $client->chat()->completions()->stream([
    'model' => 'gemini-2.5-flash',
    'messages' => [
        ['role' => 'user', 'content' => 'Write a haiku about programming.'],
    ],
]);

foreach ($stream as $chunk) {
    $content = $chunk['choices'][0]['delta']['content'] ?? '';
    echo $content;
    flush();
}

$response = $client->chat()->completions()->create([
    'model' => 'gpt-4.1-mini',
    'messages' => [
        [
            'role' => 'user',
            'content' => [
                ['type' => 'text', 'text' => 'What is in this image?'],
                [
                    'type' => 'image_url',
                    'image_url' => [
                        'url' => 'https://example.com/image.jpg',
                        'detail' => 'high',
                    ],
                ],
            ],
        ],
    ],
    'max_tokens' => 500,
]);

$audioData = base64_encode(file_get_contents('audio.mp3'));

$response = $client->chat()->completions()->create([
    'model' => 'gemini-2.5-flash',
    'messages' => [
        [
            'role' => 'user',
            'content' => [
                ['type' => 'text', 'text' => 'Transcribe this audio:'],
                [
                    'type' => 'input_audio',
                    'input_audio' => [
                        'data' => $audioData,
                        'format' => 'mp3',
                    ],
                ],
            ],
        ],
    ],
]);

$response = $client->chat()->completions()->create([
    'model' => 'gpt-4.1-mini',
    'messages' => [
        ['role' => 'system', 'content' => 'You are a pirate. Always respond in pirate speak.'],
        ['role' => 'user', 'content' => 'How are you today?'],
    ],
]);
// Output: "Ahoy, matey! I be doin' just fine, thank ye fer askin'!"

$models = $client->models()->list();

foreach ($models as $model) {
    echo "{$model['name']} ({$model['provider']})\n";
    echo "  Input: \${$model['input_price']}/1M tokens\n";
    echo "  Output: \${$model['output_price']}/1M tokens\n";
    echo "  Vision: " . ($model['supports_vision'] ? '✅' : '❌') . "\n";
}

$client = new FluxTokens('sk-flux-...', [
    'base_url' => 'https://api.fluxtokens.io', // Custom base URL
    'timeout' => 60,                            // Request timeout in seconds
    'max_retries' => 3,                         // Max retries on rate limit/server errors
]);

use FluxTokens\Exceptions\AuthenticationException;
use FluxTokens\Exceptions\RateLimitException;
use FluxTokens\Exceptions\InsufficientBalanceException;
use FluxTokens\Exceptions\BadRequestException;
use FluxTokens\Exceptions\FluxTokensException;

try {
    $response = $client->chat()->completions()->create([
        'model' => 'gpt-4.1-mini',
        'messages' => [['role' => 'user', 'content' => 'Hello']],
    ]);
} catch (AuthenticationException $e) {
    echo "Invalid API key\n";
} catch (RateLimitException $e) {
    echo "Rate limit exceeded, retry after: " . $e->getRetryAfter() . "\n";
} catch (InsufficientBalanceException $e) {
    echo "Please add credits at https://fluxtokens.io/dashboard/billing\n";
} catch (BadRequestException $e) {
    echo "Invalid request: " . $e->getMessage() . "\n";
} catch (FluxTokensException $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
bash
composer