Use Midjourney via userapi.ai API layer

Use Midjourney via userapi.ai API layer

·

5 min read

Midjourney provides the most powerful image generation capabilities (and not only generation). Marketers, designers, web developers, content makers, arbitrageurs - in my opinion, over the past year, have fundamentally changed their approach to work, having the opportunity to generate images here and now, on the fly, without hiring and waiting for a person to prepare them an image.

In this article, I'm going to tell you:

Now you can generate an image from your text description in 20 seconds. You need a discord.com account to do this. By authorising on midjourney.com through your discord account and buying a subscription there, you get the opportunity to communicate with Midjourney Bot (just find him in discord and write to him in private messages).

Communication with Midjourney Bot is done in private chat via commands. The two main commands are:

  • /imagine prompt

  • /describe [image]

If you send /imagine command to the bot, it will immediately generate an image according to the specified prompt, and after about 20 seconds you will get an image corresponding to your request

And in the opposite direction: by sending /describe and adding an image you will get a text description of the image. And if you now send it again to /imagine - you will get an image similar to the original one.

Description of existing methods of working with images in Midjourney

The two methods described above are often used in their projects outside of Discord. What if you need to automate your work with Midjourney? There are API layers for Midjourney. UserAPI.ai is one such service that provides an extensive list of methods such as:

Imagine - the method I described above, it will create you an image consisting of 4 parts, this will be 4 variations of the illustrations for your query

Describe - will return you 4 descriptions matching your image

Upscale - the method will cut out one of the 4 images produced by the Imagine method. And will return a link to that single image in better quality

Upsample - allows you to significantly improve the quality of the image (Subtle) or improve the quality of the image by adding more details (Creative).

Variation - allows you to create 4 new images, taking as an example one of the images obtained through the /imagine method.

Inpaint - this method is very rare and allows you to change the selected area of the image.

Blend - takes up to 5 images, as a result it returns you 4 images that are a blend of the original images.

Reroll - This method will redraw the 4 images from /imagine again.

Zoom - Simply zooms out the camera

Pan - will expand the image on the specified side (up, down, top, bottom)

Info - This is a helper method for automation that allows you to get subscription information

Prefer (toggle to remix mode) - switches your account mode to remix mode or vice versa. And remix mode itself allows you to make adjustments to the request when using the Variation method

There are also two assistant methods:

  • Status - hash method returns the result of image generation (below will show how to apply it)

  • Discord account info - method is indispensable when integrating api on your site - to control the status of the account

Using api layer to Midjourney

Now I would like to give you a simple example of how to get Midjourney to work through an api layer. The implication is that you can make requests from the console using the curl utility.

The first thing you need to do is to add your Discord account to the userapi.ai panel. The first and foremost thing you need to do is to find the token of your Discord account. Go to discord in your web browser and find the token as shown in the picture:

Channel ID

And then use this token (it is marked with number 2) when adding. The number 1 is Channel ID. You can verify it with the one automatically inserted into the form.

When adding an account, simply copy the Api Key. You will see it both when you add it and afterwards next to the newly added account:

and use it in the curl command:

curl -X POST https://api.userapi.ai/midjourney/v2/imagine \
-H "Content-Type: application/json" \
-H "api-key: 3fb0b6d9-6e1b-4bb7-993f-f79e9f1717e8" \
-d '{ "prompt": "nice 52 old woman in the pink hat" }'

Copy the command, replace my Api Key with yours and execute the request via terminal!

In response you will get:

{
  "hash":"cecb48b8-7edd-4c42-b63a-fc6afd755979"
}

Use this hash to get the progress and result of the image generation. This is what the curl command will look like:

curl -X GET \
  -H "Content-Type: application/json" \
  -H "api-key: 3fb0b6d9-6e1b-4bd7-993f-f79e9f1717e8" \
  "https://api.userapi.ai/midjourney/v2/status?hash=cecb48b8-7edd-4c42-b63a-fc6afd755979"

And as a result of executing this curl command, you will get the result of generation (or progress, if you manage to make a request while the image is still being generated):

{
  "account_hash":"d2a06436-1976-433d-b24b-409103061630",
  "hash":"cecb48b8-7edd-4c42-b63a-fc6afd755979",
  "webhook_url":null,
  "webhook_type":null,
  "prompt":"nice 52 old woman in the pink hat",
  "type":"imagine",
  "progress":100,
  "status":"done",
  "result":{
    "url":"https://cdn.discordapp.com/attachments/example.png",
    "proxy_url":"https://cdn.discordapp.com/attachments/example.png",
    "filename":"example.png",
    "content_type":"image/png",
    "width":2048,"height":2048,
    "size":7277491},
    "next_actions":[
        {"type":"upscale","choices":[1,2,3,4]},{"type":"reroll"},{"type":"variation","choices":[1,2,3,4]}],
    "status_reason":null,
    "prefilter_result":[],
    "created_at":"2024-03-25T10:18:55Z"
}

result.url (and result.proxy_url) - the image that was generated! Nice!

So, we've figured out that we need three basic things:

  1. An account on discord.com

  2. Subscription to midjourney.com (you need to log in through your discord account).

  3. Sign up for UserAPI.ai <- here you will need to add your discord account and get the Api Key

I hope my article was helpful and you managed to figure out how to work with Midjourney via API! Good luck!