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.
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.
Facing the Challenges:
Naturally, building this interface comes with its fair share of challenges:
- How do we determine when images are ready?
- How do we handle the cropping of images given the grid structure of the initial response?
- How do we ensure that a returned image corresponds to the specific command we issued?
- 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.
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 (15)
Did you complete the API?
If you want to develop this API yourself, here are three points you can consider:
Use the web version of Discord to fetch the API interactions between Discord and the Midjourney bot when executing commands.
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.
Depending on the application type, implement the logic to receive callback messages and achieve the desired outcome.
Going further:
Consider multiple users: How can you differentiate and deliver the results of different user requests to the respective users?
For specific complex commands, explore how to combine Discord's own API with your API to achieve functionalities like "/blend."
of course. it is working flawlessly
Do you have a link to a demo perhaps? This is awesome work.
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
maybe you have any thoughts to run a product with this api?
That was a good idea back then . The risk was that MJ may publish their own API one day, and it's done ..
Have you considered releasing your code?
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
How to build it?
@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
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
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
Very helpful, this one is good too Midjourney API
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?