Hi There,
I will have a form to display in my UI. Submitting the form will trigger an api call to the server in which am making a POST reques to klaviyo
This way
import { NextRequest, NextResponse } from "next/server";
export async function POST(request: NextRequest) {
try {
const { email, firstName, lastName } = await request.json();
const klaviyoApiUrl = `https://a.klaviyo.com/api/v2/list/${process.env.LIST_ID}/members`;
const res = await fetch(klaviyoApiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Klaviyo-API-Key ${process.env.KLAVIYO_PRIVATE_API_KEY}`,
},
body: JSON.stringify({
profiles: :
{
email: email,
first_name: firstName,
last: lastName,
},
],
}),
});
const data = await res.json();
if (!res.ok) {
console.error("Klaviyo API Error:", data);
throw new Error(data.message || "Failed to subscribe to Klaviyo");
}
return NextResponse.json({ success: true });
} catch (error: any) {
console.error("Subscription Error:", error);
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
}
}
I m keep getting
Klaviyo API Error: { message: 'The API key specified is invalid.' }
Subscription Error: Error: The API key specified is invalid.
While I tried old API key and created new API private key with full access. Both result the same.
Any help would be appreciated.
Thank you