Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

GPT Image 2 Node.js Example

This example submits a GPT Image 2 task from Node.js and polls until the task reaches a terminal state.

What this example shows

  • Loading APIDOT_API_KEY from the environment or the repo-root .env file.
  • Submitting an async image generation request with native fetch.
  • Storing the returned data.task_id.
  • Polling /api/generate/status/{task_id} until the result is finished or failed.

Requirements

  • Node.js 18 or newer.
  • An APIDot API key stored server-side.
  • No npm dependencies are required.

Environment variables

Use placeholders only. Do not commit real credentials.

APIDOT_API_KEY=YOUR_API_KEY_HERE
# Optional: only set this when you have a real public webhook receiver.
# APIDOT_CALLBACK_URL=https://example.com/api/apidot/webhook

How to run

No install step is required for this example; it uses Node.js native fetch only.

cd node/gpt-image-2
cp ../../.env.example ../../.env
# Edit ../../.env and set APIDOT_API_KEY
npm start

The script uses native fetch, so Node.js 18 or newer is required.

Expected response

The script prints each polling attempt and then the final shortened task response:

{
  "code": 200,
  "data": {
    "task_id": "task-unified-example",
    "status": "finished",
    "output": {
      "files": [
        {
          "file_url": "https://example.com/generated-image.webp",
          "file_type": "image"
        }
      ]
    }
  }
}

Production notes

  • Persist data.task_id before polling or waiting for callbacks.
  • Keep API keys out of browser code and public repositories.
  • Set APIDOT_CALLBACK_URL only after your webhook receiver is reachable from the public internet.
  • Add request timeouts and retry policy in production service code.
  • Avoid logging API keys, private prompts, private media URLs, or callback URLs.

Common mistakes

  • Running the script with Node.js older than 18.
  • Forgetting to set APIDOT_API_KEY.
  • Polling continuously without delay.
  • Assuming every task finishes successfully.
  • Using this server-side example directly in browser code.

Related links