Examples for calling the Midjourney API (text-to-image, follow-up actions, and image-to-video) through Apiframe.
Model page: Midjourney API
This repo does not host model weights. Midjourney is a closed model. Inference runs on POST /v2/images/generate with model: "midjourney". Midjourney has no official public API.
Related model id: midjourney-video on POST /v2/videos/generate.
Midjourney is a closed text-to-image model. The current default on Apiframe is V8.2. Each job returns a 4-image grid plus gridUrl. Follow-up actions (upsample, variation, inpaint, outpaint, pan) run on POST /v2/images/midjourney/action.
export APIFRAME_API_KEY=afk_your_api_key_herepip install requestsimport os
import time
import requests
API = "https://api.apiframe.ai/v2"
headers = {
"X-API-Key": os.environ["APIFRAME_API_KEY"],
"Content-Type": "application/json",
}
job = requests.post(
f"{API}/images/generate",
headers=headers,
json={
"model": "midjourney",
"prompt": "editorial still life of a red heirloom tomato and mimosa, cobalt background",
"midjourneyParams": {"aspect_ratio": "16:9"},
},
).json()
while True:
result = requests.get(f"{API}/jobs/{job['jobId']}", headers=headers).json()
if result["status"] in ("COMPLETED", "FAILED"):
break
time.sleep(2)
print(result.get("result"))npm i @apiframe-ai/sdk@nextimport { Apiframe } from "@apiframe-ai/sdk";
const client = new Apiframe({ apiKey: process.env.APIFRAME_API_KEY });
const { jobId } = await client.images.generate({
model: "midjourney",
prompt: "editorial still life of a red heirloom tomato and mimosa, cobalt background",
midjourneyParams: { aspect_ratio: "16:9" },
});
const job = await client.jobs.waitFor(jobId);
console.log(job.result);Pass webhookUrl on the generate call if you do not want to poll.
| Parameter | Type | Default | Description |
|---|---|---|---|
prompt |
string | required | Text description of the image |
midjourneyParams.aspect_ratio |
string | "1:1" |
1:1, 3:4, 4:3, 9:16, 16:9, 21:9 |
Typical latency is about 50 seconds. Output is 4 images plus gridUrl.
requests.post(
"https://api.apiframe.ai/v2/images/midjourney/action",
headers=headers,
json={
"parentJobId": "JOB_ID",
"action": "upsample", # variation | inpaint | outpaint | pan
"index": 1, # 1-4
},
)Uses the sibling model midjourney-video.
requests.post(
"https://api.apiframe.ai/v2/videos/generate",
headers=headers,
json={
"model": "midjourney-video",
"prompt": "slow push in, soft daylight",
"midjourneyVideoParams": {
"start_image": "https://example.com/still.jpg",
"motion": "low",
"resolution": "hd",
},
},
){
"images": ["https://cdn2.apiframe.ai/images/….png"],
"gridUrl": "https://cdn2.apiframe.ai/images/….png"
}| Example | Python | JavaScript |
|---|---|---|
| Text to image | text_to_image.py | text_to_image.js |
| Upsample | upsample.py | upsample.js |
| Image to video | image_to_video.py | image_to_video.js |
cp .env.example .env
# set APIFRAME_API_KEY
python examples/text_to_image.pyExample code in this repository is provided as-is. Midjourney outputs are subject to Midjourney's terms. Midjourney is a trademark of Midjourney, Inc. Apiframe is not affiliated with, endorsed by, or sponsored by Midjourney, Inc.