API
PHP example
Call the MaxLabs API with PHP and cURL.
Send a completion
<?php
$baseUrl = getenv('MAXLABS_BASE_URL') ?: 'https://api.maxlabs.ai/v1';
$apiKey = getenv('MAXLABS_API_KEY');
$payload = [
'model' => 'worker',
'messages' => [
['role' => 'user', 'content' => 'Summarize this incident in five bullets.'],
],
'max_tokens' => 600,
];
$curl = curl_init($baseUrl . '/chat/completions');
curl_setopt_array($curl, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 120,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
'Idempotency-Key: ' . bin2hex(random_bytes(16)),
'X-Session-Id: ' . bin2hex(random_bytes(16)),
],
CURLOPT_POSTFIELDS => json_encode($payload, JSON_THROW_ON_ERROR),
]);
$body = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
if ($body === false) {
throw new RuntimeException(curl_error($curl));
}
$data = json_decode($body, true, flags: JSON_THROW_ON_ERROR);
if ($status < 200 || $status >= 300) {
$message = $data['error']['message'] ?? 'MaxLabs request failed';
throw new RuntimeException($message, $status);
}
echo $data['choices'][0]['message']['content'];
Use a connect timeout and a total timeout that match your application. Keep the idempotency key if you retry an uncertain request. Replace it only when you start a new logical request.
