How to use the Midjourney API in Python

The Midjourney API is a Python library that simplifies the process of working with Midjourney's AI-assisted image generation features. This Answer will walk you through the steps of environment setup, image creation, image description, and account management using the Midjourney API in Python.

Setting up the environment

To begin with, the Midjourney API must be installed on your machine. Python's package manager, pip, can accomplish this. Open your terminal and execute the following command:

pip install midjourney-api

This command will place the Midjourney API package on your computer.

Then, import the package into your environment and initiate it using your API key, which can be obtained from your Midjourney account.

from midjourney_api import TNL
TNL_API_KEY = 'your_api_key_here'
tnl = TNL(TNL_API_KEY)

Generating an image from a prompt

The Midjourney API facilitates the generation of images based on text prompts. Here is a demonstration of how it can be done:

prompt = 'a student on his desk'
response = tnl.imagine(prompt)
print(response)

In this code snippet, the imagine method takes a text prompt as input and returns a response containing the generated image.

Generating an image from a prompt and an image

The API also allows you to generate an image using a text prompt and an existing image. Here's how:

prompt = 'a student on his desk'
img_url = 'url_of_the_base_image'
response = tnl.img2img(prompt, img_url)
print(response)

In this snippet, the img2img method accepts a text prompt and an image URL and returns a response containing the newly generated image.

Describing an image

The Midjourney API includes a function to describe an image. Here's how to achieve this:

img_url = 'url_of_the_image_to_describe'
response = tnl.describe(img_url)
print(response)

In this code snippet, the describe method accepts an image URL and delivers a response with a description of the image.

Use Midjourney

Here is a comprehensive code sample that you can run to assess the Midjourney API. This code generates an image from a prompt and describes an image through a URL.

from midjourney_api import TNL
import os
TNL_API_KEY = os.environ["SECRET_KEY"]
tnl = TNL(TNL_API_KEY)
# Create an image from a prompt
prompt = 'Enter your prompt here'
response = tnl.imagine(prompt)
print(response)
# Describe an image
img_url = 'url_of_the_image_to_describe'
response = tnl.describe(img_url)
print(response)

Code explanation

  • Line 1–2: Execute necessary imports

  • Line 4: Retrieve the API key from environment variables.

  • Line 5: Initialize the TNL API client.

  • Line 8–10: Create an image from a prompt.

  • Line 13–15: Describe an image from its URL.

Retrieving and modifying account settings

The getSettings method can be used to access your account settings:

response = tnl.getSettings()
print(response)

You can alter your account settings using the setSettings method:

settings = 'settings_to_set'
response = tnl.setSettings(settings)
print(response)

Obtaining account information

The getInfo method provides information about your account, including "Fast Time Remaining", "Job Mode", and "Queued Jobs", among other details:

response = tnl.getInfo()
print(response)

Conclusion

The Midjourney API offers a straightforward and user-friendly method to tap into Midjourney's AI image generation functionalities. With a few Python commands, you can generate images from prompts, describe images, and adjust your account settings.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved