PHP SDK
Official PHP 8.1+ client for the route-based LatentKit /v1 API.
Server-side, framework-neutral PHP client for WordPress, Drupal, Laravel, commerce integrations, workers, and scripts.
PHP 0.1 is intentionally non-streaming. It covers connection introspection, chat, vision, embeddings, image generation, and enqueue-only queue requests. JavaScript and Python remain the full-surface SDKs while PHP parity expands.
PHP SDK v0.1.0-beta.3 is available on Packagist. It remains a beta while framework and CMS compatibility testing continues.
Source code and issues are available in the public PHP SDK repository.
Keep LATENTKIT_API_KEY in server-side PHP, an environment variable, or a platform secret store. Never print it into HTML or expose it to browser JavaScript.
Install
Install the current beta:
composer require latentkit/latentkit-php:0.1.0-beta.31. Set your API key
export LATENTKIT_API_KEY="lk_..."2. Send your first request
<?php
require __DIR__ . '/vendor/autoload.php';
use LatentKit\LatentKit;
$client = new LatentKit();
$response = $client->chat->create(
messages: [
['role' => 'user', 'content' => 'Say hello from LatentKit.'],
],
maxTokens: 100,
responseProfile: 'balanced',
);
echo $response['content'];Client options
| Option | Description |
|---|---|
apiKey | Runtime API key; defaults to LATENTKIT_API_KEY |
baseUrl | Defaults to LATENTKIT_BASE_URL, then https://ai.latentkit.com |
timeoutSeconds | Overall default cURL timeout; default 120 |
headers | Extra request headers |
transport | Custom TransportInterface, including Psr18Transport |
toolSlug / toolVersion | Optional attribution for integration authors |
appId | Optional app context for account-scoped tooling credentials |
Route-based requests
Do not pass model, provider, route, or policy. The SDK rejects route-control keys before making a request. The API key's assigned published route selects the provider/model at runtime.
Inspect the connected route
$client->me->retrieveContext() returns a typed, null-safe
ConnectionContext for the authenticated workspace and app, credit balance,
published route with ordered model display metadata, and a prompt-free summary
of the latest winning provider/model:
$context = $client->me->retrieveContext();
echo $context->route?->name;
echo $context->route?->modelCount;
foreach ($context->route?->models ?? [] as $model) {
echo $model->rank . ': ' . $model->provider . ' / ' . $model->model;
}
echo $context->latestRequest?->model;The latest winner is request activity—not a fixed model—because routes can
select differently and fall back. Runtime API keys cannot change routing
through this endpoint. Existing consumers can keep using
$client->me->retrieve() for the raw associative array.
If a compatible gateway returns the ordered models list without the
redundant model_count field, the typed context derives the count from those
models rather than presenting the route as unavailable or empty.
Supported resources
| Resource | Endpoint docs |
|---|---|
$client->me->retrieve() / $client->me->retrieveContext() | Authentication context |
$client->chat->create(...) | Chat |
$client->vision->create(...) | Vision |
$client->embeddings->create(...) | Embeddings |
$client->image->generate(...) | Images |
$client->queue->create(...) | Queue |
Queue results
$queued = $client->queue->create(
endpoint: 'chat',
payload: [
'messages' => [['role' => 'user', 'content' => 'Summarize this.']],
],
idempotencyKey: 'cms-job-42',
);The public API accepts queue submissions but does not expose GET /v1/queue/{job_id}. The PHP SDK therefore has no queue retrieval method. If your workflow needs the result, use the host platform's background-job system and call a synchronous SDK resource from that job.
Errors and request IDs
use LatentKit\Exceptions\ApiException;
try {
$context = $client->me->retrieveContext();
} catch (ApiException $error) {
error_log(json_encode([
'status' => $error->statusCode,
'code' => $error->apiCode,
'request_id' => $error->requestId,
]));
}Authentication, rate-limit, and validation exceptions extend ApiException. Network failures use TransportException; malformed successful responses use InvalidResponseException.
cURL and PSR-18
The default transport uses ext-curl and does not install a concrete HTTP-client library. Laravel, Drupal, or other framework applications may inject Psr18Transport with an existing PSR-18 client and PSR-17 request/stream factories.
PSR-18 does not standardize timeout configuration. Configure timeouts on the injected client. Streaming is not supported by the current PHP beta.
See also the JavaScript SDK, Python SDK, and REST API overview.
Official PHP integrations currently use this beta: WordPress is under directory review, Drupal has a public Drupal.org review branch, and Laravel 0.1.0-alpha.1 is available on Packagist.