import { NextResponse } from "next/server"; export async function POST(req: Request) { try { const apiKey = process.env.KLAVIYO_PRIVATE_API_KEY; if (!apiKey) { return NextResponse.json( { error: "Klaviyo not configured." }, { status: 500 } ); } const body = await req.json(); const { email, firstName, lastName } = body; if (!email) { return NextResponse.json({ error: "email is required." }, { status: 400 }); } const res = await fetch("https://a.klaviyo.com/api/profiles/", { method: "POST", headers: { Authorization: `Klaviyo-API-Key ${apiKey}`, "Content-Type": "application/json", Accept: "application/json", Revision: "2024-02-15", }, body: JSON.stringify({ data: { type: "profile", attributes: { email, first_name: firstName, last_name: lastName, }, }, }), }); const data = await res.json(); if (!res.ok) { return NextResponse.json( { error: data?.errors?.[0]?.detail || "Klaviyo error." }, { status: res.status } ); } return NextResponse.json({ profile: data.data }); } catch (err: any) { return NextResponse.json( { error: err?.message || "Klaviyo identify failed." }, { status: 500 } ); } }