Automating Asset Generation with the Midjourney API • Part I
Tags: midjourney, ai, api, tutorial
Published: 2023-11-12 05:12:39

### Introduction

If you are a designer, a game developer, or simply experimenting with [Midjourney](https://midjourney.com) and want to create a large number of [/imagine](https://docs.midjourney.com/docs/quick-start#5-use-the-imagine-command) generations based on your ideas, it may be very tedious and time-consuming to do so manually.

In this article, we will explore methods to efficiently execute such tasks with minimal effort.   

We will also attempt to measure the time it takes to generate 50 Midjourney /imagine generations and compare it with the time reported by Midjourney for their Fast GPU.

Please check a [YouTube video](https://youtu.be/SIiPnTJ9SHU) covering this article.

### Setup 

We will use the Midjourney API provided by [useapi.net](https://useapi.net) to interact with Midjourney.

To get started, you'll need a Discord account with an active [Midjourney subscription](https://docs.midjourney.com/docs/plans#plan-comparison). The $10 Basic Plan will suffice.

Follow these [simple steps](https://useapi.net/docs/start-here) to obtain the following:

- Discord server id number, referred to in this article as `server`.
- Discord channel id number, referred to in this article as `channel`.
- Discord token, referred to in this article as `discord`. [Verify Discord access](https://useapi.net/docs/start-here/setup-midjourney#verify-discord-access).
- [useapi.net](https://useapi.net) API token, referred to in this article as `token`. [Verify useapi.net API access](https://useapi.net/docs/start-here/setup-useapi#verify-useapinet-api-access).

Useapi.net provides an easy way to experiment with all API endpoints without writing any code. Check the `Try It` section at the end of each document page, such as [jobs/imagine](https://useapi.net/docs/api-v1/jobs-imagine#try-it).

For your convenience, we have published all the [code](https://github.com/useapi/examples/tree/main/generate-assets) used in this article. You can choose between JavaScript and Python examples. Clone this repository locally and use it as a starting point for your experiments.
  
### Preparing Midjourney Prompts

Let's use [ChatGPT](https://chat.openai.com) to create a list of prompts. For this article, we will be generating fairy tale characters. Here's our ChatGPT prompt:

> Create a JSON array of strings with 50 Midjourney prompts. Each prompt should describe a popular fairy tale protagonist or antagonist placed in the modern world, solving contemporary problems using fairy tale methods. Be creative!

The output will look something like this:

```json
[
    "Cinderella, now a well-known fashion designer, uses her knack for transforming rags into fabulous outfits to help those in poverty dress for success.",
    "Jack, of Jack and the Beanstalk fame, is a successful botanist who tackles the issue of world hunger by growing gigantic, genetically modified crops.",
    "Rapunzel, a professional rock climber, uses her long-braided hair for safety ropes during rescue missions in difficult terrains.",
    ...
    "The Big Bad Wolf, a top-notch security consultant, fortifies homes against break-ins, teaching the importance of strong construction materials."
]
```

An array of prompts generated by ChatGPT needs to be saved to a locally cloned [prompts.json](https://github.com/useapi/examples/blob/main/generate-assets/prompts.json) file.

### Executing Prompts Using the Midjourney API

Create a file locally in the same folder named `example.sh` with the following content:

#### [JavaScript](https://github.com/useapi/examples/blob/main/generate-assets/example.js)
```bash
USEAPI_TOKEN="useapi API token" USEAPI_SERVER="Discord server" USEAPI_CHANNEL="Discord channel" USEAPI_DISCORD="Discord token" node ./example.js
```

#### [Python](https://github.com/useapi/examples/blob/main/generate-assets/example.py)
```bash
USEAPI_TOKEN="useapi API token" USEAPI_SERVER="Discord server" USEAPI_CHANNEL="Discord channel" USEAPI_DISCORD="Discord token" python3 ./example.py
```

Execute it from the command line like this: `./example.sh` and observe the magic of the API. We have created a [YouTube video](https://youtu.be/SIiPnTJ9SHU) covering this entire process.

The generated images will be saved locally. You can continue the generation process in a Discord channel to refine your desired creations, or you can use the [jobs/button](https://useapi.net/docs/api-v1/jobs-button) to automate it, following the same process as demonstrated above.

### Conclusion

As shown in the [YouTube video](https://youtu.be/SIiPnTJ9SHU), you can observe the following:
- Time to generate 50 Midjourney /imagine creations using the Midjourney API: ~**19** minutes
- Utilization of the Midjourney Fast GPU: ~**26** minutes

Visit our [Discord Server](https://discord.gg/w28uK3cnmF) for any support questions and concerns. 

We regularly post guides and tutorials on the [YouTube Channel](https://www.youtube.com/@midjourneyapi).