This example submits a GPT Image 2 task from Node.js and polls until the task reaches a terminal state.
- Loading
APIDOT_API_KEYfrom the environment or the repo-root.envfile. - Submitting an async image generation request with native
fetch. - Storing the returned
data.task_id. - Polling
/api/generate/status/{task_id}until the result isfinishedorfailed.
- Node.js 18 or newer.
- An APIDot API key stored server-side.
- No npm dependencies are required.
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/webhookNo 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 startThe script uses native fetch, so Node.js 18 or newer is required.
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"
}
]
}
}
}- Persist
data.task_idbefore polling or waiting for callbacks. - Keep API keys out of browser code and public repositories.
- Set
APIDOT_CALLBACK_URLonly 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.
- 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.
- cURL quickstart: ../../curl/image/gpt-image-2.md
- Polling guide: ../../polling/task-status.md
- Webhook examples: ../../webhooks
- GPT Image 2 docs: https://apidot.ai/docs/gpt-image-2
- GPT Image 2 landing page: https://apidot.ai/models/gpt-image-2