DEV Community

Cover image for How I built a Midjourney API
Yuval
Yuval

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

MidjourneyAPI

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:

MidjourneyAPI

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!!

https://js2ts.com/

Top comments (15)

Collapse
 
theweirs profile image
TheWeirs

Did you complete the API?

Collapse
 
jack206418070 profile image
jack206418070

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

Collapse
 
suede profile image
Yuval

of course. it is working flawlessly

Collapse
 
hopefloats101 profile image
Hope Floats • Edited

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

Thread Thread
 
xucian profile image
xucian

I can give you free access to mjapi.io, since it's in beta and I have to test it. hmu at the official email

Collapse
 
siera profile image
Mykolas Klusaitis

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

Collapse
 
suede profile image
Yuval

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

Collapse
 
cubist77 profile image
cubist77

Have you considered releasing your code?

Thread Thread
 
xucian profile image
xucian • Edited

sorry to plug this, but I can give you free access to mjapi.io, since it's in beta and I have to test it. hmu at the official email

Collapse
 
luna_cuili profile image
Luna_Cui

How to build it?

Collapse
 
useapi profile image
useapi.net

@suede
I am reaching out to inquire whether you might be interested in trying the Midjourney API. We have recently released this offering and are actively promoting it. For more details, please visit our documentation at useapi.net/docs/collaboration-offer.

Additionally, you can explore our YouTube channel for more insights: youtube.com/@midjourneyapi.

Regards,
-u

Collapse
 
nikoole profile image
nikoole

Just want to show you API layer to Midjourney: userapi.ai
Link to the documentation
There are all methods including Vary Region and Remix mode provided

Collapse
 
xucian profile image
xucian

yeah, I took the chance and built mjapi.io just for this, totalling about 10k lines of code.
if they release their own api, so be it. it was a fun experiment and I've learned about stripe, paypal, btc/lightning payments and discord stuff, really interesting

Collapse
 
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?

Collapse
 
dataheadless profile image
dataheadless

Very helpful, this one is good too Midjourney API