Cloudflare Docs
Workers AI
Edit this page on GitHub
Set theme to dark (⇧+D)

Text-to-Image

  • Task type: text-to-image
  • TypeScript class: AiTextToImage

​​ Available Embedding Models

List of available models in for this task type:

Model IDDescription
@cf/stabilityai/stable-diffusion-xl-base-1.0Diffusion-based text-to-image generative model by Stability AI. Generates and modify images based on text prompts.
More information
Terms and license

​​ Examples

import { Ai } from '@cloudflare/ai'
export interface Env {
AI: any;
}
export default {
async fetch(request: Request, env: Env) {
const ai = new Ai(env.AI);
const inputs = {
prompt: "cyberpunk cat",
};
const response = await ai.run("@cf/stabilityai/stable-diffusion-xl-base-1.0", inputs);
return new Response(response, {
headers: {
"content-type": "image/png",
},
});
}
}
$ curl https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/stabilityai/stable-diffusion-xl-base-1.0 \
-X POST \
-H "Authorization: Bearer {API_TOKEN}" \
-d '{ "prompt": "cyberpunk cat" }'

Example Workers AI response

The response is a Uint8Array binary with a PNG image.

​​ API schema

The following schema is based on JSON Schema

​​ Input

{
"type": "object",
"properties": {
"prompt": {
"type": "string"
},
"num_steps": {
"type": "integer",
"default": 20,
"maximum": 20
}
},
"required": [
"prompt"
]
}

TypeScript class: AiTextToImageInput

​​ Output

{
"type": "string",
"contentType": "image/png",
"format": "binary"
}

TypeScript class: AiTextToImageOutput