MusicGen API: Generate Melodies with Replicate

If you’ve ever dreamt of effortlessly creating beautiful melodies or unique compositions, the MusicGen API powered by Replicate might just be the tool you’re looking for. With its seamless integration and powerful features, MusicGen allows you to generate music from a given prompt or melody.

In this tutorial, we’ll walk you through the process of getting started with the MusicGen API using the Replicate Node.js client.

MusicGen API Usage:


To begin using the MusicGen API, you’ll need to install the Replicate Node.js client. Open your terminal and run the following command:

npm install replicate


After installation, you’ll need to authenticate your API usage by setting your API token as an environment variable.

Copy your API token from your account settings and run the following command:

export REPLICATE_API_TOKEN=<paste-your-token-here>

Running the Model

Now that you’re authenticated, you can run the MusicGen model using the Replicate Node.js client. Here’s a basic example:

import Replicate from "replicate";

const replicate = new Replicate({
  auth: process.env.REPLICATE_API_TOKEN,

const output = await
    input: {
      model_version: "stereo-melody-large",
      prompt: "Describe the music you want to generate",
      duration: 8,


Webhook Integration

You can specify a webhook URL to be called when the prediction is complete. Refer to the webhook documentation for setup details. Here’s an example:

const prediction = await replicate.predictions.create({
  version: "b05b1dff1d8c6dc63d14b0cdb42135378dcb87f6373b0d3d341ede46e59e2b38",
  input: {
    model_version: "stereo-melody-large",
  webhook: "",
  webhook_events_filter: ["completed"]

Input Parameters

The MusicGen API supports various input parameters to customize your music generation:

model_version: Choose from different models like stereo-melody-large, stereo-large, melody-large, large, or encode-decode.

prompt: Provide a description of the music you want to generate.

input_audio: Influence the generated music with an audio file.

duration: Set the duration of the generated audio in seconds.

continuation: Determine if the generated music will continue from the input audio or mimic its melody.

continuation_start and continuation_end: Specify the start and end times for audio file continuation.

multi_band_diffusion: Enable MultiBand Diffusion for non-stereo models.

normalization_strategy: Choose a strategy for normalizing audio (loudness, clip, peak, rms).

top_k, top_p, and temperature: Control sampling behavior for token selection.

classifier_free_guidance: Influence the output based on inputs.

output_format: Select the output format for generated audio (wav or mp3).

seed: Provide seed for the random number generator.

Output Schema

The output of the MusicGen API is a URL pointing to the generated audio file. The raw JSON schema describing the model’s output structure is as follows:

  "type": "string",
  "title": "Output",
  "format": "uri"

Now that you have a basic understanding of the MusicGen API and how to use it with the Replicate Node.js client, feel free to explore the possibilities of generating unique and captivating melodies.

For more details and advanced options, refer to the Replicate Node.js library documentation.