Learn how to create AI-generated songs from lyrics using the Weights API. This guide walks you through the process of transforming your lyrics into complete musical compositions.
The Weights API provides powerful song generation capabilities that can turn your lyrics into full musical tracks. You provide the lyrics and a style prompt, and the AI creates the melody, instrumentation, and arrangement to bring your words to life.
Before generating a song, you need to prepare your lyrics and a style prompt. The API accepts English lyrics up to 600 characters and style prompts up to 300 characters.
const songJob = await client.songs.create({ lyrics: "In the moonlight, shadows dance, Whispering secrets of romance, Every step we take together, Makes this moment last forever", prompt: "blues, melancholic, raw, lonely bar, heartbreak, acoustic guitar, soulful vocals",});console.log(`Song job created with ID: ${songJob.id}`);
Song generation is asynchronous and can take several minutes. Poll the status to track progress:
Copy
Ask AI
async function checkSongStatus(jobId) { const song = await client.songs.retrieve(jobId); if (!song) { console.log("Song not found or access denied"); return null; } console.log(`Status: ${song.status}`); console.log(`Queue Position: ${song.queuePosition || "N/A"}`); console.log(`Attempt: ${song.attempt}`); switch (song.status) { case "QUEUED": console.log("Your song is waiting in the queue..."); break; case "PROCESSING": console.log("Your song is being generated..."); break; case "SUCCEEDED": console.log("Song generated successfully!"); console.log("Download URL:", song.outputUrl); break; case "ERRORED": console.log("Song generation failed. Please try again."); break; } return song;}// Check status every 30 secondsconst song = await checkSongStatus(songJob.id);
Once the status is SUCCEEDED, your song is ready for download:
Copy
Ask AI
if (song.status === "SUCCEEDED") { console.log("Your song is ready!"); console.log("Download URL:", song.outputUrl); console.log("Original Lyrics:", song.lyrics); console.log("Style Prompt:", song.prompt); console.log( "Generation Time:", new Date(song.endTime) - new Date(song.startTime), "ms" ); // You can now download or stream the song // The URL will be available for a limited time}
Experiment with various style prompts for different musical genres:
Copy
Ask AI
// Generate a blues songconst bluesSong = await client.songs.create({ lyrics: "Walking through the city lights, Feeling free on summer nights", prompt: "blues, melancholic, raw, lonely bar, heartbreak, acoustic guitar, soulful vocals",});// Generate a pop songconst popSong = await client.songs.create({ lyrics: "Hold me close, don't let go, In your arms, I feel whole", prompt: "upbeat pop, electronic, dance, energetic, synthesizers, driving beat, catchy melody",});// Generate a rock songconst rockSong = await client.songs.create({ lyrics: "Electric dreams and neon lights, Rock and roll through endless nights", prompt: "rock, electric guitar, powerful drums, energetic, stadium anthem, distorted riffs",});// Generate a jazz songconst jazzSong = await client.songs.create({ lyrics: "Smooth as silk, the night unfolds, Stories that the moonlight told", prompt: "jazz, smooth, sophisticated, piano, saxophone, lounge, intimate atmosphere",});
Here’s a complete example that creates a song and waits for completion:
Copy
Ask AI
import Weights from "@weights-ai/sdk";const client = new Weights({ bearerToken: "your-api-key-here",});async function generateSong(lyrics, prompt) { try { // Validate lyrics length if (lyrics.length > 380) { console.log("Lyrics too long. Maximum 380 characters allowed."); return; } // Validate prompt length if (prompt.length > 590) { console.log("Prompt too long. Maximum 590 characters allowed."); return; } // Create the song generation job console.log("Creating song generation job..."); const job = await client.songs.create({ lyrics: lyrics, prompt: prompt, }); console.log(`Job created with ID: ${job.id}`); // Poll for completion let completed = false; while (!completed) { await new Promise((resolve) => setTimeout(resolve, 30000)); // Wait 30 seconds const status = await client.songs.retrieve(job.id); console.log(`Status: ${status.status}`); if (status.status === "SUCCEEDED") { console.log("Song generated successfully!"); console.log("Download URL:", status.outputUrl); console.log("Original Lyrics:", status.lyrics); console.log("Style Prompt:", status.prompt); completed = true; } else if (status.status === "ERRORED") { console.log("Song generation failed"); completed = true; } } } catch (error) { console.error("Error:", error.message); }}// Generate different types of songsgenerateSong( "In the moonlight, shadows dance, Whispering secrets of romance, Every step we take together, Makes this moment last forever", "blues, melancholic, raw, lonely bar, heartbreak, acoustic guitar, soulful vocals");generateSong( "Hold me close, don't let go, In your arms, I feel whole, Every heartbeat, every breath, You're my love, my life, my rest", "upbeat pop, electronic, dance, energetic, synthesizers, driving beat, catchy melody");