Browse Source

fix: update fal.ai CDN upload to use two-step initiate + PUT flow

The old single-POST endpoint (fal.ai/api/storage/upload) returns 404.
Switched to the current fal.ai upload API: initiate via
rest.alpha.fal.ai/storage/upload/initiate, then PUT binary data to
the returned signed URL.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
handoff-20260429-1057
shrimbly 5 months ago
parent
commit
3fea13e2cf
  1. 43
      src/app/api/generate/route.ts

43
src/app/api/generate/route.ts

@ -817,23 +817,44 @@ async function uploadImageToFal(base64DataUrl: string, apiKey: string | null): P
const contentType = match[1];
const binaryData = Buffer.from(match[2], "base64");
const headers: Record<string, string> = {
"Content-Type": contentType,
};
if (apiKey) headers["Authorization"] = `Key ${apiKey}`;
const authHeaders: Record<string, string> = {};
if (apiKey) authHeaders["Authorization"] = `Key ${apiKey}`;
const response = await fetch("https://fal.ai/api/storage/upload", {
method: "POST",
headers,
// Step 1: Initiate upload to get a signed PUT URL
const ext = contentType.split("/")[1] || "png";
const initiateResponse = await fetch(
"https://rest.alpha.fal.ai/storage/upload/initiate?storage_type=fal-cdn-v3",
{
method: "POST",
headers: {
"Content-Type": "application/json",
...authHeaders,
},
body: JSON.stringify({
content_type: contentType,
file_name: `${Date.now()}.${ext}`,
}),
}
);
if (!initiateResponse.ok) {
throw new Error(`Failed to initiate fal CDN upload: ${initiateResponse.status}`);
}
const { upload_url: uploadUrl, file_url: fileUrl } = await initiateResponse.json();
// Step 2: PUT the binary data to the signed URL
const putResponse = await fetch(uploadUrl, {
method: "PUT",
headers: { "Content-Type": contentType },
body: binaryData,
});
if (!response.ok) {
throw new Error(`Failed to upload to fal CDN: ${response.status}`);
if (!putResponse.ok) {
throw new Error(`Failed to upload to fal CDN: ${putResponse.status}`);
}
const { url } = await response.json();
return url;
return fileUrl;
}
/**

Loading…
Cancel
Save