@zerogen/sdk. Its types come
from the API’s OpenAPI document,
and it follows the conventions in these docs for you:
- It sends an
Idempotency-Keywith everyPOST, and reuses it when it retries. - It retries connection failures and errors whose
retryableistrue, and waits forRetry-Afteron a429. - It waits for a generation to end with
Prefer: wait. - It pages through lists, and uploads a file in one call.
Install
Create a generation
Set your API key asZEROGEN_API_KEY, then create a generation and wait for
it to end:
createAndWait creates the generation with Prefer: wait=60, then reads it
with the same wait until it ends. It resolves for every final status,
including failed, so check status and each output’s status. To return
without waiting, call generations.create, then generations.get or
generations.waitFor with the generation’s ID. To stop waiting, pass an
AbortSignal as signal; the generation keeps running.
Each model takes its own inputs and parameters. Read a model’s request schema
with zerogen.models.get("openai/gpt-image-2/generate"). For details, see
Models.
Upload an input
uploads.upload runs the three requests in
Upload a file and resolves with the ready asset’s
ID:
Blob, File, ArrayBuffer, or Uint8Array, and reads the media
type from the file or from the filename’s extension. For files up to 64 MiB,
it sends the file’s SHA-256 digest, so a file the project already has is
reused instead of uploaded again.
List
Iterate a list withfor await to read every item on every page:
data and nextCursor.
Handle errors
A request that ZeroGen refuses throws aZeroGenError, which carries the
error envelope: status, code, param, retryable, details,
and the response’s requestId. Branch on code:
ZeroGenConnectionError.
Retries and idempotency
The library retries a request twice by default. It retries only connection failures and errors whoseretryable is true, and it doesn’t wait for a
Retry-After longer than one minute. Each POST call gets a new
Idempotency-Key, which that call’s retries reuse.
To retry safely across calls or processes, such as from a job queue, pass your
own key:
Configure the client
Every method also takes request options as its last argument:
signal,
timeout, maxRetries, idempotencyKey for a POST, and headers.