Sample code for 30+ languages & platforms
Rust

OpenAI (ChatGPT) Create Image

See more OpenAI ChatGPT Examples

Show how to send a POST to create an image. Gets the JSON response with URLs and then downloads images to files.

Chilkat Rust Downloads

Rust

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let http = chilkat::Http::new();

// Implements the following CURL command:

// curl https://api.openai.com/v1/images/generations \
//   -H "Content-Type: application/json" \
//   -H "Authorization: Bearer $OPENAI_API_KEY" \
//   -d '{
//     "prompt": "A cute baby sea otter",
//     "n": 2,
//     "size": "1024x1024"
//   }'

// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code

// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON

// The following JSON is sent in the request body.

// {
//   "prompt": "A cute baby sea otter",
//   "n": 2,
//   "size": "1024x1024"
// }

let json = chilkat::JsonObject::new();
let _ = json.update_string("prompt", "A cute baby sea otter");
let _ = json.update_int("n", 2);
let _ = json.update_string("size", "1024x1024");

// Adds the "Authorization: Bearer $OPENAI_API_KEY" header.
// This is NOT a real key.  Change the "sk-vi...." to your own key.
http.set_auth_token("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");

let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://api.openai.com/v1/images/generations", &json, "application/json", &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

let sb_response_body = chilkat::StringBuilder::new();
let _ = resp.get_body_sb(&sb_response_body);

let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load_sb(&sb_response_body);
j_resp.set_emit_compact(false);

println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());

let resp_status_code = resp.status_code();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
    println!("Response Header:");
    println!("{}", resp.header());
    println!("Failed.");
    return;
}

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "created": 1589478378,
//   "data": [
//     {
//       "url": "https://..."
//     },
//     {
//       "url": "https://..."
//     }
//   ]
// }

// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON

let mut url = String::new();
let sb_local_file_path = chilkat::StringBuilder::new();

// Download each image..
http.clear_headers();
http.set_auth_token("");

let created = j_resp.int_of("created");
let mut i = 0;
let count_i = j_resp.size_of_array("data");
while i < count_i {
    j_resp.set_i(i);
    url = j_resp.string_of("data[i].url").unwrap_or_default();

    let _ = sb_local_file_path.set_string("c:/aaworkarea/image_");
    let _ = sb_local_file_path.append_int(i + 1);
    let _ = sb_local_file_path.append(".png");

    if http.download(&url, &sb_local_file_path.get_as_string().unwrap_or_default()).is_err() {
        println!("{}", http.last_error_text());
        return;
    }

    i = i + 1;
}