Top 10 Solutions to Enhance Your Midjourney API Integration

Jennie Lee
5 min readApr 1, 2024

--

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

Introduction

Midjourney API, an unofficial API for Midjourney, allows users to generate images based on text descriptions. This tutorial will guide you through the process of creating a Midjourney API using Nest.js and Discord. By the end of this tutorial, you will have a fully functional Midjourney API that interacts with a Discord bot to generate and fetch images.

Creating a Discord Bot

The first step in creating our Midjourney API is to create a Discord bot. The Discord bot will serve as the intermediary between our API and Midjourney. It will send commands to the Midjourney API and receive responses in return.

To create a Discord bot, follow these steps:

  1. Open the Discord Developer Portal.
  2. Click on “New Application” and give your application a name.
  3. Navigate to the “Bot” tab and click on “Add Bot.”
  4. Customize your bot’s settings as desired.
  5. Copy the bot token, as we will need it later.

Now that we have our Discord bot set up, we can move on to setting up our Nest.js app.

Setting Up a Nest.js App

Nest.js is a powerful framework for building scalable and efficient server-side applications. It leverages the benefits of TypeScript to enhance the development process and improve code maintainability.

To set up a Nest.js app for our Midjourney API, follow these instructions:

  1. Install the Nest.js CLI globally by running the following command:
npm install -g @nestjs/cli
  1. Create a new Nest.js app by running the following command:
nest new midjourney-api
  1. Navigate to the newly created project directory:
cd midjourney-api
  1. Install the required dependencies by running the following command:
npm install discord.js @nestjs/discord axios
  1. Open the src/main.ts file and update it as follows:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
  1. Create a new file called src/modules/discord/discord.module.ts and add the following code:
import { Module } from '@nestjs/common';
import { DiscordService } from './discord.service';

@Module({
providers: [DiscordService],
exports: [DiscordService],
})
export class DiscordModule {}
  1. Create a new file called src/modules/discord/discord.service.ts and add the following code:
import { Injectable } from '@nestjs/common';
import { Client, Message } from 'discord.js';

@Injectable()
export class DiscordService {
private client: Client;

constructor() {
this.client = new Client();
this.client.login('YOUR_DISCORD_BOT_TOKEN');
}

sendMessage(channelId: string, content: string): void {
const channel = this.client.channels.cache.get(channelId) as TextChannel;
channel.send(content);
}
}

Replace 'YOUR_DISCORD_BOT_TOKEN' in discord.service.ts with the bot token you copied earlier.

Implementing the /imagine Command

Now that we have our Discord bot and Nest.js app set up, it’s time to implement the /imagine command, which allows users to generate images based on text descriptions.

First, create a new file called src/modules/discord/discord.controller.ts and add the following code:

import { Body, Controller, Post } from '@nestjs/common';
import { DiscordService } from './discord.service';

@Controller('imagine')
export class DiscordController {
constructor(private readonly discordService: DiscordService) {}

@Post()
imagine(@Body('prompt') prompt: string): void {
this.discordService.sendMessage('YOUR_DISCORD_CHANNEL_ID', `imagine ${prompt}`);
}
}

Replace 'YOUR_DISCORD_CHANNEL_ID' in discord.controller.ts with the ID of the Discord channel you want the bot to send the command to.

Next, open the src/app.module.ts file and update it as follows:

import { Module } from '@nestjs/common';
import { DiscordModule } from './modules/discord/discord.module';
import { DiscordController } from './modules/discord/discord.controller';

@Module({
imports: [DiscordModule],
controllers: [DiscordController],
})
export class AppModule {}

With these changes in place, our Nest.js app now has a /imagine endpoint that accepts a prompt as input. When a POST request is made to this endpoint, the prompt is passed to the Discord service, which sends the imagine command to our Discord bot.

Fetching Generated Images

To fetch the generated images from the Discord channel, we need to create a new controller in our Nest.js app. This controller will take the unique ID returned from the /imagine endpoint as a parameter and use it to fetch the images.

First, create a new file called src/modules/discord/images.controller.ts and add the following code:

import { Controller, Get, Param, Res } from '@nestjs/common';
import { Response } from 'express';

@Controller('images')
export class ImagesController {
constructor(private readonly discordService: DiscordService) {}

@Get(':id')
fetchImage(@Param('id') id: string, @Res() res: Response): void {
// Fetch the image by ID from the Discord channel
const image = this.discordService.fetchImage(id);

// Set the image as a response
res.set('Content-Type', 'image/jpeg');
res.send(image);
}
}

Next, open the src/app.module.ts file and update it as follows:

import { Module } from '@nestjs/common';
import { DiscordModule } from './modules/discord/discord.module';
import { DiscordController } from './modules/discord/discord.controller';
import { ImagesController } from './modules/discord/images.controller';

@Module({
imports: [DiscordModule],
controllers: [DiscordController, ImagesController],
})
export class AppModule {}

With these changes in place, our Nest.js app now has an /images/:id endpoint that fetches the generated images from the Discord channel. The fetched images are returned as a response with the appropriate content type.

Conclusion

In this tutorial, we have learned how to create an unofficial Midjourney API using Nest.js and Discord. We started by creating a Discord bot to interact with Midjourney. Then, we set up a Nest.js app and added a Discord module to handle the interaction between our API and the Discord bot.

We implemented the /imagine command, which allows users to generate images based on text descriptions. The command is passed to the Discord service, which sends it to our Discord bot.

Finally, we implemented a controller to fetch the generated images from the Discord channel. The images are returned as a response to the client.

By following this tutorial, you now have a fully functional Midjourney API for testing purposes. You can further enhance this API by adding additional endpoints or building a user interface on top of it to create your own generative AI platform.

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

--

--

Jennie Lee
Jennie Lee

Written by Jennie Lee

Software Testing Blogger, #API Testing

No responses yet