PHP SDK reference
Official zero-dependency PHP 8.1+ SDK (spicyapi/spicyapi): install via Composer, quote tasks, upload files, verify webhooks, and handle errors.
spicyapi/spicyapi is the official PHP SDK for SpicyAPI. It puts image, video and audio generation behind one SpicyClient: it attaches your API key, unwraps the response envelope into plain associative arrays, waits for a task with bounded back-off, uploads local files in one call and verifies webhook signatures without any of the three mistakes PHP code usually makes there.
It requires PHP 8.1 or newer plus ext-json and ext-hash, and has no library dependencies. ext-curl is used when present; without it the client falls back to the https:// stream wrapper.
Is this the right tool?
| You want to… | Use |
|---|---|
| Call SpicyAPI from your own PHP code | This SDK |
| Call it from Node.js or TypeScript | The TypeScript SDK |
| Call it from Python, Go or Java | The Python, Go or Java SDK |
| Generate things from a terminal without writing code | The CLI |
| Let an AI assistant call SpicyAPI for you | The MCP server and the Agent Skill |
| Use a chat or text model | An established PHP client for the OpenAI, Anthropic or Gemini formats — see Text and streaming |
Install it
composer require spicyapi/spicyapi:^0.3Put your key in the environment, so it never appears in your code or in version control:
export SPICY_API_KEY="sk-spicy-..."Your first result
Ask for the price, decide, create the task, wait, read the files. Quoting is free and reserves nothing.
use SpicyApi\SpicyClient;
$client = new SpicyClient(); // reads SPICY_API_KEY from the environment
$quote = $client->quote('MODEL_ID_FROM_CATALOG', ['prompt' => 'a lantern in fog']);
printf("at most %s\n", $quote['maxCharge']); // decide before you spend
$task = $client->createTask(
model: $quote['model'],
input: ['prompt' => 'a lantern in fog'],
idempotencyKey: SpicyClient::idempotencyKey(),
quoteId: $quote['quoteId'],
expectedCost: $quote['estimatedCost'],
);
$final = $client->waitForTerminal($task['taskId']);
foreach (SpicyClient::readyAssets($final) as $asset) {
echo $asset['url'], "\n";
}Every call returns the decoded data of the response envelope as a plain associative array, so read it with $quote['maxCharge'], not $quote->maxCharge.
MODEL_ID_FROM_CATALOG is a placeholder. Take a real ID from the authenticated catalogue — $client->listModels(), which is GET /api/v1/models — and never from memory or an old page: models are added, paused and retired. Build the input from that model's own inputSchema, which listModels() returns; every model accepts different fields.
Amounts are decimal USD strings, never floats, because binary floats cannot hold most decimal amounts exactly. Compare them with SpicyClient::compareUsd(). There are no credits or points anywhere in this API.
waitForTerminal returns on any terminal state, and a terminal state is not a successful one. Check the state before reading output, and read SpicyClient::outputText($final) first for endpoints that answer in text rather than files.
Skip the first poll
waitSeconds holds the connection open until the task finishes, so a short job needs no polling loop at all. It does not promise a finished task. If the budget runs out you get the ordinary accepted response instead — same shape, different state — so branch on the state, never on the fact that you passed the parameter:
$submitted = $client->createTask(
model: 'MODEL_ID_FROM_CATALOG',
input: ['prompt' => 'a lantern in fog'],
idempotencyKey: SpicyClient::idempotencyKey(),
waitSeconds: 30,
);
if (!SpicyClient::isTerminal($submitted)) {
$submitted = $client->waitForTerminal($submitted['taskId']);
}Start from a local file
Image-to-video, subject swap and image editing all need your material on our side first. uploadFile does the three-step upload — ticket, PUT to storage, commit — in one call and returns a spicy:// URI. That URI is what goes into the input.
$uploaded = $client->uploadFile('/path/to/reference.png');
$task = $client->createTask(
model: 'MODEL_ID_FROM_CATALOG',
input: ['image' => $uploaded['uri'], 'prompt' => 'slow dolly in'],
idempotencyKey: SpicyClient::idempotencyKey(),
);A publicly reachable HTTPS URL can go into the input directly and needs no upload at all. Images are accepted to 10 MiB, audio, video and documents (PDF, Word, Excel, PowerPoint, plain text, Markdown and more) to 90 MiB, and .safetensors LoRA weights (UploadContentType::Lora, for loras[].path) to 2 GiB — raise uploadTimeoutSeconds for files that large. See Media. An upload lasts one day — to keep a file, save it to the asset library.
Keep files in the asset library
An upload's spicy://f/ URI lasts one day. Files you use again and again — a character portrait, a style reference, a voice sample — belong in the asset library: each saved file gets a stable uri of the form spicy://a/<id> that works in any file field of a model input until you delete it. How the library, its storage plans and its limits work is explained in Media: Asset library.
The key decides the library. A personal key reaches your personal library; a key created in a team workspace reaches that team's shared library, never anyone's personal one. With a team key, the current role of the member who created it decides what may change: owner and admin anything, developer only what they saved, billing and readonly only read and use. $client->assets->usage()['scope'] tells you which.
use SpicyApi\AssetSource;
// Save the first output of one of your successful tasks. source: 'url' with url: ...,
// and source: 'upload' with fileId: ..., work the same way. The required
// Idempotency-Key is added for you; pass idempotencyKey: to replay a save.
$hero = $client->assets->create(
source: AssetSource::TaskOutput,
taskId: $final['taskId'],
index: 0,
name: 'Hero portrait',
);
// Or upload a local file and save it in one call; the name defaults to the file name.
$voice = $client->assets->uploadFile('/path/to/voice-sample.wav');
// Later, in any request: the uri goes where the model's schema expects a file.
$task = $client->createTask(
model: 'MODEL_ID_FROM_CATALOG',
input: ['image_url' => $hero['uri'], 'prompt' => 'slow push-in, light rain'],
idempotencyKey: SpicyClient::idempotencyKey(),
);
// Every saved image whose name contains "portrait", across all pages.
foreach ($client->assets->iterAll(kind: 'image', q: 'portrait') as $saved) {
echo $saved['uri'], ' ', $saved['name'], "\n";
}| Method | What it does |
|---|---|
assets->create(source:, taskId:, key:, index:, fileId:, url:, name:, idempotencyKey:) | Saves a file. source is AssetSource::TaskOutput (with taskId and index or key), AssetSource::Upload (with fileId, fil_… or spicy://f/…) or AssetSource::Url (with a public http/https url), or the same values as strings. Saving the same source again returns the asset you already have |
assets->uploadFile($path, name:, contentType:) | Uploads a local file and saves it in one call |
assets->list(kind:, source:, q:, favorite:, collectionId:, entityId:, sort:, cursor:, limit:) | One page: items and nextCursor (absent on the last page) |
assets->iterAll(...) | A generator over every matching asset, with the same filters as list |
assets->get($assetId) | One asset with a fresh previewUrl |
assets->update($assetId, name:, favorite:) | Renames, marks or unmarks a favorite; only what you pass changes and the uri stays the same |
assets->delete($assetId) | Deletes for good; returns ['id' => ..., 'deleted' => true] and the uri stops working at once |
assets->usage() | scope (which library, role, canWrite, canTopUp), the plan, what is stored, the limits and today's count |
assets->characters->list(assetId:, cursor:, limit:), iterAll(...), get($characterId) | Your characters, read-only, each with its images in order |
- An asset ID can be the bare
ast_…ID or thespicy://a/…reference. An empty ID, a blank filter, an unknownsortorsource, or a save that mixes two sources throwsInvalidArgumentExceptionbefore anything is sent. favorite: falselists the assets that are not favorites; leave itnullto skip the filter.sortisAssetSort::Recent(default),Oldest,NameorSize, or the same values as strings, and anextCursoronly works with the sort it came from.iterAll()keeps your filters and sort on every page.previewUrlis a private preview signed for at most 20 minutes. Show it; storeuri.- When
scope['canTopUp']is false (a team key not created by the owner),lifetimeTopupUsdandtopupToNextTierUsdare missing fromusage(), so read them with?? null. - Characters are named, ordered sets of images, made on the Assets page of the web app. To use one, put the
uriof its images, in order, into the model's reference-image field:array_column($client->assets->characters->get($characterId)['assets'], 'uri'). Oldspicy://p/references keep working in the personal library. See Media: Characters.
Webhooks
use SpicyApi\SpicyWebhook;
// The secret comes first. The other arguments are optional: $_SERVER and the raw bytes
// of php://input are read for you unless a framework already consumed them.
$delivery = SpicyWebhook::verifyRequest(getenv('SPICY_WEBHOOK_SECRET') ?: '');
// Inside a framework that buffers the body, hand it the exact bytes yourself:
$delivery = SpicyWebhook::verifyRequest(
secret: getenv('SPICY_WEBHOOK_SECRET') ?: '',
server: $_SERVER,
rawBody: $request->getContent(),
);Three ways PHP code gets this wrong, all of which this class avoids:
- Re-serialising the body before verifying.
json_decodethenjson_encodechanges the bytes, and the signature will never match. Verify the raw bytes. - Comparing signatures with
==or===. Beyond timing,==on two strings that both look like0e…compares them as numbers —"0e123" == "0e456"istrue. This useshash_equals. - Skipping the timestamp check. A valid signature stays valid forever; without a freshness window, a captured delivery can be replayed.
Deliveries are retried, so deduplicate on the delivery ID. Full delivery format and retry schedule are in Webhooks.
Balance, usage and history
$balance = $client->getBalance(); // available / held / total, USD strings
$usage = $client->getUsage('2026-09-01', '2026-09-08'); // settled spend over [from, to)
foreach ($client->iterTasks(state: 'succeeded') as $summary) {
echo $summary['taskId'], "\n"; // every page, filters repeated for you
}held is money reserved by tasks that have not settled, so it is neither spent nor available. Dates are UTC calendar days, YYYY-MM-DD, forming a half-open [from, to) interval of at most 92 days. All three calls see only the calling API key: sibling keys and console generations are not included.
getUsage() is for reconciliation, not for progress. It carries an account-wide rate budget shared by every key, and that limiter fails closed — polling it can lock the whole account out of its own reporting. Follow a task with getTask() instead.
listTasks() returns one page as {items, hasMore, nextCursor}. Paginating by hand means repeating every filter unchanged and moving only the cursor, and stopping when hasMore is false or nextCursor is empty — checking only hasMore turns a server-side glitch into an endless loop of paid calls that looks like a hang. iterTasks() does all of that.
Errors
Failures throw SpicyApiError carrying $businessCode, $requestId, $retryAfterSeconds and $reason. Branch on the business code (and $reason when it is set), never on the message — messages are translated, codes are not.
HTTP 503 is shared by three different business codes, so the status alone cannot tell you what to do:
| Code | Meaning | What to do |
|---|---|---|
40003 | uploaded bytes do not match their ticket | start over from a fresh ticket |
40004 | no deployment serves that parameter combination | change the parameter named in the message |
40201 | not enough balance | add funds |
409 | the idempotency key was reused for a different request | resend the original request under that key, or the changed one under a new key |
40901 | the price moved before the task was created | quote again, keep the same idempotency key |
503 | a dependency is briefly unavailable | back off by Retry-After |
50301 | no usable deployment or price right now | refresh the catalogue |
50302 | a synchronous generation failed upstream and was refunded | retry with a new idempotency key |
$error->guidance() returns the same advice at runtime. Every code is listed in Errors.
Asset library failures also carry a $reason, a fixed word that says exactly why: asset_library_locked, asset_library_full and asset_file_too_large (400, plan limits — read $client->assets->usage() rather than retrying), asset_daily_limit (429, resets at 00:00 UTC, with $retryAfterSeconds), asset_storage_full (503, temporary), asset_permission_denied (403, your team role does not allow the change), asset_workspace_forbidden (403, this key cannot reach that library) and asset_entity_name_taken (409). $error->assetErrorReason() returns the matching AssetErrorReason case, or null; $reason keeps any other value the server sends unchanged, so keep a default branch. guidance() answers a known reason first. A refusal whose Retry-After is longer than the request has left, such as the daily limit, is thrown at once. What to do about each reason is in Errors.
use SpicyApi\AssetErrorReason;
use SpicyApi\SpicyApiError;
try {
$client->assets->create(source: 'url', url: 'https://example.com/still.png');
} catch (SpicyApiError $error) {
if ($error->assetErrorReason() !== AssetErrorReason::LibraryFull) {
throw $error;
}
$usage = $client->assets->usage();
printf("Full on %s; next plan: %s\n", $usage['tier']['name'], $usage['nextTier']['name'] ?? 'none');
}Two things that will save you money
Keep one idempotency key per submission, and reuse it for every resend — a lost response does not prove the task was not created. See Idempotency.
A task that succeeds is charged, even if the result disappoints. quote() reserves nothing and creates nothing.
What this package does not do
- Text models. They speak the OpenAI, Anthropic and Gemini wire formats; the established PHP clients for those already work against
https://api.spicyapi.ai/v1. See Text and streaming. - Browser-side code. The key belongs on your server, never in anything you ship to a visitor.
- Choosing a model or filling in its fields. It hands you the catalogue and each model's schema; you decide what to send.
- Cancelling a task. Once a task is accepted it runs to the end.
Source
The package is MIT-licensed and developed in the open at github.com/spicyapi-ai/spicy-php. Its README is the shortest complete tour of the client, and examples/ holds runnable scripts; this page adds the surrounding concepts.
Next steps
Model catalog and schemas
How to read a model's inputSchema and choose valid inputs
Asynchronous tasks
Task states, polling and what each field means
Quotes and compatibility
See the exact price of a request before you pay it
Webhooks
Delivery format, retry schedule and signature verification
Errors
Every error code and which ones are worth retrying
Java SDK
The same workflow from Java, with a sealed error taxonomy