POST /image/generate always returns a task (taskId + optional Public Access Token). The SDK generate() method creates that task and polls until it finishes.
Use this guide when you need to wait for results or poll yourself. For browser UI polling with a Public Access Token, see Browser Polling. For push delivery instead of polling, see Use Webhooks.
Wait in the SDK (simplest path)
On a server (or any place that holds your API key), usegenerate(). It returns a completed result wrapper:
rate() and alt() work the same way: they start a task with sync: false and poll until completion.
Start a task without waiting
UsegenerateAsync() when you need the task ID immediately — for example to return to a client, store the ID, or wait later:
rateAsync(), altAsync(). Those return a TaskAsync without a Public Access Token — poll them from trusted code with your API key.
| Method | Returns | Waits? |
|---|---|---|
generate() / rate() / alt() | Result wrapper | Yes (wait() under the hood) |
generateAsync() / rateAsync() / altAsync() | TaskAsync | No — call task.wait() yourself |
Handle success and failure
task.wait() resolves only when the task status is completed. It throws dedicated errors otherwise:
| Status | Meaning |
|---|---|
pending | Queued or still running |
completed | Finished; result payload is available |
failed | Did not complete; see task errors if present |
image.generate task can still contain per-image failures (images[].status: "failed"). Use result.getImages() for successes only, or result.getImages({ includeFailed: true }) for both.
Poll with REST
Create a generation task (always async at the API):Poll status
GET /tasks/:id/status accepts your API key or the task’s Public Access Token:
completed or failed.
Fetch the result
GET /tasks/:id/result uses the same auth options. It returns id, type, status, and result (null while pending or when a failed task has no result):
Owner-only full task
GET /tasks/:id requires your Mynth API key (not a Public Access Token). Use it when you need the original request, full result, cost, timestamps, or errors:
Public Access Tokens
Generation responses include a task-scoped Public Access Token by default (access.pat.enabled defaults to true). The token:
- is a JWT prefixed with
pat_ - is scoped to one task
- is valid for one hour
- works only on
GET /tasks/:id/statusandGET /tasks/:id/result
taskId and publicAccessToken to clients — see Browser Polling.
rate and alt tasks do not currently return a Public Access Token. Poll those with your API key from trusted server code.
SDK polling behavior
TaskAsync.wait():
| Behavior | Detail |
|---|---|
| Start | Lazy — polling begins on first wait() call |
| Dedup | Multiple wait() calls share one promise |
| Interval | ~2.5s for the first 12s, then ~5s (with jitter) |
| Timeout | 5 minutes → TaskAsyncTimeoutError |
| Status auth | Prefers the task Public Access Token; falls back to the client API key |
| Final fetch | GET /tasks/:id with the client API key after status: "completed" |
| Transient failures | Retries up to 7 times; counter resets after a successful status poll |
| Failed task | status: "failed" → TaskAsyncTaskFailedError |
wait() from code that has the SDK client configured with your key. Browser clients should poll /status and /result with the Public Access Token instead.
Full SDK helpers and error classes: SDK tasks.
REST sync mode for rate and alt
Unlike image generation,POST /image/rate and POST /image/alt default to server-side sync ("sync": true). The API waits up to ~55 seconds and returns results inline when possible.
Set "sync": false (or use rateAsync() / altAsync()) to get a pending task and poll yourself. If sync mode times out, the API returns a 202 pending task — poll the same task endpoints.
Choose a completion strategy
| Approach | Best when |
|---|---|
SDK generate() / wait() | Server code can block until the task finishes |
REST poll /status + /result | You own the HTTP client or poll from the browser with a PAT |
| Webhooks | Backend pipelines, durable side effects, multi-system fan-out |
How async fits generation
- You create a task (
POST /image/generate, orgenerate/generateAsyncin the SDK). - Mynth queues work and returns
taskId(and usually a Public Access Token). - You wait in the SDK, poll
/statusthen/result, or receive a webhook. - On
completed, read images (or rate/alt results) from the result payload.