Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.4.0+

Simple AI Image Generation

See more AI Examples

Create an image by providing a text description.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local ai = chilkat.Ai.new()

ai.provider = "openai"

-- Use your provider's API key.
ai.api_key = "MY_API_KEY"

-- Choose a model.
ai.model = "gpt-5.6-terra"

local askParams = chilkat.JsonObject.new()
askParams:update_string("image.output_format", "jpeg")
askParams:update_string("image.size", "1024x1024")
askParams:update_string("image.quality", "low")
ai:set_ask_params(askParams)

ai:input_add_text("Generate a small, cute illustration of a gray tabby cat hugging a happy otter wearing an orange scarf")

-- Ask the AI for image output.
local success = ai:ask("image")
if not success then
    print(ai.last_error_text)
    return
end

-- Get the image response data.
local bdImageData = chilkat.BinData.new()
success = ai:get_output_bd(bdImageData)
if not success then
    print(ai.last_error_text)
    return
end

bdImageData:write_file("c:/aaworkarea/out.jpg")

print("Success.")