diff --git a/music/typescript/quickstart/PROMPT.md b/music/typescript/quickstart/PROMPT.md index 67a2ee71..7e45791b 100644 --- a/music/typescript/quickstart/PROMPT.md +++ b/music/typescript/quickstart/PROMPT.md @@ -2,11 +2,12 @@ Before writing any code, invoke the `/music` skill to learn the correct ElevenLa ## `index.ts` -Create a minimal script that generates music from a text prompt using the ElevenLabs JS SDK. +Create a tutorial-friendly script that generates music from a text prompt using the live Eleven Music REST API. Do not use the ElevenLabs JS SDK for music v2 yet. - Load env vars from `.env`. - Read the music prompt from CLI args; fall back to `A chill lo-fi beat with jazzy piano chords`. -- Use `ElevenLabsClient` and call `client.music.compose` with `musicLengthMs: 10000`. -- Save the returned audio to `output.mp3` with `Readable.from(track)` and `pipeline`. +- Build a request body with `prompt`, `music_length_ms: 10_000`, and `model_id: "music_v2"`. +- Use `fetch` to POST the JSON body to `https://api.elevenlabs.io/v1/music`. +- Save the returned MP3 response body to `output.mp3`. - Print a success message with the output path. -- Handle errors with a readable message. +- Keep the code on the happy path; a simple `response.ok` check is enough. diff --git a/music/typescript/quickstart/README.md b/music/typescript/quickstart/README.md index 9d29460b..8ab5f29c 100644 --- a/music/typescript/quickstart/README.md +++ b/music/typescript/quickstart/README.md @@ -1,6 +1,8 @@ # ElevenLabs Music — Quickstart Example -Generate an MP3 track from a text prompt using the ElevenLabs JS SDK. +Generate an MP3 track from a text prompt using the Eleven Music REST API. + +This example calls the REST API directly with `model_id: "music_v2"` while SDK support for the v2 model catches up. ## Setup @@ -18,7 +20,7 @@ Generate an MP3 track from a text prompt using the ElevenLabs JS SDK. pnpm install ``` -The Music API is currently available to paid ElevenLabs users. +The Eleven Music API is available to paid ElevenLabs users. ## Run diff --git a/music/typescript/quickstart/example/README.md b/music/typescript/quickstart/example/README.md index 9d29460b..8ab5f29c 100644 --- a/music/typescript/quickstart/example/README.md +++ b/music/typescript/quickstart/example/README.md @@ -1,6 +1,8 @@ # ElevenLabs Music — Quickstart Example -Generate an MP3 track from a text prompt using the ElevenLabs JS SDK. +Generate an MP3 track from a text prompt using the Eleven Music REST API. + +This example calls the REST API directly with `model_id: "music_v2"` while SDK support for the v2 model catches up. ## Setup @@ -18,7 +20,7 @@ Generate an MP3 track from a text prompt using the ElevenLabs JS SDK. pnpm install ``` -The Music API is currently available to paid ElevenLabs users. +The Eleven Music API is available to paid ElevenLabs users. ## Run diff --git a/music/typescript/quickstart/example/index.ts b/music/typescript/quickstart/example/index.ts index 65d0cbf6..a3b19e2e 100644 --- a/music/typescript/quickstart/example/index.ts +++ b/music/typescript/quickstart/example/index.ts @@ -1,32 +1,42 @@ import "dotenv/config"; -import { ElevenLabsClient } from "@elevenlabs/elevenlabs-js"; -import { createWriteStream } from "node:fs"; +import { writeFile } from "node:fs/promises"; import path from "node:path"; -import { Readable } from "node:stream"; -import { pipeline } from "node:stream/promises"; const DEFAULT_PROMPT = "A chill lo-fi beat with jazzy piano chords"; +const MUSIC_API_URL = "https://api.elevenlabs.io/v1/music"; const OUTPUT_FILE = "output.mp3"; -async function main() { - const prompt = process.argv.slice(2).join(" ").trim() || DEFAULT_PROMPT; - const client = new ElevenLabsClient(); - - try { - const track = await client.music.compose({ - prompt, - musicLengthMs: 10_000, - }); - - const outputPath = path.resolve(process.cwd(), OUTPUT_FILE); - await pipeline(Readable.from(track), createWriteStream(outputPath)); - - console.log(`Wrote generated music to ${outputPath}`); - } catch (err) { - const message = err instanceof Error ? err.message : String(err); - console.error(`Music generation failed: ${message}`); - process.exitCode = 1; - } +const apiKey = process.env.ELEVENLABS_API_KEY; +if (!apiKey) { + throw new Error("Missing ELEVENLABS_API_KEY. Add it to .env before running."); } -void main(); +const prompt = process.argv.slice(2).join(" ").trim() || DEFAULT_PROMPT; + +const requestBody = { + prompt, + music_length_ms: 10_000, + model_id: "music_v2", +}; + +const response = await fetch(MUSIC_API_URL, { + method: "POST", + headers: { + "xi-api-key": apiKey, + "Content-Type": "application/json", + Accept: "audio/mpeg", + }, + body: JSON.stringify(requestBody), +}); + +if (!response.ok) { + throw new Error( + `Music generation failed: ${response.status} ${await response.text()}` + ); +} + +const outputPath = path.resolve(process.cwd(), OUTPUT_FILE); +const audio = Buffer.from(await response.arrayBuffer()); +await writeFile(outputPath, audio); + +console.log(`Wrote generated music to ${outputPath}`); diff --git a/music/typescript/quickstart/example/package.json b/music/typescript/quickstart/example/package.json index fc73e608..ac06c6aa 100644 --- a/music/typescript/quickstart/example/package.json +++ b/music/typescript/quickstart/example/package.json @@ -5,7 +5,6 @@ "start": "tsx index.ts" }, "dependencies": { - "@elevenlabs/elevenlabs-js": "latest", "dotenv": "latest" }, "devDependencies": { diff --git a/music/typescript/quickstart/setup.sh b/music/typescript/quickstart/setup.sh index 43b7e490..760e9ac2 100755 --- a/music/typescript/quickstart/setup.sh +++ b/music/typescript/quickstart/setup.sh @@ -21,6 +21,14 @@ rsync -a \ # Copy project-specific README cp README.md example/README.md +# Music v2 uses the live REST API directly until SDK support lands. +node -e " + const fs = require('fs'); + const pkg = JSON.parse(fs.readFileSync('example/package.json', 'utf8')); + delete pkg.dependencies['@elevenlabs/elevenlabs-js']; + fs.writeFileSync('example/package.json', JSON.stringify(pkg, null, 2) + '\n'); +" + # Setup env if [ -f "$DIR/.env" ]; then cp "$DIR/.env" example/.env