Discussions

Ask a Question
Back to All

Error Retrieving Access Token

import { NextRequest, NextResponse } from "next/server";

const NEXT_PUBLIC_HEYGEN_API_KEY = process.env.NEXT_PUBLIC_HEYGEN_API_KEY;

export async function POST(request: NextRequest) {
try {
if (!NEXT_PUBLIC_HEYGEN_API_KEY) {
throw new Error("API key is missing from .env");
}
const options = {
method: "POST",
headers: {
accept: "application/json",
"content-type": "application/json",
"x-api-key": NEXT_PUBLIC_HEYGEN_API_KEY,
},
body: JSON.stringify({}),
};
const res = await fetch(
"https://api.heygen.com/v1/streaming.create_token",
options
);
const data = await res.json();

return NextResponse.json({ token: data.data.token });

} catch (error) {
console.error("Error retrieving access token:", error);
return NextResponse.json(
{ error: "Error retrieving access token" },
{ status: 500 }
);
}
}