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

POST/midjourney/v2/inpaint

Base 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

FieldRequiredDescription
hashYesHash of a completed upscale task.
maskYesA black and white PNG in base64. White = redraw, black = keep. Same size as the upscaled image.
promptYesWhat should appear in the white area.
webhook_urlNoWhere we POST the task when its status changes.
webhook_typeNoresult: 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"
}
statusWhat it means
sentwe passed the task on for processing
waitingMidjourney accepted it and put it in the queue
progressit is running, progress goes from 0 to 100
donefinished, the output is in result
errorfailed, 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 pillow
import base64, io
from PIL import Image, ImageDraw
# take these from the upscale result: result.width, result.height
width, height = 1024, 1024
mask = Image.new("L", (width, height), 0) # black: keep
ImageDraw.Draw(mask).rectangle((300, 420, 760, 900), fill=255) # white: redraw
buf = io.BytesIO()
mask.save(buf, format="PNG")
mask_b64 = base64.b64encode(buf.getvalue()).decode()

Other methods

MethodEndpoint (POST /midjourney/v2…)Input
Imagine/imagineprompt
Upscale/upscalehash of imagine / blend / describe / animate + choice 1-4
Upsample (Subtle, Creative, 2x, 4x)/upsamplehash of upscale
Variation/variationhash of imagine / describe / blend + choice 1-4
Vary (Subtle) / Vary (Strong)/vary-subtle, /vary-stronghash of upscale
Reroll/rerollhash of a task
Animate (video)/animatehash of upscale + high / low
Inpaint (Vary Region)/inpainthash of upscale + mask + prompt
Zoom Out/zoomhash of upscale + 50 / 75 or prompt
Pan/panhash of upscale + direction
Blend/blend2-5 image URLs
Describe/describeimage URL
Seed/seedhash of a task
Upload/uploadPNG / JPG file or URL
Info, Speed, Prefer (Remix)/info, /speed, /preferaccount_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.

See also

Use image generation Magic with UserAPI