Documentation Index
Fetch the complete documentation index at: https://docs.legnext.ai/llms.txt
Use this file to discover all available pages before exploring further.
Getting Your API Key
- Visit Legnext.ai Dashboard
- Navigate to API Keys section
- Generate a new API key
- Copy and store securely
Authentication Methods
Method 1: Environment Variables (Recommended)
Set your API key as an environment variable:
export LEGNEXT_API_KEY="your-api-key-here"
Then use it in your PHP code:
<?php
require_once(__DIR__ . '/vendor/autoload.php');
use OpenAPI\Client\Configuration;
use OpenAPI\Client\Api\ImageApi;
// Configure API client
$config = OpenAPI\Client\Configuration::getDefaultConfiguration();
$config->setApiKey('x-api-key', getenv('LEGNEXT_API_KEY'));
$config->setHost('https://api.legnext.ai');
// Create API instance
$api = new OpenAPI\Client\Api\ImageApi(
new GuzzleHttp\Client(),
$config
);
// Now you can use the API
// ...
?>
Method 2: Direct Initialization
Hardcode the API key directly (not recommended for production):
<?php
require_once(__DIR__ . '/vendor/autoload.php');
use OpenAPI\Client\Configuration;
use OpenAPI\Client\Api\ImageApi;
$config = OpenAPI\Client\Configuration::getDefaultConfiguration();
$config->setApiKey('x-api-key', 'your-api-key-here');
$config->setHost('https://api.legnext.ai');
$api = new OpenAPI\Client\Api\ImageApi(
new GuzzleHttp\Client(),
$config
);
?>
Method 3: Using vlucas/phpdotenv
Use the vlucas/phpdotenv package to load from .env files:
composer require vlucas/phpdotenv
<?php
require_once(__DIR__ . '/vendor/autoload.php');
use Dotenv\Dotenv;
use OpenAPI\Client\Configuration;
use OpenAPI\Client\Api\ImageApi;
// Load .env file
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
// Configure API client
$config = OpenAPI\Client\Configuration::getDefaultConfiguration();
$config->setApiKey('x-api-key', $_ENV['LEGNEXT_API_KEY']);
$config->setHost('https://api.legnext.ai');
$api = new OpenAPI\Client\Api\ImageApi(
new GuzzleHttp\Client(),
$config
);
?>
.env file:
LEGNEXT_API_KEY=your-api-key-here
Method 4: Configuration File
Load API key from a configuration file:
<?php
require_once(__DIR__ . '/vendor/autoload.php');
use OpenAPI\Client\Configuration;
use OpenAPI\Client\Api\ImageApi;
// Load configuration from file
$config_data = include('config.php');
$config = OpenAPI\Client\Configuration::getDefaultConfiguration();
$config->setApiKey('x-api-key', $config_data['api_key']);
$config->setHost('https://api.legnext.ai');
$api = new OpenAPI\Client\Api\ImageApi(
new GuzzleHttp\Client(),
$config
);
?>
config.php:
<?php
return [
'api_key' => 'your-api-key-here',
'environment' => 'development',
];
?>
Method 5: Framework Integration (Laravel)
For Laravel applications, use environment variables:
.env:
LEGNEXT_API_KEY=your-api-key-here
config/legnext.php:
<?php
return [
'api_key' => env('LEGNEXT_API_KEY'),
'host' => env('LEGNEXT_HOST', 'https://api.legnext.ai'),
];
?>
Usage in Laravel:
<?php
namespace App\Services;
use OpenAPI\Client\Configuration;
use OpenAPI\Client\Api\ImageApi;
class LegnextService
{
protected $api;
public function __construct()
{
$config = OpenAPI\Client\Configuration::getDefaultConfiguration();
$config->setApiKey('x-api-key', config('legnext.api_key'));
$config->setHost(config('legnext.host'));
$this->api = new ImageApi(
new \GuzzleHttp\Client(),
$config
);
}
public function generateImage($text)
{
// Implementation
}
}
?>
Complete Configuration Example
<?php
require_once(__DIR__ . '/vendor/autoload.php');
use OpenAPI\Client\Configuration;
use OpenAPI\Client\Api\ImageApi;
$config = OpenAPI\Client\Configuration::getDefaultConfiguration();
// Required: API Key
$config->setApiKey('x-api-key', getenv('LEGNEXT_API_KEY'));
// Required: API Host
$config->setHost('https://api.legnext.ai');
// Optional: Set debugging mode
$config->setDebug(getenv('LEGNEXT_DEBUG') === 'true');
// Optional: Set timeout (in seconds)
$config->setCurlTimeout(120);
// Optional: Set custom user agent
$config->setUserAgent('MyApp/1.0');
// Optional: SSL verification (recommended: true)
$config->setSSLVerification(true);
$api = new OpenAPI\Client\Api\ImageApi(
new GuzzleHttp\Client(),
$config
);
?>
Security Best Practices
-
Never commit API keys to version control
# .gitignore
.env
config.php
-
Use environment variables in production
# Set in your deployment environment
export LEGNEXT_API_KEY="your-production-key"
-
Rotate keys regularly
- Generate new keys periodically
- Revoke old keys after rotation
-
Use different keys for development and production
<?php
$environment = getenv('APP_ENV') ?: 'development';
if ($environment === 'production') {
$apiKey = getenv('LEGNEXT_PROD_API_KEY');
} else {
$apiKey = getenv('LEGNEXT_DEV_API_KEY');
}
$config->setApiKey('x-api-key', $apiKey);
?>
-
Restrict API key permissions
- Use the minimum required permissions
- Monitor usage through the dashboard
-
Enable SSL verification
<?php
$config->setSSLVerification(true);
?>
-
Protect configuration files
<?php
// config.php
if (!defined('APP_ACCESS')) {
die('Direct access not permitted');
}
return [
'api_key' => getenv('LEGNEXT_API_KEY'),
];
?>
Testing Authentication
Verify your API key is working:
<?php
require_once(__DIR__ . '/vendor/autoload.php');
use OpenAPI\Client\Configuration;
use OpenAPI\Client\Api\AccountManagementApi;
$apiKey = getenv('LEGNEXT_API_KEY');
$config = OpenAPI\Client\Configuration::getDefaultConfiguration();
$config->setApiKey('x-api-key', $apiKey);
$config->setHost('https://api.legnext.ai');
$accountApi = new OpenAPI\Client\Api\AccountManagementApi(
new GuzzleHttp\Client(),
$config
);
try {
// Test with account balance endpoint
$response = $accountApi->apiAccountBalanceGet($apiKey);
echo "✅ Authentication successful!\n";
echo "Balance: " . $response->getBalance() . " credits\n";
} catch (OpenAPI\Client\ApiException $e) {
if ($e->getCode() === 401) {
echo "❌ Authentication failed: Invalid API key\n";
} else {
echo "❌ Error: " . $e->getMessage() . "\n";
}
}
?>
Debugging
Enable debugging to see HTTP requests and responses:
<?php
$config = OpenAPI\Client\Configuration::getDefaultConfiguration();
$config->setApiKey('x-api-key', getenv('LEGNEXT_API_KEY'));
$config->setHost('https://api.legnext.ai');
// Enable debug mode
$config->setDebug(true);
// Set debug file (optional)
$config->setDebugFile('/path/to/debug.log');
$api = new OpenAPI\Client\Api\ImageApi(
new GuzzleHttp\Client(),
$config
);
?>
Error Handling
<?php
try {
$response = $api->generateImage($request);
} catch (OpenAPI\Client\ApiException $e) {
$statusCode = $e->getCode();
$errorBody = $e->getResponseBody();
switch ($statusCode) {
case 401:
echo "Authentication error: Invalid API key\n";
break;
case 400:
echo "Validation error: " . $errorBody . "\n";
break;
case 404:
echo "Resource not found\n";
break;
case 429:
echo "Rate limit exceeded. Please retry later.\n";
break;
case 500:
case 502:
case 503:
echo "Server error: " . $errorBody . "\n";
break;
default:
echo "API error ({$statusCode}): " . $errorBody . "\n";
}
}
?>
Next Steps