> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sunoapi.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Add Instrumental Callbacks

> When instrumental generation tasks are completed, the system will send results to your provided callback URL via POST request

When you submit a task to the Add Instrumental API, you can use the `callBackUrl` parameter to set a callback URL. When the task is completed, the system will automatically push the results to your specified address.

## Callback Mechanism Overview

<Info>
  The callback mechanism eliminates the need to poll the API for task status. The system will proactively push task completion results to your server.
</Info>

### Callback Timing

The system will send callback notifications in the following situations:

* Instrumental generation task completed successfully
* Instrumental generation task failed
* Errors occurred during task processing

### Callback Method

* **HTTP Method**: POST
* **Content Type**: application/json
* **Timeout Setting**: 15 seconds

## Callback Request Format

When the task is completed, the system will send a POST request to your `callBackUrl` in the following format:

<CodeGroup>
  ```json Success Callback theme={null}
  {
    "code": 200,
    "msg": "All generated successfully.",
    "data": {
      "callbackType": "complete",
      "task_id": "2fac****9f72",
      "data": [
        {
          "id": "8551****662c",
          "audio_url": "https://example.cn/****.mp3",
          "source_audio_url": "https://example.cn/****.mp3",
          "stream_audio_url": "https://example.cn/****",
          "source_stream_audio_url": "https://example.cn/****",
          "image_url": "https://example.cn/****.jpeg",
          "source_image_url": "https://example.cn/****.jpeg",
          "prompt": "[Instrumental] Relaxing piano melody",
          "model_name": "chirp-v3-5",
          "title": "Relaxing Piano Instrumental",
          "tags": "relaxing, piano, instrumental",
          "createTime": "2025-01-01 00:00:00",
          "duration": 198.44
        }
      ]
    }
  }
  ```

  ```json Failure Callback theme={null}
  {
    "code": 400,
    "msg": "Instrumental generation failed",
    "data": {
      "callbackType": "error",
      "task_id": "2fac****9f72",
      "data": null
    }
  }
  ```
</CodeGroup>

## Status Code Description

<ParamField path="code" type="integer" required>
  Callback status code indicating task processing result:

  | Status Code | Description                                            |
  | ----------- | ------------------------------------------------------ |
  | 200         | Success - Instrumental generation completed            |
  | 400         | Bad Request - Parameter error, content violation, etc. |
  | 451         | Download Failed - Unable to download related files     |
  | 500         | Server Error - Please try again later                  |
</ParamField>

<ParamField path="msg" type="string" required>
  Status message providing detailed status description
</ParamField>

<ParamField path="data.callbackType" type="string" required>
  Callback type indicating the current callback stage:

  * `text`: Text generation completed
  * `first`: First track completed
  * `complete`: All tracks completed
  * `error`: Task failed
</ParamField>

<ParamField path="data.task_id" type="string" required>
  Task ID, consistent with the taskId returned when you submitted the task
</ParamField>

<ParamField path="data.data" type="array">
  Instrumental generation result information, returned on success
</ParamField>

<ParamField path="data.data[].id" type="string">
  Audio unique identifier (audioId)
</ParamField>

<ParamField path="data.data[].audio_url" type="string">
  Generated instrumental audio file URL
</ParamField>

<ParamField path="data.data[].source_audio_url" type="string">
  Original instrumental audio file URL
</ParamField>

<ParamField path="data.data[].stream_audio_url" type="string">
  Streaming instrumental audio URL
</ParamField>

<ParamField path="data.data[].source_stream_audio_url" type="string">
  Original streaming instrumental audio URL
</ParamField>

<ParamField path="data.data[].image_url" type="string">
  Cover image URL
</ParamField>

<ParamField path="data.data[].source_image_url" type="string">
  Original cover image URL
</ParamField>

<ParamField path="data.data[].prompt" type="string">
  Generation prompt describing the instrumental
</ParamField>

<ParamField path="data.data[].model_name" type="string">
  Model name used for generation
</ParamField>

<ParamField path="data.data[].title" type="string">
  Instrumental track title
</ParamField>

<ParamField path="data.data[].tags" type="string">
  Instrumental track tags
</ParamField>

<ParamField path="data.data[].createTime" type="string">
  Creation time
</ParamField>

<ParamField path="data.data[].duration" type="number">
  Audio duration (seconds)
</ParamField>

## Callback Reception Examples

Here are example codes for receiving callbacks in popular programming languages:

<Tabs>
  <Tab title="Node.js">
    ```javascript theme={null}
    const express = require('express');
    const app = express();

    app.use(express.json());

    app.post('/add-instrumental-callback', (req, res) => {
      const { code, msg, data } = req.body;
      
      console.log('Received instrumental generation callback:', {
        taskId: data.task_id,
        callbackType: data.callbackType,
        status: code,
        message: msg
      });
      
      if (code === 200) {
        // Task completed successfully
        console.log('Instrumental generation completed');
        const instrumentalData = data.data || [];
        
        console.log(`Generated ${instrumentalData.length} instrumental tracks:`);
        instrumentalData.forEach((instrumental, index) => {
          console.log(`Instrumental ${index + 1}:`);
          console.log(`  Title: ${instrumental.title}`);
          console.log(`  Duration: ${instrumental.duration} seconds`);
          console.log(`  Tags: ${instrumental.tags}`);
          console.log(`  Audio URL: ${instrumental.audio_url}`);
          console.log(`  Cover URL: ${instrumental.image_url}`);
        });
        
        // Process generated instrumental
        // Can download audio files, save locally, etc.
        
      } else {
        // Task failed
        console.log('Instrumental generation failed:', msg);
        
        // Handle failure cases...
        if (code === 400) {
          console.log('Parameter error or content violation');
        } else if (code === 451) {
          console.log('File download failed');
        } else if (code === 500) {
          console.log('Server internal error');
        }
      }
      
      // Return 200 status code to confirm callback received
      res.status(200).json({ status: 'received' });
    });

    app.listen(3000, () => {
      console.log('Callback server running on port 3000');
    });
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={null}
    from flask import Flask, request, jsonify
    import requests

    app = Flask(__name__)

    @app.route('/add-instrumental-callback', methods=['POST'])
    def handle_callback():
        data = request.json
        
        code = data.get('code')
        msg = data.get('msg')
        callback_data = data.get('data', {})
        task_id = callback_data.get('task_id')
        callback_type = callback_data.get('callbackType')
        instrumental_data = callback_data.get('data', [])
        
        print(f"Received instrumental generation callback: {task_id}, type: {callback_type}, status: {code}, message: {msg}")
        
        if code == 200:
            # Task completed successfully
            print("Instrumental generation completed")
            
            print(f"Generated {len(instrumental_data)} instrumental tracks:")
            for i, instrumental in enumerate(instrumental_data):
                print(f"Instrumental {i + 1}:")
                print(f"  Title: {instrumental.get('title')}")
                print(f"  Duration: {instrumental.get('duration')} seconds")
                print(f"  Tags: {instrumental.get('tags')}")
                print(f"  Audio URL: {instrumental.get('audio_url')}")
                print(f"  Cover URL: {instrumental.get('image_url')}")
                
                # Download audio file example
                try:
                    audio_url = instrumental.get('audio_url')
                    if audio_url:
                        response = requests.get(audio_url)
                        if response.status_code == 200:
                            filename = f"generated_instrumental_{task_id}_{i + 1}.mp3"
                            with open(filename, "wb") as f:
                                f.write(response.content)
                            print(f"Instrumental saved as {filename}")
                except Exception as e:
                    print(f"Audio download failed: {e}")
                    
        else:
            # Task failed
            print(f"Instrumental generation failed: {msg}")
            
            # Handle failure cases...
            if code == 400:
                print("Parameter error or content violation")
            elif code == 451:
                print("File download failed")
            elif code == 500:
                print("Server internal error")
        
        # Return 200 status code to confirm callback received
        return jsonify({'status': 'received'}), 200

    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=3000)
    ```
  </Tab>

  <Tab title="PHP">
    ```php theme={null}
    <?php
    header('Content-Type: application/json');

    // Get POST data
    $input = file_get_contents('php://input');
    $data = json_decode($input, true);

    $code = $data['code'] ?? null;
    $msg = $data['msg'] ?? '';
    $callbackData = $data['data'] ?? [];
    $taskId = $callbackData['task_id'] ?? '';
    $callbackType = $callbackData['callbackType'] ?? '';
    $instrumentalData = $callbackData['data'] ?? [];

    error_log("Received instrumental generation callback: $taskId, type: $callbackType, status: $code, message: $msg");

    if ($code === 200) {
        // Task completed successfully
        error_log("Instrumental generation completed");
        
        error_log("Generated " . count($instrumentalData) . " instrumental tracks:");
        foreach ($instrumentalData as $index => $instrumental) {
            error_log("Instrumental " . ($index + 1) . ":");
            error_log("  Title: " . ($instrumental['title'] ?? ''));
            error_log("  Duration: " . ($instrumental['duration'] ?? 0) . " seconds");
            error_log("  Tags: " . ($instrumental['tags'] ?? ''));
            error_log("  Audio URL: " . ($instrumental['audio_url'] ?? ''));
            error_log("  Cover URL: " . ($instrumental['image_url'] ?? ''));
            
            // Download audio file example
            try {
                $audioUrl = $instrumental['audio_url'] ?? '';
                if ($audioUrl) {
                    $audioContent = file_get_contents($audioUrl);
                    if ($audioContent !== false) {
                        $filename = "generated_instrumental_{$taskId}_" . ($index + 1) . ".mp3";
                        file_put_contents($filename, $audioContent);
                        error_log("Instrumental saved as $filename");
                    }
                }
            } catch (Exception $e) {
                error_log("Audio download failed: " . $e->getMessage());
            }
        }
        
    } else {
        // Task failed
        error_log("Instrumental generation failed: $msg");
        
        // Handle failure cases...
        if ($code === 400) {
            error_log("Parameter error or content violation");
        } elseif ($code === 451) {
            error_log("File download failed");
        } elseif ($code === 500) {
            error_log("Server internal error");
        }
    }

    // Return 200 status code to confirm callback received
    http_response_code(200);
    echo json_encode(['status' => 'received']);
    ?>
    ```
  </Tab>
</Tabs>

## Best Practices

<Tip>
  ### Callback URL Configuration Recommendations

  1. **Use HTTPS**: Ensure your callback URL uses HTTPS protocol for secure data transmission
  2. **Verify Source**: Verify the legitimacy of the request source in callback processing
  3. **Idempotent Processing**: The same taskId may receive multiple callbacks, ensure processing logic is idempotent
  4. **Quick Response**: Callback processing should return a 200 status code as quickly as possible to avoid timeout
  5. **Asynchronous Processing**: Complex business logic should be processed asynchronously to avoid blocking callback response
  6. **Audio Processing**: Audio download and processing should be done in asynchronous tasks to avoid blocking callback response
</Tip>

<Warning>
  ### Important Reminders

  * Callback URL must be a publicly accessible address
  * Server must respond within 15 seconds, otherwise it will be considered a timeout
  * If 3 consecutive retries fail, the system will stop sending callbacks
  * Please ensure the stability of callback processing logic to avoid callback failures due to exceptions
  * Generated audio URLs may have time limits, recommend downloading and saving promptly
  * Pay attention to content policy compliance to avoid generation failures due to policy violations
</Warning>

## Troubleshooting

If you do not receive callback notifications, please check the following:

<AccordionGroup>
  <Accordion title="Network Connection Issues">
    * Confirm that the callback URL is accessible from the public network
    * Check firewall settings to ensure inbound requests are not blocked
    * Verify that domain name resolution is correct
  </Accordion>

  <Accordion title="Server Response Issues">
    * Ensure the server returns HTTP 200 status code within 15 seconds
    * Check server logs for error messages
    * Verify that the interface path and HTTP method are correct
  </Accordion>

  <Accordion title="Content Format Issues">
    * Confirm that the received POST request body is in JSON format
    * Check that Content-Type is application/json
    * Verify that JSON parsing is correct
  </Accordion>

  <Accordion title="Audio Processing Issues">
    * Confirm that audio URLs are accessible
    * Check audio download permissions and network connections
    * Verify audio save paths and permissions
    * Note whether audio content complies with content policies
  </Accordion>
</AccordionGroup>

## Alternative Solution

If you cannot use the callback mechanism, you can also use polling:

<Card title="Poll Query Results" icon="radar" href="/suno-api/get-music-generation-details">
  Use the get music generation details endpoint to regularly query task status. We recommend querying every 30 seconds.
</Card>
