n8n
AI image generation in n8n with Midjourney, step by step
Build an n8n workflow that turns a prompt into a Midjourney image, waits for it, upscales it and saves the file, then scale it to a whole Google Sheet of prompts.
Published
This is a walkthrough of one complete workflow: a prompt goes in, a finished Midjourney image ends up in Google Drive. Every node is explained, so you can change it instead of copying it blindly. At the end the same workflow runs over a Google Sheet with hundreds of prompts.
If you only need the installation steps and the list of operations, they are on the Midjourney in n8n page.
What n8n can use to generate images
| Option | Where it comes from | What you get |
|---|---|---|
| OpenAI node, Image → Generate an Image | Built into n8n | DALL·E images, as a binary file by default |
| Google Gemini node, Generate an Image | Built into n8n | Images from Google's models |
UserAPI node (n8n-nodes-userapi) | Community node, verified by n8n | Midjourney: Imagine, Upscale, Variation, Zoom, Pan, Animate and the rest |
Midjourney has no official API, so there is no built-in Midjourney node. Our node calls Midjourney through your own Midjourney account. Because the node is verified, it installs from the nodes panel on n8n Cloud as well as on self-hosted n8n.
The rest of the article is about Midjourney. The workflow logic (start a task, wait, save the file) is the same for most image APIs.
What you need
- n8n 1.80 or newer, Cloud or self-hosted.
- A UserAPI key. Registration takes a minute and the first 50 requests are free, no card needed. Details are in how to get a Midjourney API key.
- A Discord account with a Midjourney subscription, added in the UserAPI dashboard. The first steps page shows where to find its token. Without a Midjourney account there is UserAPI Cloud, which is in early access right now: see Midjourney API without Discord.
Step 1. Install the node and add the key
- On the canvas, open the nodes panel with + or the N key.
- Search for UserAPI. It is under More from the community. Select Install (the instance owner or an admin can do this).
- Add the node, create UserAPI API credentials and paste your key.
Step 2. Start the generation
Add a trigger (start with Manual Trigger, swap it for a form, a schedule or a webhook later), then a UserAPI node with the Imagine operation. Put the prompt in Prompt. Midjourney parameters go into the prompt the same way as in Discord:
a red fox in a snowy forest, cinematic light --ar 3:2 --v 7
Imagine does not return the image. It returns a task hash straight away, and Midjourney works on the task in the background. Everything after this node is about waiting for that task.
Step 3. Wait for the result
The simplest way is a loop of three nodes:
- Wait, 15 seconds.
- UserAPI → Status with the hash from the Imagine node:
{{ $('Generate image grid').item.json.hash }}
- If with the condition
statusequalsdone. The true branch goes on to the next step.
The false branch needs one more If, otherwise a failed task loops forever. It checks two conditions joined with OR. The first one: {{ $json.status }} is equal to error. The second one catches a task that has been running for more than 30 minutes: a Number condition, "is greater than" 30, on
{{ $json.created_at ? $now.diff(DateTime.fromISO($json.created_at), 'minutes').minutes : 0 }}
If either is true, a Stop and Error node ends the run with the reason:
Midjourney task failed: {{ $json.status_reason || $json.status }}
Otherwise the branch goes back to Wait.
How long does it actually take? Across our users' tasks over the last 30 days, half of the Imagine grids were ready within 36 seconds and 90% within 88 seconds. So with a 15-second wait the loop usually runs three or four times. Checking the status is free: only the generation itself counts as a request.
Step 4. Upscale one picture
Imagine gives a grid of four pictures. To get one of them at full size, add UserAPI → Upscale after the true branch of "Is grid ready?":
- Hash:
{{ $json.hash }}, the grid's hash from the Status node. - Choice: 1 to 4, counting left to right, top to bottom.
Upscale is a new task with its own hash, so it needs the same Wait, Status and If loop. It is fast: over the same 30 days, most upscales were done in 2 to 3 seconds.
The same hash also works for Variation, Zoom, Pan and the other actions on a grid or an upscaled image. The upscale guide covers Upscale, Upsample and the Subtle and Creative variants.
Step 5. Save the file
When the task is done, the Status output has result.url with the image and result.width and result.height. The link points to Discord's CDN, which is not permanent storage, so keep the file itself:
- HTTP Request with the URL
{{ $json.result.url }}and, under Options → Response, the format File. - Google Drive → Upload (or S3, Dropbox, an FTP server) with that binary file.
Step 6. Run it over a Google Sheet
One prompt at a time is fine for testing. For product photos, ad creatives or blog covers, put the prompts in a sheet:
| prompt | status | image_url | task_hash |
|---|---|---|---|
| a white sneaker on a concrete floor, soft studio light --ar 4:5 | |||
| a ceramic mug with coffee beans, top view --ar 1:1 |
The workflow changes in three places:
- Google Sheets → Get Rows reads the rows where
statusis empty, so finished rows are skipped on the next run. - Loop Over Items sends the rows one at a time into the same Imagine, Wait, Status and If chain.
- After the upload, Google Sheets → Update Row writes
status=done, the Drive link and the hash. The error branch writeserror:and the reason instead of stopping the whole run, and the loop moves on to the next row.
The run time depends on your Midjourney plan more than on n8n: Midjourney runs only a few jobs at a time on one account, and fast hours run out. If a sheet has thousands of rows, add several Discord accounts to the same API key. Requests without account_hash are then spread across them evenly.
Webhook instead of the loop
Polling is the easiest to build, but every waiting task keeps an execution open. With many images, let us send the result to you:
- Create a second workflow that starts with a Webhook node. Set its HTTP method to POST and copy the production URL.
- In the Imagine node, open Additional Fields and add Webhook URL with that address. Webhook Type set to Result sends only the finished task, Progress also sends the intermediate updates.
- Add Callback ID with your own identifier, a row number for example. It comes back with the result, so the second workflow knows which row to update.
The webhook body is the same JSON as the Status output: status, hash, result.url and the rest.
When a task fails
Over the last 30 days, about one Imagine task in ten ended with status = error. The reason is in status_reason, and it is Midjourney's own message. The most common ones:
- No active Discord account on the key: the account was disconnected or its token expired. Check it in the dashboard.
- Out of fast hours: switch the account to Relax with UserAPI → Speed, or buy more hours in Midjourney.
- Maximum number of concurrent jobs, or the job queue is full: too many tasks on one account at once. Slow down the loop or add accounts.
- A pending moderation message: open Discord and acknowledge Midjourney's message.
- Parameter conflicts in the prompt, such as a parameter that the chosen version does not support.
Sending the same prompt again rarely helps, so write the reason to the sheet or a log and look at it.
What it costs
Each task is one request: an Imagine plus an Upscale is two. On pay-per-request that is $0.04 per request, or $25 a month per Discord account with no request limit. Status checks are free. The first 50 requests are free, which is enough to build and test this workflow. Your Midjourney subscription is paid separately. The full comparison is on the pricing page.
Ready-made workflows
All of the above is available as workflows you can import into n8n: generate and upscale, a Telegram bot, and bulk generation from Google Sheets to Google Drive. Import one, select your UserAPI credentials, and it runs.
Try it on 50 free requests
Register, add a Discord account that has Midjourney, and you get an API key. The first 50 requests are free and you don’t need to enter a card. Since 2023 more than 10,000 people have signed up and sent over 8 million requests through the API.