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

Simple AI Image Generation

See more AI Examples

Create an image by providing a text description.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const ai = try chilkat.Ai.init();
    defer ai.deinit();

    ai.setProvider("openai");

    // Use your provider's API key.
    ai.setApiKey("MY_API_KEY");

    // Choose a model.
    ai.setModel("gpt-5.6-terra");

    const ask_params = try chilkat.JsonObject.init();
    defer ask_params.deinit();
    ask_params.updateString("image.output_format", "jpeg") catch {};
    ask_params.updateString("image.size", "1024x1024") catch {};
    ask_params.updateString("image.quality", "low") catch {};
    ai.setAskParams(ask_params) catch {};

    ai.inputAddText("Generate a small, cute illustration of a gray tabby cat hugging a happy otter wearing an orange scarf") catch {};

    // Ask the AI for image output.
    ai.ask("image") catch {
        std.debug.print("{s}\n", .{try ai.getLastErrorText(alloc)});
        return;
    };

    // Get the image response data.
    const bd_image_data = try chilkat.BinData.init();
    defer bd_image_data.deinit();
    ai.getOutputBd(bd_image_data) catch {
        std.debug.print("{s}\n", .{try ai.getLastErrorText(alloc)});
        return;
    };

    bd_image_data.writeFile("c:/aaworkarea/out.jpg") catch {};

    std.debug.print("Success.\n", .{});
}