import { NextRequest, NextResponse } from 'next/server' import { fal } from "@fal-ai/client" if (!process.env.FAL_AI_KEY) { throw new Error('FAL_AI_KEY is not configured in environment variables') } // Configure fal client fal.config({ credentials: process.env.FAL_AI_KEY }) export async function POST(req: NextRequest): Promise { try { const { prompt, modelId } = await req.json() if (!prompt || !modelId) { return NextResponse.json( { error: 'Prompt and modelId are required' }, { status: 400 } ) } console.log('Generating with Flux model:', modelId) console.log('Prompt:', prompt) const result = await fal.subscribe(modelId, { input: { prompt: prompt, }, logs: true, onQueueUpdate: (update) => { if (update.status === "IN_PROGRESS") { console.log('Generation progress:', update.logs.map((log) => log.message)) } }, }) console.log('Flux generation result:', result) if (!result.data?.images?.[0]?.url) { throw new Error('No image URL in response') } return NextResponse.json({ imageUrl: result.data.images[0].url }) } catch (error) { console.error('Flux generation error:', error) return NextResponse.json( { error: error instanceof Error ? error.message : 'Failed to generate image' }, { status: 500 } ) } } export const dynamic = 'force-dynamic'