How to Generate Images from Text Prompts

Last verified 16 Oct 2026

Inference provides a single control plane for managing inference workflows. It includes a Model Catalog where you can view available foundation models, including both DigitalOcean-hosted and third-party commercial models, compare model capabilities and pricing, use routing to match inference requests to the best-fit model, and run inference using serverless or dedicated deployments.

The following cURL, Python PyDo, Python OpenAI, and Gradient Python SDK examples show how to generate an image from a text prompt. Include your model access key and the following in your request:

  • model: The model ID of the image generation model you want to use. Get the model ID using /v1/models or on the available models page.

  • prompt: The text prompt to generate the image from.

  • n: The number of images to generate. Must be between 1 and 10.

  • size: The desired dimensions of the generated image. Supported values are 256x256, 512x512, and 1024x1024.

Make sure to always specify n and size when generating images.

Create a model access key and save it for use with the API.

Using PyDo, the official DigitalOcean API client for Python:

import os
from pydo import Client

client = Client(token=os.environ.get("DIGITALOCEAN_TOKEN"))

resp = client.images.generate(
    model="openai-gpt-image-1",
    prompt="A cute baby sea otter floating on its back in calm blue water",
    size="auto",
    quality="auto",
    n=1,
)

print(resp.data[0].b64_json)

Using Godo, the official DigitalOcean API client for Go:

package main

import (
    "context"
    "fmt"
    "os"

    "github.com/digitalocean/godo"
)

func main() {
    client := godo.NewFromToken(os.Getenv("DIGITALOCEAN_TOKEN"))
    ctx := context.Background()

    resp, _, err := client.ImageGenerations.Generate(ctx, &godo.ImageGenerateParams{
        Model:   "openai-gpt-image-1",
        Prompt:  "A cute baby sea otter floating on its back in calm blue water",
        Size:    godo.PtrTo("auto"),
        Quality: godo.PtrTo("auto"),
        N:       1,
    })
    if err != nil {
        panic(err)
    }

    fmt.Println(resp.Data[0].B64JSON)
}

Using dots, the official DigitalOcean API client for JavaScript:

import { InferenceClient } from "@digitalocean/dots";

const client = new InferenceClient({
    apiKey: process.env.DIGITALOCEAN_TOKEN,
});

const resp = await client.images.generate({
    model: "openai-gpt-image-1",
    prompt: "A cute baby sea otter floating on its back in calm blue water",
    size: "auto",
    quality: "auto",
    n: 1,
});

console.log(resp.data[0].b64_json);

Send a POST request to https://inference.do-ai.run/v1/images/generations.

Using cURL:

curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
  -d '{"prompt": "A cute baby sea otter floating on its back in calm blue water", "model": "openai-gpt-image-1", "size": "auto", "quality": "auto"}' \
  "https://inference.do-ai.run/v1/images/generations"

The response includes a JSON object with a Base64 image string and other details such as image format and tokens used:

{
    "background": "opaque",
    "created": 1770659857,
    "data": [
        {
            "b64_json": "iVBORw0KGgoAAAANSUhEU...
        }
    ],
    "output_format": "png",
    "quality": "medium",
    "size": "1024x1024",
    "usage": {
        "input_tokens": 20,
        "input_tokens_details": {
            "text_tokens": 20
        },
        "output_tokens": 1056,
        "total_tokens": 1076
    }
}

If you want to save the image as a file, pipe the image string to a file using jq and base64:

curl -X POST https://inference.do-ai.run/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $MODEL_ACCESS_KEY" \
  -d '{
    "model": "openai-gpt-image-1",
    "prompt": "A cute baby sea otter floating on its back in calm blue water",
    "n": 1,
    "size": "1024x1024"
  }' | jq -r '.data[0].b64_json' | base64 --decode > sea_otter.png

An image named sea_otter.png is created in your current directory after a few seconds.

You can also use the Python OpenAI SDK:

from openai import OpenAI
from dotenv import load_dotenv
import os, base64

load_dotenv()

client = OpenAI(
    base_url="https://inference.do-ai.run/v1",
    api_key=os.getenv("MODEL_ACCESS_KEY"),
)

result = client.images.generate(
    model="openai-gpt-image-1",
    prompt="A cute baby sea otter floating on its back in calm blue water",
    size="1024x1024",
    n=1
)

b64 = result.data[0].b64_json
with open("sea_otter.png", "wb") as f:
    f.write(base64.b64decode(b64))

print("Saved sea_otter.png")

You can also use the Go SDK (Godo):

package main

import (
	"context"
	"encoding/base64"
	"fmt"
	"os"

	"github.com/digitalocean/godo"
)

func main() {
	client := godo.NewFromToken(os.Getenv("MODEL_ACCESS_KEY"))
	ctx := context.Background()

	result, _, err := client.ImageGenerations.Generate(ctx, &godo.ImageGenerateParams{
		Model:  "openai-gpt-image-1",
		Prompt: "A cute baby sea otter floating on its back in calm blue water",
		Size:   godo.PtrTo("1024x1024"),
		N:      1,
	})
	if err != nil {
		panic(err)
	}

	imageBytes, err := base64.StdEncoding.DecodeString(result.Data[0].B64JSON)
	if err != nil {
		panic(err)
	}

	if err := os.WriteFile("sea_otter.png", imageBytes, 0644); err != nil {
		panic(err)
	}
	fmt.Println("Saved sea_otter.png")
}

We can't find any results for your search.

Try using different keywords or simplifying your search terms.