Python – How to use the DALL·E 2 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 DALL·E 2

You may select an ai_version_code. For example : dalle2_text2img.

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': 'dalle2_text2img'}

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":"cde75d8e-4aee-496e-a1c5-8cf823b43f1c",
   "ai_code":"dalle2",
   "ai_version_code":"dalle2_text2img",
   "title":"New thread with DALL·E 2 Image Generation",
   "created_at":"2023-06-11T15:52:52.000000Z",
   "updated_at":"2023-06-11T15:52:52.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', 'n' : 2, 'size': '512x512', 'prompt' : "Cats and dogs with lightsabers"}

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":"c2bc2c7f-095a-4998-8833-85c2e2cb077c",
   "thread_guid":"cde75d8e-4aee-496e-a1c5-8cf823b43f1c",
   "type":"request",
   "error":"None",
   "content":{
      "n":2,
      "size":"512x512",
      "prompt":"Cats and dogs with lightsabers"
   },
   "waiting_response":true,
   "credits":0,
   "created_at":"2023-06-11T15:52:52.000000Z"
}

Step 4 – Wait for a response from DALL·E 2

You must now wait for the AI’s response. The “waiting_response” property of your thread entry informs you of DALL·E 2-‘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 DALL·E 2 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":"c2bc2c7f-095a-4998-8833-85c2e2cb077c",
      "thread_guid":"cde75d8e-4aee-496e-a1c5-8cf823b43f1c",
      "type":"request",
      "error":"None",
      "content":{
         "n":2,
         "size":"512x512",
         "prompt":"Cats and dogs with lightsabers"
      },
      "waiting_response":false,
      "credits":0,
      "created_at":"2023-06-11T15:52:52.000000Z"
   },
   {
      "guid":"5c540108-21bd-45af-b4fb-e3d7ca10c8d8",
      "thread_guid":"cde75d8e-4aee-496e-a1c5-8cf823b43f1c",
      "type":"response",
      "error":"None",
      "content":[
         {
            "url":"https://app.ai-client.com/storage/dalle2_text2img/23e205f2-ed89-4c9f-b5f7-6fa6c69eb3c5.png",
            "thumbnail_url":"https://app.ai-client.com/storage/dalle2_text2img/23e205f2-ed89-4c9f-b5f7-6fa6c69eb3c5.png.thumbnail.jpg",
            "dimensions":"512x512",
            "filesize":787387
         },
         {
            "url":"https://app.ai-client.com/storage/dalle2_text2img/8ccc9361-a83a-426b-a65d-8f817d9ae937.png",
            "thumbnail_url":"https://app.ai-client.com/storage/dalle2_text2img/8ccc9361-a83a-426b-a65d-8f817d9ae937.png.thumbnail.jpg",
            "dimensions":"512x512",
            "filesize":787387
         }
      ],
      "waiting_response":false,
      "credits":14,
      "created_at":"2023-06-11T15:52:59.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': 'dalle2_text2img'}

headers = {
  'Accept': 'application/json',
  'Authorization': 'Bearer ' + bearer
}

response = requests.request("POST", url, headers=headers, data=payload)

thread = response.json()['data']

print(thread)
# {'guid': 'cde75d8e-4aee-496e-a1c5-8cf823b43f1c', 'ai_code': 'dalle2', 'ai_version_code': 'dalle2_text2img', 'title': 'New thread with DALL·E 2 Image Generation', 'created_at': '2023-06-11T15:52:52.000000Z', 'updated_at': '2023-06-11T15:52:52.000000Z'}


# CREATE THREAD ENTRY OF TYPE request
# -----------------------------------
url = "https://app.ai-client.com/api/v1/threads/" + thread['guid'] + "/entry"

payload={'type': 'request', 'n' : 2, 'size': '512x512', 'prompt' : "Cats and dogs with lightsabers"}

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': 'c2bc2c7f-095a-4998-8833-85c2e2cb077c', 'thread_guid': 'cde75d8e-4aee-496e-a1c5-8cf823b43f1c', 'type': 'request', 'error': None, 'content': {'n': 2, 'size': '512x512', 'prompt': 'Cats and dogs with lightsabers'}, 'waiting_response': True, 'credits': 0, 'created_at': '2023-06-11T15:52:52.000000Z'}


# WAIT FOR 60 SECONDS
# -------------------
time.sleep(60)

# 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': 'c2bc2c7f-095a-4998-8833-85c2e2cb077c', 'thread_guid': 'cde75d8e-4aee-496e-a1c5-8cf823b43f1c', 'type': 'request', 'error': None, 'content': {'n': 2, 'size': '512x512', 'prompt': 'Cats and dogs with lightsabers'}, 'waiting_response': False, 'credits': 0, 'created_at': '2023-06-11T15:52:52.000000Z'}, {'guid': '5c540108-21bd-45af-b4fb-e3d7ca10c8d8', 'thread_guid': 'cde75d8e-4aee-496e-a1c5-8cf823b43f1c', 'type': 'response', 'error': None, 'content': [{'url': 'https://app.ai-client.com/storage/dalle2_text2img/23e205f2-ed89-4c9f-b5f7-6fa6c69eb3c5.png', 'thumbnail_url': 'https://app.ai-client.com/storage/dalle2_text2img/23e205f2-ed89-4c9f-b5f7-6fa6c69eb3c5.png.thumbnail.jpg', 'dimensions': '512x512', 'filesize': 787387}, {'url': 'https://app.ai-client.com/storage/dalle2_text2img/8ccc9361-a83a-426b-a65d-8f817d9ae937.png', 'thumbnail_url': 'https://app.ai-client.com/storage/dalle2_text2img/8ccc9361-a83a-426b-a65d-8f817d9ae937.png.thumbnail.jpg', 'dimensions': '512x512', 'filesize': 787387}], 'waiting_response': False, 'credits': 14, 'created_at': '2023-06-11T15:52:59.000000Z'}]

See our Swagger API documentation for more information.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *