| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- export async function POST(req: Request) {
- const { text, voiceId } = await req.json();
- const apiKey = process.env.ELEVENLABS_API_KEY;
- if (!apiKey) {
- return new Response(
- JSON.stringify({ error: "ELEVENLABS_API_KEY not set" }),
- {
- status: 500,
- headers: { "Content-Type": "application/json" },
- },
- );
- }
- const voice = voiceId || "21m00Tcm4TlvDq8ikWAM";
- const response = await fetch(
- `https://api.elevenlabs.io/v1/text-to-speech/${voice}`,
- {
- method: "POST",
- headers: {
- "xi-api-key": apiKey,
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- text,
- model_id: "eleven_monolingual_v1",
- voice_settings: {
- stability: 0.5,
- similarity_boost: 0.75,
- },
- }),
- },
- );
- if (!response.ok) {
- return new Response(JSON.stringify({ error: "TTS generation failed" }), {
- status: 500,
- headers: { "Content-Type": "application/json" },
- });
- }
- const audioBuffer = await response.arrayBuffer();
- return new Response(audioBuffer, {
- headers: {
- "Content-Type": "audio/mpeg",
- "Cache-Control": "public, max-age=3600",
- },
- });
- }
|