Skip to main content
This example runs a complete text-to-image request with Node.js 20 or newer. It submits one task, polls that same job_id, and saves the completed image.

1. Set your API key

Create a key in the Legnext dashboard, then add it to your server-side environment:

2. Run the example

Save the following as legnext-example.mjs:
Run it:
The ten-minute wait in this example is a client-side limit, not a service guarantee. In production, persist the job_id before waiting so another process can resume the task.
GET /v1/job/{job_id} does not use the API key. Treat the UUID as a private capability token and keep it behind your server boundary.

3. Put it into production

  • Move the API call behind a server route or background worker.
  • Persist the first successful job_id; do not submit again just because polling or a webhook is delayed.
  • Treat completed and failed as terminal states.
  • Continue waiting through pending, staged, processing, and retry.
  • Copy result files to your own durable storage.
  • Display safe error messages to users and retain job_id for support.
Read Task lifecycle before adding retries or webhooks. See Image Parameters for the model-specific flags accepted in text.