File size: 2,328 Bytes
b6bdcd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Main function to generate audio
async function generateAudio() {
    const taskId = document.getElementById('taskId').value.trim();
    const audioId = document.getElementById('audioId').value.trim();
    const generateBtn = document.getElementById('generateBtn');
    const loading = document.getElementById('loading');
    const status = document.getElementById('status');
    const result = document.getElementById('result');
    const response = document.getElementById('response');
    
    // Validate inputs
    if (!taskId || !audioId) {
        showStatus('Please fill in both Task ID and Audio ID', 'error');
        return;
    }
    
    // Validate API key
    if (!window.config.apiKey || window.config.apiKey === '') {
        showStatus('API Key is not configured. Please set SUNO_API_KEY in Space secrets.', 'error');
        return;
    }
    
    // Validate callback URL
    if (!window.config.callbackUrl || window.config.callbackUrl === '') {
        showStatus('Callback URL is not configured. Please set CALLBACK_URL in Space secrets.', 'error');
        return;
    }
    
    // Prepare request
    const requestData = {
        taskId: taskId,
        audioId: audioId,
        callBackUrl: window.config.callbackUrl
    };
    
    const options = {
        method: 'POST',
        headers: {
            'Authorization': `Bearer ${window.config.apiKey}`,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(requestData)
    };
    
    // Show loading state
    generateBtn.disabled = true;
    loading.classList.add('show');
    status.classList.remove('show', 'success', 'error');
    result.classList.remove('show');
    
    try {
        // Make API call
        const apiResponse = await fetch('https://api.sunoapi.org/api/v1/wav/generate', options);
        const data = await apiResponse.json();
        
        // Display response
        response.textContent = JSON.stringify(data, null, 2);
        result.classList.add('show');
        
        // Show success status
        if (apiResponse.ok) {
            showStatus('✅ Audio generation request sent successfully!', 'success');
        } else {
            showStatus(`❌ Error: ${data.message || 'Unknown error'}`, 'error');
        }
        
    } catch (error) {
        // Handle errors