|
| 1 | +# music-2.0 |
| 2 | + |
| 3 | +{% columns %} |
| 4 | +{% column width="66.66666666666666%" %} |
| 5 | +{% hint style="info" %} |
| 6 | +This documentation is valid for the following list of our models: |
| 7 | + |
| 8 | +* `minimax/music-2.0` |
| 9 | +{% endhint %} |
| 10 | +{% endcolumn %} |
| 11 | + |
| 12 | +{% column width="33.33333333333334%" %} |
| 13 | +<a href="https://aimlapi.com/app/minimax/music-2-0" class="button primary">Try in Playground</a> |
| 14 | +{% endcolumn %} |
| 15 | +{% endcolumns %} |
| 16 | + |
| 17 | +A fast and cost-efficient music generator optimized for high-quality music production. The model creates full-length songs (up to 4 minutes) featuring natural-sounding vocals and detailed instrumental arrangements. |
| 18 | + |
| 19 | +## Setup your API Key |
| 20 | + |
| 21 | +If you don’t have an API key for the AI/ML API yet, feel free to use our [Quickstart guide](https://docs.aimlapi.com/quickstart/setting-up). |
| 22 | + |
| 23 | +## API Schemas |
| 24 | + |
| 25 | +### Generate music sample <a href="#retrieve-the-generated-video-from-the-server" id="retrieve-the-generated-video-from-the-server"></a> |
| 26 | + |
| 27 | +This endpoint generates a music piece based on the prompt (which includes style instructions) and the provided lyrics. It returns a generation task ID, its status, and related metadata. |
| 28 | + |
| 29 | +{% openapi-operation spec="music-2-0" path="/v2/generate/audio" method="post" %} |
| 30 | +[OpenAPI music-2-0](https://raw.githubusercontent.com/aimlapi/api-docs/refs/heads/main/docs/api-references/music-models/MiniMax/music-2.0.json) |
| 31 | +{% endopenapi-operation %} |
| 32 | + |
| 33 | +### Retrieve the generated music sample from the server <a href="#retrieve-the-generated-video-from-the-server" id="retrieve-the-generated-video-from-the-server"></a> |
| 34 | + |
| 35 | +After sending a request for music generation, this task is added to the queue. Based on the service's load, the generation can be completed in 50-60 seconds or take a bit more. |
| 36 | + |
| 37 | +{% openapi-operation spec="universal-audio-fetch" path="/v2/generate/audio" method="get" %} |
| 38 | +[OpenAPI universal-audio-fetch](https://raw.githubusercontent.com/aimlapi/api-docs/refs/heads/main/docs/api-references/music-models/universal-audio-fetch.json) |
| 39 | +{% endopenapi-operation %} |
| 40 | + |
| 41 | +## Quick Code Example |
| 42 | + |
| 43 | +Here’s an example of generating an audio file using a prompt with style instructions and a separate parameter for the lyrics. |
| 44 | + |
| 45 | +{% tabs %} |
| 46 | +{% tab title="Python" %} |
| 47 | +{% code overflow="wrap" %} |
| 48 | +```python |
| 49 | +import time |
| 50 | +import requests |
| 51 | + |
| 52 | +# Insert your AI/ML API key instead of <YOUR_AIMLAPI_KEY>: |
| 53 | +aimlapi_key = '<YOUR_AIMLAPI_KEY>' |
| 54 | + |
| 55 | +# Creating and sending an audio generation task to the server (returns a generation ID) |
| 56 | +def generate_audio(): |
| 57 | + url = "https://api.aimlapi.com/v2/generate/audio" |
| 58 | + payload = { |
| 59 | + "model": "minimax/music-2.0", |
| 60 | + "prompt": "A calm and soothing instrumental music with gentle piano and soft strings.", |
| 61 | + "lyrics": "[Verse]\nStreetlights flicker, the night breeze sighs\nShadows stretch as I walk alone\nAn old coat wraps my silent sorrow\nWandering, longing, where should I go\n[Chorus]\nPushing the wooden door, the aroma spreads\nIn a familiar corner, a stranger gazes back\nWarm lights flicker, memories awaken\nIn this small cafe, I find my way\n[Verse]\nRaindrops tap on the windowpane\nA melody plays, soft and low\nThe clink of cups, the murmur of dreams\nIn this haven, I find my home\n[Chorus]\nPushing the wooden door, the aroma spreads\nIn a familiar corner, a stranger gazes back\nWarm lights flicker, memories awaken\nIn this small cafe, I find my way" |
| 62 | + } |
| 63 | + headers = {"Authorization": f"Bearer {aimlapi_key}", "Content-Type": "application/json"} |
| 64 | + |
| 65 | + response = requests.post(url, json=payload, headers=headers) |
| 66 | + |
| 67 | + if response.status_code >= 400: |
| 68 | + print(f"Error: {response.status_code} - {response.text}") |
| 69 | + else: |
| 70 | + response_data = response.json() |
| 71 | + print("Generation:", response_data) |
| 72 | + return response_data |
| 73 | + |
| 74 | + |
| 75 | +# Requesting the result of the generation task from the server using the generation_id: |
| 76 | +def retrieve_audio(gen_id): |
| 77 | + url = "https://api.aimlapi.com/v2/generate/audio" |
| 78 | + params = { |
| 79 | + "generation_id": gen_id, |
| 80 | + } |
| 81 | + headers = {"Authorization": f"Bearer {aimlapi_key}", "Content-Type": "application/json"} |
| 82 | + |
| 83 | + response = requests.get(url, params=params, headers=headers) |
| 84 | + return response.json() |
| 85 | + |
| 86 | + |
| 87 | +# This is the main function of the program. From here, we sequentially call the audio generation and then repeatedly request the result from the server every 10 seconds: |
| 88 | +def main(): |
| 89 | + # Running video generation and getting a task id |
| 90 | + gen_response = generate_audio() |
| 91 | + print(gen_response) |
| 92 | + gen_id = gen_response.get("id") |
| 93 | + print("Generation ID: ", gen_id) |
| 94 | + |
| 95 | + # Try to retrieve the video from the server every 15 sec |
| 96 | + if gen_id: |
| 97 | + start_time = time.time() |
| 98 | + |
| 99 | + timeout = 1000 |
| 100 | + while time.time() - start_time < timeout: |
| 101 | + response_data = retrieve_audio(gen_id) |
| 102 | + |
| 103 | + if response_data is None: |
| 104 | + print("Error: No response from API") |
| 105 | + break |
| 106 | + |
| 107 | + status = response_data.get("status") |
| 108 | + |
| 109 | + if status in ["queued", "generating"]: |
| 110 | + print(f"Status: {status}. Checking again in 15 seconds.") |
| 111 | + time.sleep(15) |
| 112 | + else: |
| 113 | + print("Processing complete:\n", response_data) |
| 114 | + return response_data |
| 115 | + |
| 116 | + print("Timeout reached. Stopping.") |
| 117 | + return None |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + main() |
| 122 | +``` |
| 123 | +{% endcode %} |
| 124 | +{% endtab %} |
| 125 | + |
| 126 | +{% tab title="JavaScript" %} |
| 127 | +{% code overflow="wrap" %} |
| 128 | +```javascript |
| 129 | +// Insert your AI/ML API key instead of <YOUR_AIMLAPI_KEY>: |
| 130 | +const API_KEY = '<YOUR_AIMLAPI_KEY>'; |
| 131 | + |
| 132 | +async function generateAudio() { |
| 133 | + const url = 'https://api.aimlapi.com/v2/generate/audio'; |
| 134 | + const payload = { |
| 135 | + model: 'minimax/music-2.0', |
| 136 | + prompt: 'A calm and soothing instrumental music with gentle piano and soft strings.', |
| 137 | + lyrics: '[Verse]\nStreetlights flicker, the night breeze sighs\nShadows stretch as I walk alone\nAn old coat wraps my silent sorrow\nWandering, longing, where should I go\n[Chorus]\nPushing the wooden door, the aroma spreads\nIn a familiar corner, a stranger gazes back\nWarm lights flicker, memories awaken\nIn this small cafe, I find my way\n[Verse]\nRaindrops tap on the windowpane\nA melody plays, soft and low\nThe clink of cups, the murmur of dreams\nIn this haven, I find my home\n[Chorus]\nPushing the wooden door, the aroma spreads\nIn a familiar corner, a stranger gazes back\nWarm lights flicker, memories awaken\nIn this small cafe, I find my way' |
| 138 | + }; |
| 139 | + |
| 140 | + const response = await fetch(url, { |
| 141 | + method: 'POST', |
| 142 | + headers: { |
| 143 | + 'Authorization': `Bearer ${API_KEY}`, |
| 144 | + 'Content-Type': 'application/json' |
| 145 | + }, |
| 146 | + body: JSON.stringify(payload) |
| 147 | + }); |
| 148 | + |
| 149 | + if (!response.ok) { |
| 150 | + console.error(`Error: ${response.status} - ${await response.text()}`); |
| 151 | + return null; |
| 152 | + } |
| 153 | + |
| 154 | + const data = await response.json(); |
| 155 | + console.log('Generation:', data); |
| 156 | + return data; |
| 157 | +} |
| 158 | + |
| 159 | +async function retrieveAudio(generationId) { |
| 160 | + const url = `https://api.aimlapi.com/v2/generate/audio?generation_id=${generationId}`; |
| 161 | + |
| 162 | + const response = await fetch(url, { |
| 163 | + method: 'GET', |
| 164 | + headers: { |
| 165 | + 'Authorization': `Bearer ${API_KEY}`, |
| 166 | + 'Content-Type': 'application/json' |
| 167 | + } |
| 168 | + }); |
| 169 | + |
| 170 | + if (!response.ok) { |
| 171 | + console.error(`Error: ${response.status} - ${await response.text()}`); |
| 172 | + return null; |
| 173 | + } |
| 174 | + |
| 175 | + return await response.json(); |
| 176 | +} |
| 177 | + |
| 178 | +async function main() { |
| 179 | + const generationResponse = await generateAudio(); |
| 180 | + |
| 181 | + if (!generationResponse || !generationResponse.id) { |
| 182 | + console.error('No generation ID received.'); |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + const genId = generationResponse.id; |
| 187 | + const timeout = 600000; // 10 minutes |
| 188 | + const interval = 15000; // 15 seconds |
| 189 | + const start = Date.now(); |
| 190 | + |
| 191 | + const intervalId = setInterval(async () => { |
| 192 | + if (Date.now() - start > timeout) { |
| 193 | + console.log('Timeout reached. Stopping.'); |
| 194 | + clearInterval(intervalId); |
| 195 | + return; |
| 196 | + } |
| 197 | + |
| 198 | + const result = await retrieveAudio(genId); |
| 199 | + |
| 200 | + if (!result) { |
| 201 | + console.error('No response from API.'); |
| 202 | + clearInterval(intervalId); |
| 203 | + return; |
| 204 | + } |
| 205 | + |
| 206 | + const status = result.status; |
| 207 | + if (['generating', 'queued'].includes(status)) { |
| 208 | + console.log(`Status: ${status}. Checking again in 15 seconds.`); |
| 209 | + } else { |
| 210 | + console.log('Generation complete:\n', result); |
| 211 | + clearInterval(intervalId); |
| 212 | + } |
| 213 | + }, interval); |
| 214 | +} |
| 215 | + |
| 216 | +main(); |
| 217 | +``` |
| 218 | +{% endcode %} |
| 219 | +{% endtab %} |
| 220 | +{% endtabs %} |
| 221 | + |
| 222 | +<details> |
| 223 | + |
| 224 | +<summary>Response</summary> |
| 225 | + |
| 226 | +{% code overflow="wrap" %} |
| 227 | +```json5 |
| 228 | +Generation: {'id': 'e84c6bd4-bb02-4702-a55d-9b73e170c110:minimax/music-2.0', 'status': 'queued'} |
| 229 | +{'id': 'e84c6bd4-bb02-4702-a55d-9b73e170c110:minimax/music-2.0', 'status': 'queued'} |
| 230 | +Generation ID: e84c6bd4-bb02-4702-a55d-9b73e170c110:minimax/music-2.0 |
| 231 | +Status: generating. Checking again in 15 seconds. |
| 232 | +Status: generating. Checking again in 15 seconds. |
| 233 | +Status: generating. Checking again in 15 seconds. |
| 234 | +Status: generating. Checking again in 15 seconds. |
| 235 | +Status: generating. Checking again in 15 seconds. |
| 236 | +Processing complete: |
| 237 | + {'id': 'e84c6bd4-bb02-4702-a55d-9b73e170c110:minimax/music-2.0', 'status': 'completed', 'audio_file': {'url': 'https://minimax-algeng-chat-tts-us.oss-us-east-1.aliyuncs.com/music%2Fprod%2Ftts-20260108074124-WOYPTzIhedvMMVNw.mp3?Expires=1767915690&OSSAccessKeyId=LTAI5tCpJNKCf5EkQHSuL9xg&Signature=PjSPH21%2FpILSs35%2Fl019p66wv1Y%3D'}, 'extra_info': {'music_duration': 107102, 'music_sample_rate': 44100, 'music_channel': 2, 'bitrate': 25600, 'music_size': 0}, 'trace_id': '05ae21d02744d305c8493d77c5b96a26'} |
| 238 | +``` |
| 239 | +{% endcode %} |
| 240 | + |
| 241 | +</details> |
| 242 | + |
| 243 | +Listen to the track we generated: |
| 244 | + |
| 245 | +{% embed url="https://drive.google.com/file/d/1nYJ3OCdcQ8_pR4vD_0yCZhnHDuS7LFdp/view" %} |
0 commit comments