PHP – 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.

$bearer = '<<your_token_here>>';

// CREATE THREAD
// -------------
$url = "https://app.ai-client.com/api/v1/threads";

$headers = [
  'Accept: application/json',
  'Authorization: Bearer '.$bearer
];

$options = [
    'ai_version_code' => 'dalle2_text2img'
];

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => $options,
  CURLOPT_HTTPHEADER => $headers,
  CURLOPT_RETURNTRANSFER => true
));

$response = curl_exec($curl);
$thread = json_decode($response)->data;
curl_close($curl);

print_r($thread);

This will return :

stdClass Object
(
    [guid] => bffc10ad-6ebd-4c43-845e-8fa254bc1b51
    [ai_code] => dalle2
    [ai_version_code] => dalle2_text2img
    [title] => New thread with DALL·E 2 Image Generation
    [created_at] => 2023-06-16T22:10:09.000000Z
    [updated_at] => 2023-06-16T22:10:09.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";

$headers = [
  'Accept: application/json',
  'Authorization: Bearer '.$bearer
];

$options = ['type' => 'request', 'n' => '2', 'size' => '512x512', 'prompt' => "Cats and dogs with lightsabers"];

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => $options,
  CURLOPT_HTTPHEADER => $headers,
  CURLOPT_RETURNTRANSFER => true
));

$response = curl_exec($curl);
$threadEntry = json_decode($response)->data;
curl_close($curl);

print_r($threadEntry);

This will return :

stdClass Object
(
    [guid] => af92a337-4316-44d2-8a98-ff4d99d2b072
    [thread_guid] => bffc10ad-6ebd-4c43-845e-8fa254bc1b51
    [type] => request
    [error] =>
    [content] => stdClass Object
        (
            [n] => 2
            [size] => 512x512
            [prompt] => Cats and dogs with lightsabers
        )

    [waiting_response] => 1
    [credits] => 0
    [created_at] => 2023-06-16T22:10:09.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
];

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => $headers,
  CURLOPT_RETURNTRANSFER => true
));

$response = curl_exec($curl);
$threadEntries = json_decode($response)->data;
curl_close($curl);

print_r($threadEntries);

This will return :

Array
(
    [0] => stdClass Object
        (
            [guid] => af92a337-4316-44d2-8a98-ff4d99d2b072
            [thread_guid] => bffc10ad-6ebd-4c43-845e-8fa254bc1b51
            [type] => request
            [error] =>
            [content] => stdClass Object
                (
                    [n] => 2
                    [size] => 512x512
                    [prompt] => Cats and dogs with lightsabers
                )

            [waiting_response] =>
            [credits] => 0
            [created_at] => 2023-06-16T22:10:09.000000Z
        )

    [1] => stdClass Object
        (
            [guid] => a0283751-5ac0-4057-9dce-e3afaf87b4f0
            [thread_guid] => bffc10ad-6ebd-4c43-845e-8fa254bc1b51
            [type] => response
            [error] =>
            [content] => Array
                (
                    [0] => stdClass Object
                        (
                            [url] => https://app.ai-client.com/storage/dalle2_text2img/c3c2a5d0-0c5c-41af-8452-9176c352a63c.png
                            [thumbnail_url] => https://app.ai-client.com/storage/dalle2_text2img/c3c2a5d0-0c5c-41af-8452-9176c352a63c.png.thumbnail.jpg
                            [dimensions] => 512x512
                            [filesize] => 787387
                        )

                    [1] => stdClass Object
                        (
                            [url] => https://app.ai-client.com/storage/dalle2_text2img/6ed1fa99-ab17-4b27-8085-aaf45662870a.png
                            [thumbnail_url] => https://app.ai-client.com/storage/dalle2_text2img/6ed1fa99-ab17-4b27-8085-aaf45662870a.png.thumbnail.jpg
                            [dimensions] => 512x512
                            [filesize] => 787387
                        )

                )

            [waiting_response] =>
            [credits] => 14
            [created_at] => 2023-06-16T22:10:16.000000Z
        )

)

Full code example

<?php
$bearer = '<<your_token_here>>';

// CREATE THREAD
// -------------
$url = "https://app.ai-client.com/api/v1/threads";

$headers = [
  'Accept: application/json',
  'Authorization: Bearer '.$bearer
];

$options = [
    'ai_version_code' => 'dalle2_text2img'
];

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => $options,
  CURLOPT_HTTPHEADER => $headers,
  CURLOPT_RETURNTRANSFER => true
));

$response = curl_exec($curl);
$thread = json_decode($response)->data;
curl_close($curl);

print_r($thread);

/*
stdClass Object
(
    [guid] => bffc10ad-6ebd-4c43-845e-8fa254bc1b51
    [ai_code] => dalle2
    [ai_version_code] => dalle2_text2img
    [title] => New thread with DALL·E 2 Image Generation
    [created_at] => 2023-06-16T22:10:09.000000Z
    [updated_at] => 2023-06-16T22:10:09.000000Z
)
*/

// CREATE THREAD ENTRY OF TYPE request
// -----------------------------------
$url = "https://app.ai-client.com/api/v1/threads/" . $thread->guid . "/entry";

$headers = [
  'Accept: application/json',
  'Authorization: Bearer '.$bearer
];

$options = ['type' => 'request', 'n' => '2', 'size' => '512x512', 'prompt' => "Cats and dogs with lightsabers"];

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => $options,
  CURLOPT_HTTPHEADER => $headers,
  CURLOPT_RETURNTRANSFER => true
));

$response = curl_exec($curl);
$threadEntry = json_decode($response)->data;
curl_close($curl);

print_r($threadEntry);

/*
stdClass Object
(
    [guid] => af92a337-4316-44d2-8a98-ff4d99d2b072
    [thread_guid] => bffc10ad-6ebd-4c43-845e-8fa254bc1b51
    [type] => request
    [error] =>
    [content] => stdClass Object
        (
            [n] => 2
            [size] => 512x512
            [prompt] => Cats and dogs with lightsabers
        )

    [waiting_response] => 1
    [credits] => 0
    [created_at] => 2023-06-16T22:10:09.000000Z
)
*/

// WAIT FOR 60 SECONDS
// -------------------
sleep(60);

// GET THREAD ENTRIES
// ------------------
$url = "https://app.ai-client.com/api/v1/threads/" . $thread->guid . "/entries";

$headers = [
  'Accept: application/json',
  'Authorization: Bearer '.$bearer
];

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => $headers,
  CURLOPT_RETURNTRANSFER => true
));

$response = curl_exec($curl);
$threadEntries = json_decode($response)->data;
curl_close($curl);

print_r($threadEntries);

/*
Array
(
    [0] => stdClass Object
        (
            [guid] => af92a337-4316-44d2-8a98-ff4d99d2b072
            [thread_guid] => bffc10ad-6ebd-4c43-845e-8fa254bc1b51
            [type] => request
            [error] =>
            [content] => stdClass Object
                (
                    [n] => 2
                    [size] => 512x512
                    [prompt] => Cats and dogs with lightsabers
                )

            [waiting_response] =>
            [credits] => 0
            [created_at] => 2023-06-16T22:10:09.000000Z
        )

    [1] => stdClass Object
        (
            [guid] => a0283751-5ac0-4057-9dce-e3afaf87b4f0
            [thread_guid] => bffc10ad-6ebd-4c43-845e-8fa254bc1b51
            [type] => response
            [error] =>
            [content] => Array
                (
                    [0] => stdClass Object
                        (
                            [url] => https://app.ai-client.com/storage/dalle2_text2img/c3c2a5d0-0c5c-41af-8452-9176c352a63c.png
                            [thumbnail_url] => https://app.ai-client.com/storage/dalle2_text2img/c3c2a5d0-0c5c-41af-8452-9176c352a63c.png.thumbnail.jpg
                            [dimensions] => 512x512
                            [filesize] => 787387
                        )

                    [1] => stdClass Object
                        (
                            [url] => https://app.ai-client.com/storage/dalle2_text2img/6ed1fa99-ab17-4b27-8085-aaf45662870a.png
                            [thumbnail_url] => https://app.ai-client.com/storage/dalle2_text2img/6ed1fa99-ab17-4b27-8085-aaf45662870a.png.thumbnail.jpg
                            [dimensions] => 512x512
                            [filesize] => 787387
                        )

                )

            [waiting_response] =>
            [credits] => 14
            [created_at] => 2023-06-16T22:10:16.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 *