Python – How to use the Midjourney API in 4 easy steps
Step 1 – Generate a bearer token
Login and then create an application from the My account menu.
Generate a bearer token from your new application.
Step 2 – Start a new thread with Midjourney
You may select an ai_version_code. For example : midjourney.
Send an http POST request to create a new thread.
import requests bearer = '<<your_token_here>>' # CREATE THREAD # ------------- url = "https://app.ai-client.com/api/v1/threads" payload={'ai_version_code': 'midjourney'} headers = { 'Accept': 'application/json', 'Authorization': 'Bearer ' + bearer } response = requests.request("POST", url, headers=headers, data=payload) thread = response.json()['data'] print(thread)
This will return :
{ "guid":"fd67b44e-efdd-47f2-9291-646902add92b", "ai_code":"midjourney", "ai_version_code":"midjourney", "title":"New thread with Midjourney", "created_at":"2023-06-11T20:09:07.000000Z", "updated_at":"2023-06-11T20:09:07.000000Z" }
Step 3 – Create a thread entry to send a message
You may use the thread object to create a new entry with a POST request.
url = "https://app.ai-client.com/api/v1/threads/" + thread['guid'] + "/entry" payload={'type': 'request', 'command' : 'imagine', 'prompt' : "Cats and dogs with lightsabers --ar 16:9 --v 5"} headers = { 'Accept': 'application/json', 'Authorization': 'Bearer ' + bearer } response = requests.request("POST", url, headers=headers, data=payload) thread_entry = response.json()['data'] print(thread_entry)
This will return :
{ "guid":"9ffb2e4a-33af-4d54-bcc9-1086f12558cc", "thread_guid":"fd67b44e-efdd-47f2-9291-646902add92b", "type":"request", "error":"None", "content":{ "command":"imagine", "prompt":"Cats and dogs with lightsabers --ar 16:9 --v 5" }, "waiting_response":true, "credits":0, "created_at":"2023-06-11T20:09:07.000000Z" }
Step 4 – Wait for a response from Midjourney
You must now wait for the AI’s response. The “waiting_response” property of your thread entry informs you of Midjourney’s response.
You may use the thread object to get all the thread entries. Send a GET request. You will see your thread entry of type “request” and the Midjourney thread entry of type “response“.
url = "https://app.ai-client.com/api/v1/threads/" + thread['guid'] + "/entries" headers = { 'Accept': 'application/json', 'Authorization': 'Bearer ' + bearer } response = requests.request("GET", url, headers=headers) thread_entries = response.json()['data'] print(thread_entries)
This will return :
[ { "guid":"9ffb2e4a-33af-4d54-bcc9-1086f12558cc", "thread_guid":"fd67b44e-efdd-47f2-9291-646902add92b", "type":"request", "error":"None", "content":{ "command":"imagine", "prompt":"Cats and dogs with lightsabers --ar 16:9 --v 5" }, "waiting_response":false, "credits":0, "created_at":"2023-06-11T20:09:07.000000Z" }, { "guid":"8474a886-1438-458a-bfcc-50579212ab2c", "thread_guid":"fd67b44e-efdd-47f2-9291-646902add92b", "type":"response", "error":"None", "content":{ "state":"completed", "percent":100, "images":[ { "url":"https://app.ai-client.com/storage/midjourney/8a0ec264-98b5-45e4-99df-5fdfee37a175.png", "thumbnail_url":"https://app.ai-client.com/storage/midjourney/8a0ec264-98b5-45e4-99df-5fdfee37a175.png.thumbnail.jpg", "dimensions":"1456x816", "filesize":1642438 }, { "url":"https://app.ai-client.com/storage/midjourney/753e4eb7-2a32-4923-ac20-c193bbbd4f5f.png", "thumbnail_url":"https://app.ai-client.com/storage/midjourney/753e4eb7-2a32-4923-ac20-c193bbbd4f5f.png.thumbnail.jpg", "dimensions":"1456x816", "filesize":1682993 }, { "url":"https://app.ai-client.com/storage/midjourney/e3f37f34-419b-45da-a027-1b09f3e934cb.png", "thumbnail_url":"https://app.ai-client.com/storage/midjourney/e3f37f34-419b-45da-a027-1b09f3e934cb.png.thumbnail.jpg", "dimensions":"1456x816", "filesize":1420694 }, { "url":"https://app.ai-client.com/storage/midjourney/953d8005-a295-49d6-bdb6-7ef7f6bf3b1f.png", "thumbnail_url":"https://app.ai-client.com/storage/midjourney/953d8005-a295-49d6-bdb6-7ef7f6bf3b1f.png.thumbnail.jpg", "dimensions":"1456x816", "filesize":1264734 } ] }, "waiting_response":false, "credits":10, "created_at":"2023-06-11T20:09:10.000000Z" } ]
Full code example
import requests import time bearer = '<<your_token_here>>' # CREATE THREAD # ------------- url = "https://app.ai-client.com/api/v1/threads" payload={'ai_version_code': 'midjourney'} headers = { 'Accept': 'application/json', 'Authorization': 'Bearer ' + bearer } response = requests.request("POST", url, headers=headers, data=payload) thread = response.json()['data'] print(thread) # {'guid': 'fd67b44e-efdd-47f2-9291-646902add92b', 'ai_code': 'midjourney', 'ai_version_code': 'midjourney', 'title': 'New thread with Midjourney', 'created_at': '2023-06-11T20:09:07.000000Z', 'updated_at': '2023-06-11T20:09:07.000000Z'} # CREATE THREAD ENTRY OF TYPE request # ----------------------------------- url = "https://app.ai-client.com/api/v1/threads/" + thread['guid'] + "/entry" payload={'type': 'request', 'command' : 'imagine', 'prompt' : "Cats and dogs with lightsabers --ar 16:9 --v 5"} headers = { 'Accept': 'application/json', 'Authorization': 'Bearer ' + bearer } response = requests.request("POST", url, headers=headers, data=payload) thread_entry = response.json()['data'] print(thread_entry) # {'guid': '9ffb2e4a-33af-4d54-bcc9-1086f12558cc', 'thread_guid': 'fd67b44e-efdd-47f2-9291-646902add92b', 'type': 'request', 'error': None, 'content': {'command': 'imagine', 'prompt': 'Cats and dogs with lightsabers --ar 16:9 --v 5'}, 'waiting_response': True, 'credits': 0, 'created_at': '2023-06-11T20:09:07.000000Z'} # WAIT FOR 2 MINUTES # ------------------ time.sleep(120) # GET THREAD ENTRIES # ------------------ url = "https://app.ai-client.com/api/v1/threads/" + thread['guid'] + "/entries" headers = { 'Accept': 'application/json', 'Authorization': 'Bearer ' + bearer } response = requests.request("GET", url, headers=headers) thread_entries = response.json()['data'] print(thread_entries) # [{'guid': '9ffb2e4a-33af-4d54-bcc9-1086f12558cc', 'thread_guid': 'fd67b44e-efdd-47f2-9291-646902add92b', 'type': 'request', 'error': None, 'content': {'command': 'imagine', 'prompt': 'Cats and dogs with lightsabers --ar 16:9 --v 5'}, 'waiting_response': False, 'credits': 0, 'created_at': '2023-06-11T20:09:07.000000Z'}, {'guid': '8474a886-1438-458a-bfcc-50579212ab2c', 'thread_guid': 'fd67b44e-efdd-47f2-9291-646902add92b', 'type': 'response', 'error': None, 'content': {'state': 'completed', 'percent': 100, 'images': [{'url': 'https://app.ai-client.com/storage/midjourney/8a0ec264-98b5-45e4-99df-5fdfee37a175.png', 'thumbnail_url': 'https://app.ai-client.com/storage/midjourney/8a0ec264-98b5-45e4-99df-5fdfee37a175.png.thumbnail.jpg', 'dimensions': '1456x816', 'filesize': 1642438}, {'url': 'https://app.ai-client.com/storage/midjourney/753e4eb7-2a32-4923-ac20-c193bbbd4f5f.png', 'thumbnail_url': 'https://app.ai-client.com/storage/midjourney/753e4eb7-2a32-4923-ac20-c193bbbd4f5f.png.thumbnail.jpg', 'dimensions': '1456x816', 'filesize': 1682993}, {'url': 'https://app.ai-client.com/storage/midjourney/e3f37f34-419b-45da-a027-1b09f3e934cb.png', 'thumbnail_url': 'https://app.ai-client.com/storage/midjourney/e3f37f34-419b-45da-a027-1b09f3e934cb.png.thumbnail.jpg', 'dimensions': '1456x816', 'filesize': 1420694}, {'url': 'https://app.ai-client.com/storage/midjourney/953d8005-a295-49d6-bdb6-7ef7f6bf3b1f.png', 'thumbnail_url': 'https://app.ai-client.com/storage/midjourney/953d8005-a295-49d6-bdb6-7ef7f6bf3b1f.png.thumbnail.jpg', 'dimensions': '1456x816', 'filesize': 1264734}]}, 'waiting_response': False, 'credits': 10, 'created_at': '2023-06-11T20:09:10.000000Z'}]
See our Swagger API documentation for more information.