suno-api/src/app/api/custom_generate/route.ts
gohoski 7da7ac6ae2 Implement hCaptcha solving via 2Captcha & other stuff
- implement hCaptcha solving via paid service 2Captcha and browser automation library Playwright with rebrowser-patches

- implement sunoApi instances caching so sessions won't be constantly updated

- add support for entering cookies not only in SUNO_COOKIE, but also the Cookie HTTP header

- update docs and add Russian docs
2025-01-06 05:25:08 +03:00

55 lines
1.6 KiB
TypeScript

import { NextResponse, NextRequest } from "next/server";
import { cookies } from 'next/headers';
import { DEFAULT_MODEL, sunoApi } from "@/lib/SunoApi";
import { corsHeaders } from "@/lib/utils";
export const maxDuration = 60; // allow longer timeout for wait_audio == true
export const dynamic = "force-dynamic";
export async function POST(req: NextRequest) {
if (req.method === 'POST') {
try {
const body = await req.json();
const { prompt, tags, title, make_instrumental, model, wait_audio, negative_tags } = body;
const audioInfo = await (await sunoApi((await cookies()).toString())).custom_generate(
prompt, tags, title,
Boolean(make_instrumental),
model || DEFAULT_MODEL,
Boolean(wait_audio),
negative_tags
);
return new NextResponse(JSON.stringify(audioInfo), {
status: 200,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
} catch (error: any) {
console.error('Error generating custom audio:', error);
return new NextResponse(JSON.stringify({ error: error.response?.data?.detail || error.toString() }), {
status: error.response?.status || 500,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
}
} else {
return new NextResponse('Method Not Allowed', {
headers: {
Allow: 'POST',
...corsHeaders
},
status: 405
});
}
}
export async function OPTIONS(request: Request) {
return new Response(null, {
status: 200,
headers: corsHeaders
});
}