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

Simple AI Image Generation

See more AI Examples

Create an image by providing a text description.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

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

  final ai = CkAi();

  ai.provider = 'openai';

  // Use your provider's API key.
  ai.apiKey = 'MY_API_KEY';

  // Choose a model.
  ai.model = 'gpt-5.6-terra';

  final askParams = CkJsonObject();
  askParams.updateString('image.output_format', 'jpeg');
  askParams.updateString('image.size', '1024x1024');
  askParams.updateString('image.quality', 'low');
  ai.setAskParams(askParams);

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

  // Ask the AI for image output.
  try {
    ai.ask('image');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Get the image response data.
  final bdImageData = CkBinData();
  try {
    ai.getOutputBd(bdImageData);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  bdImageData.writeFile('c:/aaworkarea/out.jpg');

  print('Success.');
}