Skip to main content

1. Install

composer require legnext-api/php-sdk

2. Set API Key

<?php
putenv('LEGNEXT_API_KEY=your-api-key');
?>

3. Basic Example

<?php
require_once(__DIR__ . '/vendor/autoload.php');

$apiKey = getenv('LEGNEXT_API_KEY');

// Configure API client
$config = OpenAPI\Client\Configuration::getDefaultConfiguration();
$config->setHost('https://api.legnext.ai');
$config->setApiKey('x-api-key', $apiKey);

// Create API instance
$api = new OpenAPI\Client\Api\ImageApi(
    new GuzzleHttp\Client(),
    $config
);

try {
    // Generate image
    $body = [
        'text' => 'a beautiful sunset over mountains'
    ];

    $response = $api->apiV1DiffusionPost($apiKey, $body);

    echo "Job ID: " . $response->getJobId() . "\n";
    echo "Status: " . $response->getStatus() . "\n";

} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>

4. Check Task Status

<?php
use OpenAPI\Client\Api\VideoApi;

$taskApi = new VideoApi(
    new GuzzleHttp\Client(),
    $config
);

try {
    $taskResponse = $taskApi->apiV1JobJobIdGet($jobId, $apiKey);

    echo "Status: " . $taskResponse->getStatus() . "\n";
    echo "Task Type: " . $taskResponse->getTaskType() . "\n";

    if ($taskResponse->getOutput() !== null) {
        $output = $taskResponse->getOutput();
        if ($output->getImageUrls() !== null) {
            echo "Images:\n";
            foreach ($output->getImageUrls() as $url) {
                echo "  - " . $url . "\n";
            }
        }
    }
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>

Error Handling

<?php
try {
    $response = $api->apiV1DiffusionPost($apiKey, $body);
} catch (OpenAPI\Client\ApiException $e) {
    echo "HTTP Status Code: " . $e->getCode() . "\n";
    echo "Error Message: " . $e->getMessage() . "\n";
    echo "Response Body: " . $e->getResponseBody() . "\n";
}
?>

Available APIs

  • OpenAPI\Client\Api\ImageApi - Text to image, variations, upscaling, editing
  • OpenAPI\Client\Api\VideoApi - Video generation, upscaling, and task status
  • OpenAPI\Client\Api\AccountManagementApi - Account balance and information