Sample code for 30+ languages & platforms
Tcl

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set ai [new_CkAi]

# Can currently be "openai" or "google" because these are the only AI's that can reference pre-loaded image data by file ID.
CkAi_put_Provider $ai "openai"
# Use your provider's API key.
CkAi_put_ApiKey $ai "MY_API_KEY"

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

set contentType "image/jpeg"
set localFilePath "qa_data/jpg/starfish.jpg"
set filenameOnServer "starfish.jpg"

# Upload directly from a file.
set file_id [CkAi_uploadFile $ai $localFilePath $contentType]
if {[CkAi_get_LastMethodSuccess $ai] == 0} then {
    puts [CkAi_lastErrorText $ai]
    puts "AI File Upload Failed."
} else {
    puts "File ID: $file_id"
    puts "File uploaded."
}

# Upload from the contents of a Chilkat BinData
set bd [new_CkBinData]

set success [CkBinData_LoadFile $bd $localFilePath]
if {$success == 0} then {
    puts [CkBinData_lastErrorText $bd]
    delete_CkAi $ai
    delete_CkBinData $bd
    exit
}

set file_id [CkAi_uploadFileBd $ai $bd $filenameOnServer $contentType]
if {[CkAi_get_LastMethodSuccess $ai] == 0} then {
    puts [CkAi_lastErrorText $ai]
    puts "AI File Upload Failed."
} else {
    puts "File ID: $file_id"
    puts "File uploaded."
}


delete_CkAi $ai
delete_CkBinData $bd