Sample code for 30+ languages & platforms
Unicode C

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 Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkJsonObjectW json;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *url;
    HCkStringBuilderW sbLocalFilePath;
    int created;
    int i;
    int count_i;

    success = FALSE;

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

    http = CkHttpW_Create();

    //  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"
    //  }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"prompt",L"A cute baby sea otter");
    CkJsonObjectW_UpdateInt(json,L"n",2);
    CkJsonObjectW_UpdateString(json,L"size",L"1024x1024");

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

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpJson(http,L"POST",L"https://api.openai.com/v1/images/generations",json,L"application/json",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"Failed.\n");
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkHttpResponseW_Dispose(resp);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        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

    sbLocalFilePath = CkStringBuilderW_Create();

    //  Download each image..
    CkHttpW_ClearHeaders(http);
    CkHttpW_putAuthToken(http,L"");

    created = CkJsonObjectW_IntOf(jResp,L"created");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"data");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        url = CkJsonObjectW_stringOf(jResp,L"data[i].url");

        CkStringBuilderW_SetString(sbLocalFilePath,L"c:/aaworkarea/image_");
        CkStringBuilderW_AppendInt(sbLocalFilePath,i + 1);
        CkStringBuilderW_Append(sbLocalFilePath,L".png");

        success = CkHttpW_Download(http,url,CkStringBuilderW_getAsString(sbLocalFilePath));
        if (success == FALSE) {
            wprintf(L"%s\n",CkHttpW_lastErrorText(http));
            CkHttpW_Dispose(http);
            CkJsonObjectW_Dispose(json);
            CkHttpResponseW_Dispose(resp);
            CkStringBuilderW_Dispose(sbResponseBody);
            CkJsonObjectW_Dispose(jResp);
            CkStringBuilderW_Dispose(sbLocalFilePath);
            return;
        }

        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkHttpResponseW_Dispose(resp);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);
    CkStringBuilderW_Dispose(sbLocalFilePath);

    }