Sample code for 30+ languages & platforms
PureBasic

Upload an Image File to an AI Provider (OpenAI, Google, Antropic, X)

See more AI Examples

Uploads an image file to an AI provider using the provider's File API and returns the id that can later be used to reference the file in an query. This currently works with ChatGPT and Gemini, but not other AI's. Most AI's currently don't have the ability to reference pre-uploaded image data in conversations.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkAi.pb"
IncludeFile "CkBinData.pb"

Procedure ChilkatExample()

    success.i = 0

    ai.i = CkAi::ckCreate()
    If ai.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Can currently be "openai" or "google" because these are the only AI's that can reference pre-loaded image data by file ID.
    CkAi::setCkProvider(ai, "openai")
    ; Use your provider's API key.
    CkAi::setCkApiKey(ai, "MY_API_KEY")

    ; We can upload directly from a file in the filesystem, or from a Chilkat BinData.

    contentType.s = "image/jpeg"
    localFilePath.s = "qa_data/jpg/starfish.jpg"
    filenameOnServer.s = "starfish.jpg"

    ; Upload directly from a file.
    file_id.s = CkAi::ckUploadFile(ai,localFilePath,contentType)
    If CkAi::ckLastMethodSuccess(ai) = 0
        Debug CkAi::ckLastErrorText(ai)
        Debug "AI File Upload Failed."
    Else
        Debug "File ID: " + file_id
        Debug "File uploaded."
    EndIf

    ; Upload from the contents of a Chilkat BinData
    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkBinData::ckLoadFile(bd,localFilePath)
    If success = 0
        Debug CkBinData::ckLastErrorText(bd)
        CkAi::ckDispose(ai)
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

    file_id = CkAi::ckUploadFileBd(ai,bd,filenameOnServer,contentType)
    If CkAi::ckLastMethodSuccess(ai) = 0
        Debug CkAi::ckLastErrorText(ai)
        Debug "AI File Upload Failed."
    Else
        Debug "File ID: " + file_id
        Debug "File uploaded."
    EndIf



    CkAi::ckDispose(ai)
    CkBinData::ckDispose(bd)


    ProcedureReturn
EndProcedure