Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oJson
LOCAL oResp
LOCAL oSbResponseBody
LOCAL oJResp
LOCAL nRespStatusCode
LOCAL cUrl
LOCAL oSbLocalFilePath
LOCAL nCreated
LOCAL i
LOCAL nCount_i

nSuccess := 0

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

oHttp := CreateObject("Chilkat.Http")

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

oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("prompt", "A cute baby sea otter")
oJson:UpdateInt("n", 2)
oJson:UpdateString("size", "1024x1024")

//  Adds the "Authorization: Bearer $OPENAI_API_KEY" header.
//  This is NOT a real key.  Change the "sk-vi...." to your own key.
oHttp:AuthToken := "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"

oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", "https://api.openai.com/v1/images/generations", oJson, "application/json", oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oJson:destroy()
    oResp:destroy()
    RETURN
ENDIF

oSbResponseBody := CreateObject("Chilkat.StringBuilder")
oResp:GetBodySb(oSbResponseBody)

oJResp := CreateObject("Chilkat.JsonObject")
oJResp:LoadSb(oSbResponseBody)
oJResp:EmitCompact := 0

? "Response Body:"
? oJResp:Emit()

nRespStatusCode := oResp:StatusCode
? "Response Status Code = " + Str(nRespStatusCode)
IF (nRespStatusCode >= 400)
    ? "Response Header:"
    ? oResp:Header
    ? "Failed."
    oHttp:destroy()
    oJson:destroy()
    oResp:destroy()
    oSbResponseBody:destroy()
    oJResp:destroy()
    RETURN
ENDIF

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

oSbLocalFilePath := CreateObject("Chilkat.StringBuilder")

//  Download each image..
oHttp:ClearHeaders()
oHttp:AuthToken := ""

nCreated := oJResp:IntOf("created")
i := 0
nCount_i := oJResp:SizeOfArray("data")
DO WHILE i < nCount_i
    oJResp:I := i
    cUrl := oJResp:StringOf("data[i].url")

    oSbLocalFilePath:SetString("c:/aaworkarea/image_")
    oSbLocalFilePath:AppendInt(i + 1)
    oSbLocalFilePath:Append(".png")

    nSuccess := oHttp:Download(cUrl, oSbLocalFilePath:GetAsString())
    IF (nSuccess == 0)
        ? oHttp:LastErrorText
        oHttp:destroy()
        oJson:destroy()
        oResp:destroy()
        oSbResponseBody:destroy()
        oJResp:destroy()
        oSbLocalFilePath:destroy()
        RETURN
    ENDIF

    i := i + 1
ENDDO

oHttp:destroy()
oJson:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()
oSbLocalFilePath:destroy()