Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Midjourney API

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.

What is Midjourney?

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.

Quick start

export APIFRAME_API_KEY=afk_your_api_key_here

Python

pip install requests
import 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"))

JavaScript

npm i @apiframe-ai/sdk@next
import { 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.

Parameters

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.

Follow-up actions

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
    },
)

Image to video

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",
        },
    },
)

Output

{
  "images": ["https://cdn2.apiframe.ai/images/….png"],
  "gridUrl": "https://cdn2.apiframe.ai/images/….png"
}

Examples

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.py

License

Example 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.

About

Midjourney API — text-to-image examples

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors