Upload audio (Minimax Music)

API Reference

Examples

// npm install node-fetch form-data
import fs from "fs";
import path from "path";
import fetch from "node-fetch";
import FormData from "form-data";

const upload = async () => {
  const url = "https://api.aimlapi.com/v2/generate/audio/minimax/upload";
  const fileName = "filename.mp3";
  const filePath = path.resolve(fileName); // Path to your file

  const purpose = "song"; // Possible values: "song", "voice", "instrumental"

  const formData = new FormData();
  formData.append("purpose", purpose);
  formData.append("file", fs.createReadStream(filePath), {
    filename: fileName,
    contentType: "audio/mpeg",
  });

  const apiKey = "<YOUR_API_KEY>"; 

  try {
    const response = await fetch(url, {
      method: "POST",
      headers: {
        Authorization: `Bearer ${apiKey}`,
        ...formData.getHeaders(),
      },
      body: formData,
    });

    if (response.ok) {
      const data = await response.json();
      console.log("Upload successful:", data);
    } else {
      console.error("Upload failed:", response.status, await response.text());
    }
  } catch (error) {
    console.error("Error during upload:", error.message);
  }
}

upload();
// npm install node-fetch form-data
import fs from "fs";
import path from "path";
import fetch from "node-fetch";
import FormData from "form-data";

const upload = async () => {
  const url = "https://api.aimlapi.com/v2/generate/audio/minimax/upload";
  const audioUrl = "url_to_file.mp3";  // Replace with the actual URL of the file
  const fileName = "filename.mp3";  // The name you want to give to the uploaded file
  const purpose = "song"; // Possible values: "song", "voice", "instrumental"

  const apiKey = "<YOUR_API_KEY>";  // Replace with your actual API key

  try {
  // Download the file from the URL
  const response = await fetch(audioUrl);
  if (!response.ok) {
    throw new Error(`Failed to fetch file from URL: ${audioUrl}`);
  }
  
  const fileBuffer = await response.arrayBuffer(); // Get the file as a buffer

  // Upload the file to the server using FormData
  const formData = new FormData();
  formData.append("purpose", purpose);
  formData.append("file", Buffer.from(fileBuffer), {
    filename: fileName,
    contentType: "audio/mpeg",
  });

  const uploadResponse = await fetch(url, {
    method: "POST",
    headers: {
    Authorization: `Bearer ${apiKey}`,
    ...formData.getHeaders(),
    },
    body: formData,
  });

  if (uploadResponse.ok) {
    const data = await uploadResponse.json();
    console.log("Upload successful:", data);
  } else {
    console.error("Upload failed:", uploadResponse.status, await uploadResponse.text());
  }
  } catch (error) {
  console.error("Error during upload:", error);
  }
};

upload();

Last updated