export const dynamic = "force-dynamic";
import { NextResponse } from "next/server";
export const POST = async (request: Request) => {
// Here you could do any user authorization checks you need for your app
const endpoint = "https://api.layercode.com/v1/agents/web/authorize_session";
const apiKey = process.env.LAYERCODE_API_KEY;
if (!apiKey) {
throw new Error("LAYERCODE_API_KEY is not set.");
}
const requestBody = await request.json();
if (!requestBody || !requestBody.agent_id) {
throw new Error("Missing agent_id in request body.");
}
requestBody.config = {
type: "voice",
clients: {
browser: {
enabled: true
}
},
plugins: [
{ use: "stt.deepgram", options: { model_id: "flux" } },
{ use: "turn_manager", options: { mode: "automatic" } },
{ use: "agent.llm", options: { provider: "google", model_id: "gemini-2.5-flash-lite" } },
{ use: "tts.rime", options: { model_id: "mistv2", voice_id: "courtney" } }
],
session_webhook: {
url: "https://example.com/session-webhook",
events: ["session.start", "session.end", "session.update"],
custom_metadata: {
tenant_id: "t_42",
crm_contact_id: "abc-123"
},
custom_headers: {
"x-tenant-id": "t_42",
"x-layercode-flow": "concierge"
}
}
}
try {
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify(requestBody),
});
if (!response.ok) {
const text = await response.text();
throw new Error(text || response.statusText);
}
return NextResponse.json(await response.json());
} catch (error: any) {
console.log("Layercode authorize session response error:", error.message);
return NextResponse.json({ error: error.message }, { status: 500 });
}
};