BrandApp Playground

Image Generation

Generate images with configurable style and aspect ratio.

app/api/ai/image/route.ts
import { getAi } from "@/lib/ai";
import { NextRequest, NextResponse } from "next/server";

export async function POST(request: NextRequest) {
  const { prompt, negativePrompt, style, aspectRatio } = await request.json();
  const ai = await getAi();

  const result = await ai.generateImage({
    prompt,
    // Optional: elements to steer the model away from.
    negativePrompt,
    style,
    aspectRatio: aspectRatio ?? "1:1",
  });

  return NextResponse.json({
    url: result.url,
    size: result.size,
    // creditCost is nested under usage.
    creditCost: result.usage.creditCost,
    creditsRemaining: result.creditsRemaining,
  });
}