How I built a Midjourney API
Tags: chatgpt, nextjs, midjourne, python
Published: 2023-05-29 12:00:00

[Want to see it in action? Follow me on LinkedIn and text me](https://www.linkedin.com/in/yuval-suede)

**Building a Midjourney API:  Breaking Down the Process**

![MidjourneyAPI](https://media.giphy.com/media/UbGBm1sId2YAxlWZXO/giphy-downsized-large.gif)

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](https://www.linkedin.com/in/yuval-suede/)


[Want to see it in action? Follow me on LinkedIn and text me](https://www.linkedin.com/in/yuval-suede)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9haynet2pidwp3urdc4l.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16aitm5wv5bq1946q388.png)

**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
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5oskd02859jzxuzmwuqx.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oexr6whrvig8qx91n9o2.png)

You can check out the final solution, [just follow me on linkedin and DM me](https://www.linkedin.com/mynetwork/discovery-see-all/?usecase=PEOPLE_FOLLOWS&followMember=yuval-suede)


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

https://js2ts.com/