Generate and save images

Create an image, save its bytes through Storage and keep the result attached to its AI request.

On this pageSource-backed Markdown

Generate an image for your app

Use app().ai.generateImage() for an image operation, or this.generateImage() inside an agent tool. Supply a prompt and optional model, size, quality and output format. The current image adapter uses the OpenAI Images API.

Provide a store callback to save the returned bytes through your configured Storage disk or Media workflow. Its return value becomes result.stored and the saved request’s response reference.

server/ai/createHelpImage.ts
ts
import { app } from '@db3.ai/app/server';

/** Generates a PNG and records the application's storage reference on its AI request. */
export async function createHelpImage(prompt: string, path: string) {
	return app().ai.generateImage({ prompt, outputFormat: 'png' }, {
		/** Saves bytes through the configured framework disk without exposing provider URLs. */
		store: async ({ b64Json }) => {
			const disk = app().storage.disk();
			await disk.put(path, Buffer.from(b64Json, 'base64'), { mimeType: 'image/png' });
			return { disk: disk.name, path };
		},
	});
}

Keep files and tracking together

The result includes b64Json, model, aiRequestId and your storage reference. Raw image bytes are omitted from the persisted provider summary. Choose the output path in trusted app code and authorize any later download.

A storage failure fails the operation even if the provider already generated the image. Inspect the saved request before repeating a costly generation. Image and embedding calls currently use one provider; automatic provider failover is available for text and agent runs.