Core concepts
Jobs and polling
Create a job, follow its status and read the result.
A job is one run of a model. You create it with POST /v1/jobs and read it with GET /v1/jobs/{id} until it finishes.
Creating a job
A job needs three things:
model: the model ID fromGET /v1/models.input: an object that matches the model's input schema.max_charge_amount: the most this job may cost, as a six-decimal USD string. See Pricing and quotes.
It also needs an Idempotency-Key header, which makes retries safe. See Idempotency and retries. You can add a client_reference_id of up to 200 characters, such as your own order ID. It is stored on the job and returned with it.
The API checks the input, works out the price and sets it aside from your wallet, then answers 202 Accepted with the job. Generation hasn't started yet at that point.
Statuses
| Status | Final | Meaning |
|---|---|---|
queued | No | Waiting for capacity. It can return here from running during a retry. |
running | No | Generating. |
cancel_requested | No | Cancel recorded; the job has not settled yet. |
succeeded | Yes | Finished. Results are in output.assets. |
failed | Yes | Finished without a result. Not charged. |
canceled | Yes | Stopped by a cancel. |
Once a job is succeeded, failed or canceled, its status never changes again. Its billing and result availability still can, for example when the charge settles.
Why a job is waiting
While a job is queued, wait_reason says why:
| wait_reason | Meaning |
|---|---|
account_capacity | Waiting for a free slot on your account. Every new job starts here, and it is usually brief unless your account is already running 2 jobs. waiting_expires_at says when it gives up. |
provider_capacity | The job has left your account queue and is being sent to, or waiting at, the model provider. |
recovery | The provider can't be reached for now, or a run was interrupted and is being retried. A job may move from running back to queued. |
Polling
Poll GET /v1/jobs/{id} every few seconds until the status is final. Images usually finish in under a minute. Video can take several minutes.
import { Astorie } from "@astorie/client";
const client = new Astorie();
// wait() polls for you and returns when the job is final.
const job = await client.wait("job_3k7qz2m5x4rbd6wn2yhv7tcl4a", {
timeoutMs: 10 * 60_000,
pollIntervalMs: 3_000,
});- Each response carries a
versionnumber that goes up with every change. If you poll from several places, ignore a response whose version is lower than one you already saw. - Don't assume the status only moves forward. A job in
recoverycan go fromrunningback toqueued. - If
wait()times out on your side, the job is not canceled. It keeps running and may still be charged. Cancel it if you no longer want it. - If a job is sent and the provider never confirms it got it, or later no longer recognizes it, the job fails one hour after it was sent with
internal_errorand you get a full refund. A job the provider is still working on isn't cut off, so long videos can run longer.
Listing jobs
GET /v1/jobs returns your account's jobs, newest first, 50 per page by default (up to 100 with limit). Pass next_cursor as cursor to get the next page. See List jobs.
Failed jobs
A failed job is still a normal 200 response when you read it. Its status is failed and error says why. You are not charged for it.
{
"id": "job_3k7qz2m5x4rbd6wn2yhv7tcl4a",
"status": "failed",
"error": {
"code": "content_policy",
"message": "Blocked by content policy. Revise the prompt or media, then submit a new job.",
"retryable": false,
"details": []
},
"billing": {
"state": "released",
"charged_amount": "0.000000",
"released_amount": "0.080000"
}
}See Errors for the list of job error codes.