Skip to main content

Generate images using Squid AI

Generate custom images on the fly.

Use the Squid AI image generator to create bespoke images for your applications.

Benefits

  • Provides bespoke image generation on a global scale, enabling quick creation of images as needed.
  • Secures image generation using the Squid backend context to ensure exclusive access to resources.

Use cases

  • Create one-off images on the fly in your Squid applications.
  • Use the image generator as part of your embedded AI workflow.

How it works

To interact with the image generator, use the Squid Client SDK.

Note

The Squid AI image generator requires admin access. Therefore, its methods must be run in the Squid backend or another admin environment that has access to the app's API key.

Create an image generator

To create an image generator, use Squid AI's .image() method:

Backend code
const imageGenerator = this.squid.ai().image();

You can then use this instance to generate images using the generate() method. generate() takes two parameters: the string prompt for the image to generate, and the AiGenerateImageOptions type that contains options for generating the image. This function returns a promise that resolves to a URL for the generated image.

The following example shows how to generate an image using generate():

Backend code
const options: AiGenerateImageOptions = {
modelName: 'dall-e-3',
quality: 'standard',
size: '1024x1024',
numberOfImagesToGenerate: 1,
};

const imageUrl = await imageGenerator.generate('a pirate ship', options);

The AiGenerateImageOptions can be of type DallEOptions or StableDiffusionCoreOptions. More options will be available in the future.

The DallEOptions type has the following properties:

DallEOptions {
modelName: 'dall-e-3';
quality?: 'hd' | 'standard';
size?: '1024x1024' | '1792x1024' | '1024x1792';
numberOfImagesToGenerate?: 1;
}

The StableDiffusionCoreOptions type has the following properties:

StableDiffusionCoreOptions {
modelName: 'stable-diffusion-core';
aspectRatio?: '16:9' | '1:1' | '21:9' | '2:3' | '3:2' | '4:5' | '5:4' | '9:16' | '9:21'; // default is 1:1
negativePrompt?: string;
seed?: number; // default is 0, where 0 means most random
stylePreset?:
| 'analog-film'
| 'anime'
| 'cinematic'
| 'comic-book'
| 'digital-art'
| 'enhance'
| 'fantasy-art'
| 'isometric'
| 'line-art'
| 'low-poly'
| 'modeling-compound'
| 'neon-punk'
| 'origami'
| 'photographic'
| 'pixel-art'
| 'tile-texture';
outputFormat?: 'jpeg' | 'png' | 'webp'; // default is 'png'
}

Securing AI image generation

Since the Squid AI image generator requires admin access, it should only be run in the Squid backend or another environment with admin privileges. If you are running the image generator from a client application connected to the Squid backend, you can use an Executable to trigger a backend function from the frontend. The following Executable function checks that the user is authenticated, and then if they are, generates an image and sends the URL to the client:

Backend code
import { executable, SquidService } from '@squidcloud/backend';
import { AiGenerateImageOptions } from '@squidcloud/client';

@executable()
async generateImage(prompt: string): Promise<string | Error> {
if (!this.isAuthenticated()) {
return Error('Access denied');
}
const options: AiGenerateImageOptions = {
modelName: 'dall-e-3',
quality: 'standard',
size: '1024x1024',
numberOfImagesToGenerate: 1,
};

return await this.squid
.ai()
.image()
.generate(prompt, options);
}

In addition to image generation, Squid offers AI agent, AI assistant, and Query with AI functionality to build powerful AI agent and AI workflows. To learn more, check out all of the Squid AI documentation.