feat: Suno sunoapi.org v1 스펙 적용 + 팩 상세 섹션 재구성

- Suno API: /api/v1/generate (taskId) + record-info 폴링으로 전환
- SUNO_API_URL 기본값 https://api.sunoapi.org, SUNO_API_KEY만 필수
- 모델: V4 / V4_5 / V3_5, customMode·callBackUrl 지원
- 결과 카드: sunoData 배열(오디오·이미지·태그·duration) 렌더
- 팩 상세: 팩 구성품 + 추천 대상 섹션 추가, Before/After 제거
This commit is contained in:
2026-04-15 03:34:44 +09:00
parent a362f7b387
commit b8c5a202ce
4 changed files with 249 additions and 186 deletions

View File

@@ -13,12 +13,12 @@ type GenerateBody = {
};
export async function POST(request: Request) {
const apiUrl = process.env.SUNO_API_URL;
const apiUrl = process.env.SUNO_API_URL ?? 'https://api.sunoapi.org';
const apiKey = process.env.SUNO_API_KEY;
if (!apiUrl || !apiKey) {
if (!apiKey) {
return NextResponse.json(
{ error: 'Suno API 미설정 (SUNO_API_URL / SUNO_API_KEY 환경변수 필요)' },
{ error: 'Suno API 미설정 (SUNO_API_KEY 환경변수 필요)' },
{ status: 503 },
);
}
@@ -30,27 +30,30 @@ export async function POST(request: Request) {
return NextResponse.json({ error: 'Invalid JSON' }, { status: 400 });
}
const endpoint = body.mode === 'custom' ? '/api/custom_generate' : '/api/generate';
const origin = new URL(request.url).origin;
const callBackUrl = `${origin}/api/studio/callback`;
const payload =
body.mode === 'custom'
? {
prompt: body.lyrics ?? '',
tags: body.tags ?? '',
title: body.title ?? '',
make_instrumental: !!body.make_instrumental,
model: body.model ?? 'chirp-v3-5',
wait_audio: false,
}
: {
prompt: body.prompt ?? '',
make_instrumental: !!body.make_instrumental,
model: body.model ?? 'chirp-v3-5',
wait_audio: false,
};
const isCustom = body.mode === 'custom';
const payload = isCustom
? {
prompt: body.lyrics ?? '',
style: body.tags ?? '',
title: body.title ?? 'Untitled',
customMode: true,
instrumental: !!body.make_instrumental,
model: body.model ?? 'V4',
callBackUrl,
}
: {
prompt: body.prompt ?? '',
customMode: false,
instrumental: !!body.make_instrumental,
model: body.model ?? 'V4',
callBackUrl,
};
try {
const res = await fetch(`${apiUrl.replace(/\/$/, '')}${endpoint}`, {
const res = await fetch(`${apiUrl.replace(/\/$/, '')}/api/v1/generate`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@@ -60,10 +63,10 @@ export async function POST(request: Request) {
});
const data = await res.json().catch(() => null);
if (!res.ok) {
if (!res.ok || (data && typeof data === 'object' && 'code' in data && data.code !== 200)) {
return NextResponse.json(
{ error: '생성 실패', detail: data ?? (await res.text().catch(() => '')) },
{ status: res.status },
{ status: res.ok ? 502 : res.status },
);
}
return NextResponse.json({ ok: true, data });

View File

@@ -3,23 +3,23 @@ import { NextResponse } from 'next/server';
export const runtime = 'nodejs';
export async function GET(request: Request) {
const apiUrl = process.env.SUNO_API_URL;
const apiUrl = process.env.SUNO_API_URL ?? 'https://api.sunoapi.org';
const apiKey = process.env.SUNO_API_KEY;
if (!apiUrl || !apiKey) {
if (!apiKey) {
return NextResponse.json(
{ error: 'Suno API 미설정 (SUNO_API_URL / SUNO_API_KEY 환경변수 필요)' },
{ error: 'Suno API 미설정 (SUNO_API_KEY 환경변수 필요)' },
{ status: 503 },
);
}
const { searchParams } = new URL(request.url);
const ids = searchParams.get('ids');
if (!ids) return NextResponse.json({ error: 'ids required' }, { status: 400 });
const taskId = searchParams.get('taskId');
if (!taskId) return NextResponse.json({ error: 'taskId required' }, { status: 400 });
try {
const res = await fetch(
`${apiUrl.replace(/\/$/, '')}/api/get?ids=${encodeURIComponent(ids)}`,
`${apiUrl.replace(/\/$/, '')}/api/v1/generate/record-info?taskId=${encodeURIComponent(taskId)}`,
{ headers: { Authorization: `Bearer ${apiKey}` } },
);
const data = await res.json().catch(() => null);