// /app/api/bgremove/route.ts import { NextResponse } from "next/server"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; const BGBYE_URL = process.env.BGBYE_URL || process.env.BG_BYE_URL || process.env.BGREMOVER_BASE_URL || // <-- support your existing env var "http://bgbye:7001"; export async function POST(req: Request) { try { const inForm = await req.formData(); const method = String(inForm.get("method") || ""); const file = inForm.get("file") as any; // Loosen the guard: some Node/undici builds return a File-like Blob from a different realm if (!file || !method) { return NextResponse.json({ error: "file and method are required" }, { status: 400 }); } const outForm = new FormData(); const filename = (file as any).name || "upload"; outForm.set("method", method); outForm.set("file", file, filename); const res = await fetch(`${BGBYE_URL}/remove_background/`, { method: "POST", body: outForm }); const buf = await res.arrayBuffer(); return new NextResponse(buf, { status: res.status, headers: { "content-type": res.headers.get("content-type") || "application/octet-stream", "cache-control": "no-store", }, }); } catch (err: any) { return NextResponse.json({ error: String(err?.message || err) }, { status: 500 }); } }