├── README.md ├── image.png ├── index.js ├── stable_audio.js └── token.json /README.md: -------------------------------------------------------------------------------- 1 | # Stable Audio 2 API 2 | 3 | Access Stable Audio 2 for absolutely free using this reverse engineered API! 4 | 5 | ## How to use? 6 | 7 | In order to use this API, you need to first make sure you have Node.JS installed on your machine. Alternatively, you may use an online IDE like [Repl.it](https://replit.com) to run the code. 8 | 9 | Then, follow the steps below: 10 | 11 | 1. Obtaining your Stable Audio token. Scroll down to view how 12 | 2. Once you have your token, set it in the token.json file accordingly. 13 | 3. The `stable_audio.js` file contains the core functionality in the form of a function which has been exported. You may access it like this: 14 | 15 | ```js 16 | const { generateAudio } = require('./stable_audio'); 17 | 18 | const main = async () => { 19 | await generateAudio('electronic dance music', 180, 123); // Prompt (required) | Length (optional) | Seed (optional) 20 | }; 21 | 22 | main(); 23 | ``` 24 | 25 | 4. Run the file using `node index.js`. 26 | 4. There you have it! Once you run the file, it will download to audio_file.mp3 in the same directory for you to listen to. You may adjust the function if you would like to download it in another location. 27 | 28 | ## How to get my Stable Audio Token? 29 | 30 | 1. Visit https://stableaudio.com/generate and register for an account. 31 | 2. Open up your browser developer tools (Chrome - `Ctrl` + `Shift` + `I`) 32 | 3. Navigate to the "Application" tab. 33 | 4. Under `Local Storage` -> `https://stableaudio.com` there should be something that looks like this: `@@auth0spajs@@::XxXxX12345::@@user@@`. 34 | 5. Copy the value under `id_token`. 35 | 6. Then set it in the token.json file accordingly. 36 | 37 | ![alt text](image.png) -------------------------------------------------------------------------------- /image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skzhengkai/stable-audio-api/d2925c2c9347092a905aa84062a796327eb13af4/image.png -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const { generateAudio } = require('./stable_audio'); 2 | 3 | const main = async () => { 4 | await generateAudio('electronic dance music', 180, 123); 5 | }; 6 | 7 | main(); -------------------------------------------------------------------------------- /stable_audio.js: -------------------------------------------------------------------------------- 1 | const axios = require('axios'); 2 | const fs = require('fs'); 3 | const path = require('path'); 4 | 5 | const downloadFile = async (url, filePath) => { 6 | try { 7 | // Read the token from the token.json file 8 | const tokenData = JSON.parse(fs.readFileSync(path.join(__dirname, 'token.json'), 'utf8')); 9 | const token = tokenData.token; 10 | let response; 11 | let retryCount = 0; 12 | const maxRetries = 50; 13 | const retryDelay = 3000; // 3 seconds 14 | 15 | while (retryCount < maxRetries) { 16 | response = await axios({ 17 | url: url, 18 | method: 'GET', 19 | headers: { 20 | 'Authorization': `Bearer ${token}`, 21 | 'Accept': '*/*', 22 | 'Accept-Encoding': 'gzip, deflate, br, zstd', 23 | 'Accept-Language': 'en-US,en;q=0.9' 24 | }, 25 | responseType: 'stream' 26 | }); 27 | 28 | if (response.status === 200) { 29 | const writer = fs.createWriteStream(filePath); 30 | 31 | response.data.pipe(writer); 32 | 33 | await new Promise((resolve, reject) => { 34 | writer.on('finish', resolve); 35 | writer.on('error', reject); 36 | }); 37 | 38 | console.log('File downloaded successfully!'); 39 | return; 40 | } else if (response.status === 202) { 41 | console.log('Generation in progress, retrying in 3 seconds...'); 42 | await new Promise(resolve => setTimeout(resolve, retryDelay)); 43 | retryCount++; 44 | } else { 45 | console.error('Error downloading file:', response.status); 46 | return; 47 | } 48 | } 49 | 50 | console.error('Maximum number of retries reached, unable to download file.'); 51 | } catch (error) { 52 | console.error('Error downloading file:', error); 53 | } 54 | }; 55 | 56 | const generateAudio = async (prompt, lengthSeconds = 178, seed = 123) => { 57 | try { 58 | const tokenData = JSON.parse(fs.readFileSync(path.join(__dirname, 'token.json'), 'utf8')); 59 | const token = tokenData.token; 60 | const options = { 61 | method: 'POST', 62 | url: 'https://api.stableaudio.com/v1alpha/generations/stable-audio-audiosparx-v2-0/text-to-music', 63 | headers: { 64 | 'Content-Type': 'application/json', 65 | 'Authorization': `Bearer ${token}` 66 | }, 67 | data: { 68 | "data": { 69 | "type": "generations", 70 | "attributes": { 71 | "prompts": [ 72 | { 73 | "text": prompt, 74 | "weight": 1 75 | } 76 | ], 77 | "length_seconds": lengthSeconds, 78 | "seed": seed 79 | } 80 | } 81 | } 82 | }; 83 | 84 | const response = await axios(options); 85 | console.log(`Status Code: ${response.status}`); 86 | 87 | // Extract the result URL from the response 88 | const resultUrl = response.data.data[0].links.result; 89 | 90 | // Download the result file 91 | const filePath = path.join(__dirname, 'audio_file.mp3'); 92 | await downloadFile(resultUrl, filePath); 93 | } catch (error) { 94 | console.error(`Error: ${error}`); 95 | } 96 | }; 97 | 98 | module.exports = { generateAudio }; -------------------------------------------------------------------------------- /token.json: -------------------------------------------------------------------------------- 1 | { 2 | "token": "eyJ..." 3 | } --------------------------------------------------------------------------------