Php – How to use the Stablediffusion 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 Stablediffusion
You may select an ai_version_code and an engine. For example : stablediffusion_text2img and stable-diffusion-xl-1024-v0-9.
Send an http POST request to create a new thread.
<?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' => 'stablediffusion_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] => bf1e2937-18ea-48a1-8fa3-219e5aa2bd39 [ai_code] => stablediffusion [ai_version_code] => stablediffusion_text2img [title] => New thread with Stable Diffusion Image Generation [created_at] => 2023-07-13T19:51:33.000000Z [updated_at] => 2023-07-13T19:51:33.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', 'engine' => 'stable-diffusion-xl-1024-v0-9', 'resolution' => '1024x1024', // positive prompt: what we want to see 'prompts[0][text]' => "Delorean hacking time", 'prompts[0][weight]' => 1, // negative prompt: what we want to avoid 'prompts[1][text]' => "Clock", 'prompts[1][weight]' => -1, 'images' => 2, 'steps' => 45, 'style' => "neon-punk" ]; $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] => 2947cf15-d7a7-4e12-ad73-1384d3b10eb8 [thread_guid] => bf1e2937-18ea-48a1-8fa3-219e5aa2bd39 [type] => request [error] => [content] => stdClass Object ( [prompts] => Array ( [0] => stdClass Object ( [text] => Delorean hacking time [weight] => 1 ) [1] => stdClass Object ( [text] => Clock [weight] => -1 ) ) [engine] => stable-diffusion-xl-1024-v0-9 [style] => neon-punk [images] => 2 [steps] => 45 [resolution] => 1024x1024 ) [waiting_response] => 1 [credits] => 0 [created_at] => 2023-07-13T19:51:33.000000Z )
Step 4 – Wait for a response from Stablediffusion
You must now wait for the AI’s response. The “waiting_response” property of your thread entry informs you of Stablediffusion’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 Stablediffusion 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] => 2947cf15-d7a7-4e12-ad73-1384d3b10eb8 [thread_guid] => bf1e2937-18ea-48a1-8fa3-219e5aa2bd39 [type] => request [error] => [content] => stdClass Object ( [prompts] => Array ( [0] => stdClass Object ( [text] => Delorean hacking time [weight] => 1 ) [1] => stdClass Object ( [text] => Clock [weight] => -1 ) ) [engine] => stable-diffusion-xl-1024-v0-9 [style] => neon-punk [images] => 2 [steps] => 45 [resolution] => 1024x1024 ) [waiting_response] => [credits] => 0 [created_at] => 2023-07-13T19:51:33.000000Z ) [1] => stdClass Object ( [guid] => 6ff0c6ff-e77b-42ec-9c9b-a5d75834ae92 [thread_guid] => bf1e2937-18ea-48a1-8fa3-219e5aa2bd39 [type] => response [error] => [content] => Array ( [0] => stdClass Object ( [url] => https://app.ai-client.com/storage/stablediffusion_text2img/acba3af7-db22-430b-9116-a429f35d276d.png [thumbnail_url] => https://app.ai-client.com/storage/stablediffusion_text2img/acba3af7-db22-430b-9116-a429f35d276d.png.thumbnail.jpg [dimensions] => 1024x1024 [filesize] => 1511595 ) [1] => stdClass Object ( [url] => https://app.ai-client.com/storage/stablediffusion_text2img/8d5fe620-325d-4f19-95ed-a6a5a9488f84.png [thumbnail_url] => https://app.ai-client.com/storage/stablediffusion_text2img/8d5fe620-325d-4f19-95ed-a6a5a9488f84.png.thumbnail.jpg [dimensions] => 1024x1024 [filesize] => 1424489 ) ) [waiting_response] => [credits] => 10 [created_at] => 2023-07-13T19:52:24.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' => 'stablediffusion_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] => bf1e2937-18ea-48a1-8fa3-219e5aa2bd39 [ai_code] => stablediffusion [ai_version_code] => stablediffusion_text2img [title] => New thread with Stable Diffusion Image Generation [created_at] => 2023-07-13T19:51:33.000000Z [updated_at] => 2023-07-13T19:51:33.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', 'engine' => 'stable-diffusion-xl-1024-v0-9', 'resolution' => '1024x1024', // positive prompt: what we want to see 'prompts[0][text]' => "Delorean hacking time", 'prompts[0][weight]' => 1, // negative prompt: what we want to avoid 'prompts[1][text]' => "Clock", 'prompts[1][weight]' => -1, 'images' => 2, 'steps' => 45, 'style' => "neon-punk" ]; $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] => 2947cf15-d7a7-4e12-ad73-1384d3b10eb8 [thread_guid] => bf1e2937-18ea-48a1-8fa3-219e5aa2bd39 [type] => request [error] => [content] => stdClass Object ( [prompts] => Array ( [0] => stdClass Object ( [text] => Delorean hacking time [weight] => 1 ) [1] => stdClass Object ( [text] => Clock [weight] => -1 ) ) [engine] => stable-diffusion-xl-1024-v0-9 [style] => neon-punk [images] => 2 [steps] => 45 [resolution] => 1024x1024 ) [waiting_response] => 1 [credits] => 0 [created_at] => 2023-07-13T19:51:33.000000Z ) */ // WAIT FOR 120 SECONDS // ------------------- sleep(120); // 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] => 2947cf15-d7a7-4e12-ad73-1384d3b10eb8 [thread_guid] => bf1e2937-18ea-48a1-8fa3-219e5aa2bd39 [type] => request [error] => [content] => stdClass Object ( [prompts] => Array ( [0] => stdClass Object ( [text] => Delorean hacking time [weight] => 1 ) [1] => stdClass Object ( [text] => Clock [weight] => -1 ) ) [engine] => stable-diffusion-xl-1024-v0-9 [style] => neon-punk [images] => 2 [steps] => 45 [resolution] => 1024x1024 ) [waiting_response] => [credits] => 0 [created_at] => 2023-07-13T19:51:33.000000Z ) [1] => stdClass Object ( [guid] => 6ff0c6ff-e77b-42ec-9c9b-a5d75834ae92 [thread_guid] => bf1e2937-18ea-48a1-8fa3-219e5aa2bd39 [type] => response [error] => [content] => Array ( [0] => stdClass Object ( [url] => https://app.ai-client.com/storage/stablediffusion_text2img/acba3af7-db22-430b-9116-a429f35d276d.png [thumbnail_url] => https://app.ai-client.com/storage/stablediffusion_text2img/acba3af7-db22-430b-9116-a429f35d276d.png.thumbnail.jpg [dimensions] => 1024x1024 [filesize] => 1511595 ) [1] => stdClass Object ( [url] => https://app.ai-client.com/storage/stablediffusion_text2img/8d5fe620-325d-4f19-95ed-a6a5a9488f84.png [thumbnail_url] => https://app.ai-client.com/storage/stablediffusion_text2img/8d5fe620-325d-4f19-95ed-a6a5a9488f84.png.thumbnail.jpg [dimensions] => 1024x1024 [filesize] => 1424489 ) ) [waiting_response] => [credits] => 10 [created_at] => 2023-07-13T19:52:24.000000Z ) ) */
See our Swagger API documentation for more information.