API method
Vary Region (inpaint) via API
In Discord you select an area with a brush and write what should be there. Through the API it is the same, except the area is a mask image. The method is called inpaint.
Updated
Example
There is a dog lying on a couch in the picture. You paint the dog white on a black mask, send the prompt cat, and Midjourney redraws only that area: now it's a cat on the couch, and the rest of the picture stays as it was. It works only on an upscaled image, so the order is imagine, upscale, inpaint.
Request
/midjourney/v2/inpaintBase URL is https://api.userapi.ai. Every request needs two headers, api-key and Content-Type: application/json.
{"hash": "6794f6ef-866a-4bc3-b0bc-7b28ec010d1b","mask": "UklGRgQoAABXR...","prompt": "a ginger cat sleeping","webhook_url": "https://example.com/inpaint/webhook-url","webhook_type": "progress"}
Parameters
| Field | Required | Description |
|---|---|---|
hash | Yes | Hash of a completed upscale task. |
mask | Yes | A black and white PNG in base64. White = redraw, black = keep. Same size as the upscaled image. |
prompt | Yes | What should appear in the white area. |
webhook_url | No | Where we POST the task when its status changes. |
webhook_type | No | result: one call at the end (done or error). progress: also every progress update. |
What comes back
The response is just the task hash, e.g. {"hash": "ed162774-9244-4038-bdf1-801b033809ec"}. The finished task is what you later get from GET /midjourney/v2/status?hash=… or in the webhook:
{"account_hash": "a7d44910-88a6-4673-acc8-d60ffc3479a6","hash": "c688a245-3964-4286-9572-d6c7e22f589a","type": "inpaint","progress": 100,"status": "done","result": {"url": "https://cdn.discordapp.com/...515a41ad678f.png","proxy_url": "https://media.discordapp.net/...515a41ad678f.png","filename": "...-515a41ad678f.png","content_type": "image/png","width": 2048,"height": 2048,"size": 7003237},"status_reason": null,"created_at": "2006-01-02T15:04:05+00:00"}
| status | What it means |
|---|---|
sent | we passed the task on for processing |
waiting | Midjourney accepted it and put it in the queue |
progress | it is running, progress goes from 0 to 100 |
done | finished, the output is in result |
error | failed, see status_reason |
Example with cURL
1. Picture
curl -X POST https://api.userapi.ai/midjourney/v2/imagine \-H "Content-Type: application/json" \-H "api-key: $USERAPI_KEY" \-d '{"prompt": "a dog lying on a cozy couch"}'
2. Upscale
curl "https://api.userapi.ai/midjourney/v2/status?hash=IMAGINE_HASH" \-H "api-key: $USERAPI_KEY"curl -X POST https://api.userapi.ai/midjourney/v2/upscale \-H "Content-Type: application/json" \-H "api-key: $USERAPI_KEY" \-d '{"hash": "IMAGINE_HASH", "choice": 2}'
3. Mask
The size is in result.width and result.height of the upscale task. Black background, white where to redraw, then PNG to base64. A Python example is below.
4. inpaint
curl -X POST https://api.userapi.ai/midjourney/v2/inpaint \-H "Content-Type: application/json" \-H "api-key: $USERAPI_KEY" \-d '{"hash": "UPSCALE_HASH", "mask": "MASK_BASE64", "prompt": "a ginger cat sleeping"}'
Wrappers for Python, Node.js and PHP are on separate pages. If you'd rather not write code, the same methods are available in n8n and Make. This method in the documentation.
Mask in Python
# pip install pillowimport base64, iofrom PIL import Image, ImageDraw# take these from the upscale result: result.width, result.heightwidth, height = 1024, 1024mask = Image.new("L", (width, height), 0) # black: keepImageDraw.Draw(mask).rectangle((300, 420, 760, 900), fill=255) # white: redrawbuf = io.BytesIO()mask.save(buf, format="PNG")mask_b64 = base64.b64encode(buf.getvalue()).decode()
Other methods
| Method | Endpoint (POST /midjourney/v2…) | Input |
|---|---|---|
| Imagine | /imagine | prompt |
| Upscale | /upscale | hash of imagine / blend / describe / animate + choice 1-4 |
| Upsample (Subtle, Creative, 2x, 4x) | /upsample | hash of upscale |
| Variation | /variation | hash of imagine / describe / blend + choice 1-4 |
| Vary (Subtle) / Vary (Strong) | /vary-subtle, /vary-strong | hash of upscale |
| Reroll | /reroll | hash of a task |
| Animate (video) | /animate | hash of upscale + high / low |
| Inpaint (Vary Region) | /inpaint | hash of upscale + mask + prompt |
| Zoom Out | /zoom | hash of upscale + 50 / 75 or prompt |
| Pan | /pan | hash of upscale + direction |
| Blend | /blend | 2-5 image URLs |
| Describe | /describe | image URL |
| Seed | /seed | hash of a task |
| Upload | /upload | PNG / JPG file or URL |
| Info, Speed, Prefer (Remix) | /info, /speed, /prefer | account_hash |
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.
Questions
Is inpaint the same as Vary (Region)?
Yes. In Discord the button is Vary (Region), in our API the method is called inpaint.
What should the mask look like?
A black and white PNG the size of the upscaled image, sent as base64. White is redrawn, black is kept.
Can I inpaint a grid from imagine?
No, upscale one picture first. inpaint only takes the hash of an upscale.
Is there progress for inpaint?
Yes. With webhook_type progress you get the updates in the webhook, or you can watch progress via status.