DEV Community

Cover image for How I built a Midjourney API

Posted on • Updated on

How I built a Midjourney API

Want to see it in action? Follow me on LinkedIn and text me

Building a Midjourney API: Breaking Down the Process


If you're in the world of AI, you've probably heard of Midjourney. It's a powerful tool that transforms text into high-resolution, high-quality images, all within Discord. With an easy-to-use platform and a dynamic user community, it's become a standout tool in the digital landscape.

If you have your SaaS product, an idea that you need midjourney API for, this can be done. Talk to me

Want to see it in action? Follow me on LinkedIn and text me

However, while Midjourney doesn't offer an official API, we can still build one to better leverage its capabilities. Here's a breakdown of the process:


Understanding Midjourney:

First things first, we need to grasp how Mindjourney operates on Discord. Users simply interact with an AI bot using commands, and the bot processes these commands to generate images based on the descriptions provided.

Connecting to Midjourney:

Without an official API, establishing a connection to Mindjourney involves a Discord bot. To achieve this, we take the following steps:

Identify and connect a Mindjourney token of your account. This acts as the unique identifier allowing your bot to interact with the Mindjourney platform.

Clarification: Keep in mind - this is not an API for production, it just a technical article explaining the architecture of a midjourney request, it is not a suggestion to use it in production, or at all. Other than playing around, it is not a business application.

Create a Discord bot that can listen to our channel. This bot serves as our link between our own systems and Midjourney's image generation capabilities.

Adding a Midjourney bot to your server

Integrating the API:

Upon establishing the connection, we develop a mechanism to interact with our channel and the Midjourney bot. Our API integration requires:

Posting commands to our channel: The commands instruct the bot to generate specific images.

Fetching results: After posting commands, our bot listens for and fetches the image generation results.

Here is an example of how I fetch results using FastAPI and my Discord bot:

def get_result_from_midjourney():
        Get request for getting the result of post request
    get_url = "<your api url>"
    get_payload = {}
    get_headers = {
        'Authorization': some key,
        'Content-Type': 'application/json',
        'Cookie': <my cookie here>
    get_response = requests.request(
        "GET", get_url, headers=get_headers, data=get_payload
    return get_response
Enter fullscreen mode Exit fullscreen mode

Uploading images to cloud hosting: We use a cloud hosting service (like AWS S3) to store the generated images. This step ensures the scalability and accessibility of our image storage.

Cropping: The initial response from Midjourney usually delivers images structured as a grid. We need to intelligently crop these images for use in different contexts.

Returning final images: Once images are processed and stored, we return them through our API, ready for use wherever they are needed.

Creating images with FastAPI

Facing the Challenges:

Naturally, building this interface comes with its fair share of challenges:

  1. How do we determine when images are ready?
  2. How do we handle the cropping of images given the grid structure of the initial response?
  3. How do we ensure that a returned image corresponds to the specific command we issued?
  4. How to scale, so I can server multiple concurrent calls?

Despite these challenges, creating a Midjourney API has the potential to revolutionize how we generate and use digital images. I invite you to join me on this exciting journey as we navigate through these challenges and uncover new possibilities.

I create AI that works

You can check out the final solution, just follow me on linkedin and DM me

I have also built a great website that converts any JS code to Typescript - check this out!!

Top comments (9)

siera profile image
Mykolas Klusaitis

maybe you have any thoughts to run a product with this api?

suede profile image

That was a good idea back then . The risk was that MJ may publish their own API one day, and it's done ..

cubist77 profile image

Have you considered releasing your code?

theweirs profile image

Did you complete the API?

jack206418070 profile image

If you want to develop this API yourself, here are three points you can consider:

  1. Use the web version of Discord to fetch the API interactions between Discord and the Midjourney bot when executing commands.

  2. Create a Discord bot that listens to all messages from the Midjourney bot, including the messages it sends when generating images. Use callbacks to transmit these messages to your desired endpoints.

  3. Depending on the application type, implement the logic to receive callback messages and achieve the desired outcome.

Going further:

  1. Consider multiple users: How can you differentiate and deliver the results of different user requests to the respective users?

  2. For specific complex commands, explore how to combine Discord's own API with your API to achieve functionalities like "/blend."

suede profile image

of course. it is working flawlessly

hopefloats101 profile image
Hope Floats • Edited

Do you have a link to a demo perhaps? This is awesome work.

luna_cuili profile image

How to build it?

therrou profile image
Tomas Herrou

Did you create a bot? If so, how did you by-pass the bot flag from Midjourney? Or you created a self-bot? If so, are you breaking the ToS from Discord?