Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions music/typescript/quickstart/PROMPT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 4 additions & 2 deletions music/typescript/quickstart/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand Down
6 changes: 4 additions & 2 deletions music/typescript/quickstart/example/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand Down
58 changes: 34 additions & 24 deletions music/typescript/quickstart/example/index.ts
Original file line number Diff line number Diff line change
@@ -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}`);
1 change: 0 additions & 1 deletion music/typescript/quickstart/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"start": "tsx index.ts"
},
"dependencies": {
"@elevenlabs/elevenlabs-js": "latest",
"dotenv": "latest"
},
"devDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions music/typescript/quickstart/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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');
"
Comment thread
tadaspetra marked this conversation as resolved.

# Setup env
if [ -f "$DIR/.env" ]; then
cp "$DIR/.env" example/.env
Expand Down
Loading