feat: add code example
This commit is contained in:
		
							parent
							
								
									cd939db5c9
								
							
						
					
					
						commit
						1cb4e921db
					
				
							
								
								
									
										115
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										115
									
								
								README.md
									
									
									
									
									
								
							@ -113,6 +113,121 @@ Suno API currently mainly implements the following APIs:
 | 
			
		||||
For more detailed documentation, please check out the demo site:
 | 
			
		||||
[suno.gcui.art/docs](https://suno.gcui.art/docs)
 | 
			
		||||
 | 
			
		||||
## Code example
 | 
			
		||||
 | 
			
		||||
### Python
 | 
			
		||||
 | 
			
		||||
```python
 | 
			
		||||
import time
 | 
			
		||||
import requests
 | 
			
		||||
 | 
			
		||||
# replace your vercel domain
 | 
			
		||||
base_url = 'http://localhost:3000'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def custom_generate_audio(payload):
 | 
			
		||||
    url = f"{base_url}/api/custom_generate"
 | 
			
		||||
    response = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})
 | 
			
		||||
    return response.json()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def generate_audio_by_prompt(payload):
 | 
			
		||||
    url = f"{base_url}/api/generate"
 | 
			
		||||
    response = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})
 | 
			
		||||
    return response.json()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_audio_information(audio_ids):
 | 
			
		||||
    url = f"{base_url}/api/get?ids={audio_ids}"
 | 
			
		||||
    response = requests.get(url)
 | 
			
		||||
    return response.json()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_quota_information():
 | 
			
		||||
    url = f"{base_url}/api/get_limit"
 | 
			
		||||
    response = requests.get(url)
 | 
			
		||||
    return response.json()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    data = generate_audio_by_prompt({
 | 
			
		||||
        "prompt": "A popular heavy metal song about war, sung by a deep-voiced male singer, slowly and melodiously. The lyrics depict the sorrow of people after the war.",
 | 
			
		||||
        "make_instrumental": False,
 | 
			
		||||
        "wait_audio": False
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    ids = f"{data[0]['id']},{data[1]['id']}"
 | 
			
		||||
    print(f"ids: {ids}")
 | 
			
		||||
 | 
			
		||||
    for _ in range(60):
 | 
			
		||||
        data = get_audio_information(ids)
 | 
			
		||||
        if data[0]["status"] == 'streaming':
 | 
			
		||||
            print(f"{data[0]['id']} ==> {data[0]['audio_url']}")
 | 
			
		||||
            print(f"{data[1]['id']} ==> {data[1]['audio_url']}")
 | 
			
		||||
            break
 | 
			
		||||
        # sleep 5s
 | 
			
		||||
        time.sleep(5)
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### Js
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
const axios = require('axios');
 | 
			
		||||
 | 
			
		||||
// replace your vercel domain
 | 
			
		||||
const baseUrl = 'http://localhost:3000';
 | 
			
		||||
 | 
			
		||||
async function customGenerateAudio(payload) {
 | 
			
		||||
    const url = `${baseUrl}/api/custom_generate`;
 | 
			
		||||
    const response = await axios.post(url, payload, { headers: { 'Content-Type': 'application/json' } });
 | 
			
		||||
    return response.data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function generateAudioByPrompt(payload) {
 | 
			
		||||
    const url = `${baseUrl}/api/generate`;
 | 
			
		||||
    const response = await axios.post(url, payload, { headers: { 'Content-Type': 'application/json' } });
 | 
			
		||||
    return response.data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function getAudioInformation(audioIds) {
 | 
			
		||||
    const url = `${baseUrl}/api/get?ids=${audioIds}`;
 | 
			
		||||
    const response = await axios.get(url);
 | 
			
		||||
    return response.data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function getQuotaInformation() {
 | 
			
		||||
    const url = `${baseUrl}/api/get_limit`;
 | 
			
		||||
    const response = await axios.get(url);
 | 
			
		||||
    return response.data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function main() {
 | 
			
		||||
    const data = await generateAudioByPrompt({
 | 
			
		||||
        prompt: "A popular heavy metal song about war, sung by a deep-voiced male singer, slowly and melodiously. The lyrics depict the sorrow of people after the war.",
 | 
			
		||||
        make_instrumental: false,
 | 
			
		||||
        wait_audio: false
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    const ids = `${data[0].id},${data[1].id}`;
 | 
			
		||||
    console.log(`ids: ${ids}`);
 | 
			
		||||
 | 
			
		||||
    for (let i = 0; i < 60; i++) {
 | 
			
		||||
        const data = await getAudioInformation(ids);
 | 
			
		||||
        if (data[0].status === 'streaming') {
 | 
			
		||||
            console.log(`${data[0].id} ==> ${data[0].audio_url}`);
 | 
			
		||||
            console.log(`${data[1].id} ==> ${data[1].audio_url}`);
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        // sleep 5s
 | 
			
		||||
        await new Promise(resolve => setTimeout(resolve, 5000));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
main();
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Integration with Custom Agents
 | 
			
		||||
 | 
			
		||||
You can integrate Suno AI as a tool/plugin/action into your AI agent.
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										116
									
								
								README_CN.md
									
									
									
									
									
								
							
							
						
						
									
										116
									
								
								README_CN.md
									
									
									
									
									
								
							@ -110,6 +110,122 @@ Suno API 目前主要实现了以下 API:
 | 
			
		||||
详细文档请查看演示站点:
 | 
			
		||||
[suno.gcui.art/docs](https://suno.gcui.art/docs)
 | 
			
		||||
 | 
			
		||||
## 代码示例
 | 
			
		||||
 | 
			
		||||
### Python
 | 
			
		||||
 | 
			
		||||
```python
 | 
			
		||||
import time
 | 
			
		||||
import requests
 | 
			
		||||
 | 
			
		||||
# replace your vercel domain
 | 
			
		||||
base_url = 'http://localhost:3000'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def custom_generate_audio(payload):
 | 
			
		||||
    url = f"{base_url}/api/custom_generate"
 | 
			
		||||
    response = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})
 | 
			
		||||
    return response.json()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def generate_audio_by_prompt(payload):
 | 
			
		||||
    url = f"{base_url}/api/generate"
 | 
			
		||||
    response = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})
 | 
			
		||||
    return response.json()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_audio_information(audio_ids):
 | 
			
		||||
    url = f"{base_url}/api/get?ids={audio_ids}"
 | 
			
		||||
    response = requests.get(url)
 | 
			
		||||
    return response.json()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_quota_information():
 | 
			
		||||
    url = f"{base_url}/api/get_limit"
 | 
			
		||||
    response = requests.get(url)
 | 
			
		||||
    return response.json()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    data = generate_audio_by_prompt({
 | 
			
		||||
        "prompt": "A popular heavy metal song about war, sung by a deep-voiced male singer, slowly and melodiously. The lyrics depict the sorrow of people after the war.",
 | 
			
		||||
        "make_instrumental": False,
 | 
			
		||||
        "wait_audio": False
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    ids = f"{data[0]['id']},{data[1]['id']}"
 | 
			
		||||
    print(f"ids: {ids}")
 | 
			
		||||
 | 
			
		||||
    for _ in range(60):
 | 
			
		||||
        data = get_audio_information(ids)
 | 
			
		||||
        if data[0]["status"] == 'streaming':
 | 
			
		||||
            print(f"{data[0]['id']} ==> {data[0]['audio_url']}")
 | 
			
		||||
            print(f"{data[1]['id']} ==> {data[1]['audio_url']}")
 | 
			
		||||
            break
 | 
			
		||||
        # sleep 5s
 | 
			
		||||
        time.sleep(5)
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### Js
 | 
			
		||||
 | 
			
		||||
```js
 | 
			
		||||
const axios = require('axios');
 | 
			
		||||
 | 
			
		||||
// replace your vercel domain
 | 
			
		||||
const baseUrl = 'http://localhost:3000';
 | 
			
		||||
 | 
			
		||||
async function customGenerateAudio(payload) {
 | 
			
		||||
    const url = `${baseUrl}/api/custom_generate`;
 | 
			
		||||
    const response = await axios.post(url, payload, { headers: { 'Content-Type': 'application/json' } });
 | 
			
		||||
    return response.data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function generateAudioByPrompt(payload) {
 | 
			
		||||
    const url = `${baseUrl}/api/generate`;
 | 
			
		||||
    const response = await axios.post(url, payload, { headers: { 'Content-Type': 'application/json' } });
 | 
			
		||||
    return response.data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function getAudioInformation(audioIds) {
 | 
			
		||||
    const url = `${baseUrl}/api/get?ids=${audioIds}`;
 | 
			
		||||
    const response = await axios.get(url);
 | 
			
		||||
    return response.data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function getQuotaInformation() {
 | 
			
		||||
    const url = `${baseUrl}/api/get_limit`;
 | 
			
		||||
    const response = await axios.get(url);
 | 
			
		||||
    return response.data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function main() {
 | 
			
		||||
    const data = await generateAudioByPrompt({
 | 
			
		||||
        prompt: "A popular heavy metal song about war, sung by a deep-voiced male singer, slowly and melodiously. The lyrics depict the sorrow of people after the war.",
 | 
			
		||||
        make_instrumental: false,
 | 
			
		||||
        wait_audio: false
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    const ids = `${data[0].id},${data[1].id}`;
 | 
			
		||||
    console.log(`ids: ${ids}`);
 | 
			
		||||
 | 
			
		||||
    for (let i = 0; i < 60; i++) {
 | 
			
		||||
        const data = await getAudioInformation(ids);
 | 
			
		||||
        if (data[0].status === 'streaming') {
 | 
			
		||||
            console.log(`${data[0].id} ==> ${data[0].audio_url}`);
 | 
			
		||||
            console.log(`${data[1].id} ==> ${data[1].audio_url}`);
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        // sleep 5s
 | 
			
		||||
        await new Promise(resolve => setTimeout(resolve, 5000));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
main();
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## 集成到到常见的自定义 Agent 中
 | 
			
		||||
 | 
			
		||||
你可以把 suno ai 当做一个 工具/插件/Action 集成到你的 AI Agent 中。
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user