Seedance 2.5 from ByteDance is officially available on Apiframe.

How to Build with an AI Music Generation API: A Step-by-Step Developer Guide

A step-by-step developer tutorial for integrating an AI music generation API into your app.

Renaud Last updated November 9, 2026 August 6, 2026 · 8 min read Intermediate
How to Build with an AI Music Generation API: A Step-by-Step Developer Guide

If you want generated soundtracks, background music, or full songs in an app without hiring a composer for every asset, an AI music generation API is the quickest way there. This guide covers what these APIs do, how to connect one to your app, and the mistakes that catch out most first-time builds.

What an AI Music Generation API Does

An AI music generation API lets your app reach models like Suno or Udio and produce full tracks, vocals, instrumentals, or both, from a written description. You describe what you want, meaning a genre, a mood, sometimes actual lyrics, and the model sends back an audio file.

It helps to separate this from text-to-speech or voice APIs. Those produce spoken audio: narration, voiceover, a cloned voice reading a script. A music API produces full songs with melody, instruments, and structure, not a voice reading text aloud. For how the technology works and how to pick between providers, see our AI music generation API guide.

One thing to know before you plan an integration: neither Suno nor Udio sells direct developer access. Both are reachable only through platforms that resell them, so the API key you end up with comes from one of those rather than from Suno or Udio themselves. Our comparison of Suno API providers covers the options.

What You Can Build

Background music generators

Apps and video editors can offer users tracks on demand, matched to a mood or scene, instead of licensing stock music libraries.

Custom soundtrack tools

Creators and marketers can generate a unique track for each piece of content rather than reusing the same handful of royalty-free songs everyone else uses.

Jingle and ad music generators

Small businesses without a budget for custom music can produce short branded jingles for ads or social content in minutes.

Music inside games and social apps

Games can give each player a theme tune, and social apps can let people score their own posts or clips. Worth planning around the timing here: generation takes a minute or two, so this works for music prepared ahead of time or requested by a user who expects to wait, not for a soundtrack that has to react to what is happening on screen.

If you are weighing which use cases and models suit your product, our guide on music API use cases and setup goes further into this.

What You'll Need

An API key from whichever platform you are using, which our getting started guide covers. A grasp of how these APIs work, meaning you send a request, get a job ID back, then check for the result later rather than waiting on the connection. And a sense of how prompt wording shapes output, since vague prompts give generic results.

Generating Your First Track

The example below uses Apiframe, which reaches Suno, Udio, and other music models through a single endpoint. The pattern underneath, authenticate, submit, check the result, download, is the same almost everywhere in this space.

Step 1: Check your key works

Every request carries an API key as a header. Calling the account endpoint first is a cheap way to confirm the key is valid before you spend credits on a real generation.

bash
curl -H "X-API-Key: afk_your_api_key_here" \
  https://api.apiframe.ai/v2/me

The response carries your account details and current credit balance.

Step 2: Write a usable prompt

Prompts work best when they cover genre, mood, tempo, and whether you want an instrumental or something with vocals. "Make a song" gives the model almost nothing. "Upbeat electronic track with synth arpeggios and driving bass, 120 bpm" gives it real direction. If you want vocals, decide upfront whether you are supplying the lyrics yourself or letting the model write them from a short description, since most providers treat those as two separate modes.

Step 3: Send the request

Everything goes to https://api.apiframe.ai/v2. For music, send a POST to /v2/music/generate with a prompt, a model, and that model's own settings.

bash
curl -X POST https://api.apiframe.ai/v2/music/generate \
  -H "X-API-Key: afk_your_api_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "upbeat electronic track with synth arpeggios and driving bass",
    "model": "suno",
    "sunoParams": {
      "model_version": "V4_5PLUS",
      "style": "electronic, synthwave"
    }
  }'

The reply arrives straight away with a job ID, before the track exists.

json
{
  "jobId": "c3d4e5f6-a7b8-9012-cdef-345678901234",
  "status": "QUEUED"
}

Step 4: Wait for it to finish

Check the jobs endpoint with your job ID to see where it is.

bash
curl -H "X-API-Key: afk_your_api_key_here" \
  https://api.apiframe.ai/v2/jobs/c3d4e5f6-a7b8-9012-cdef-345678901234

Better for anything running in production, add a webhookUrl and webhookEvents to the original request and the API will send the result to your server when the job finishes, with no checking loop to run. Our walkthrough on callbacks covers setting that up, including verifying the request really came from the API.

Step 5: Fetch and store the audio

Once status reads COMPLETED, the result carries an array of tracks. Suno and Udio both return two per request, and Mureka returns two by default and up to three, so you usually get more than one take to choose from.

json
{
  "id": "c3d4e5f6-a7b8-9012-cdef-345678901234",
  "status": "COMPLETED",
  "result": {
    "tracks": [
      {
        "id": "8b2f64d1-3c5e-4a7f-9d2b-6e1a0c4f8b3d",
        "audioUrl": "https://cdn2.apiframe.ai/audio/c3d4e5f6-a7b8-9012-cdef-345678901234-0.mp3",
        "imageUrl": "https://cdn2.apiframe.ai/audio/c3d4e5f6-a7b8-9012-cdef-345678901234-0.jpeg",
        "title": "Neon Skyline",
        "tags": "synthwave, electronic, upbeat",
        "duration": 184.2
      },
      {
        "id": "f4a1c9e7-6b2d-4e8a-a3f5-0d7c2b9e1a64",
        "audioUrl": "https://cdn2.apiframe.ai/audio/c3d4e5f6-a7b8-9012-cdef-345678901234-1.mp3",
        "imageUrl": "https://cdn2.apiframe.ai/audio/c3d4e5f6-a7b8-9012-cdef-345678901234-1.jpeg",
        "title": "Neon Skyline",
        "tags": "synthwave, electronic, upbeat",
        "duration": 178.6
      }
    ]
  },
  "creditCost": 11
}

Each track comes with a link to the hosted audio file. Download anything you plan to keep, since these links do not last forever. On Apiframe the window is 90 days. Note the creditCost of 11 covers the whole request, both tracks included, not 11 per track.

Step 6: Handle errors and retries

The status codes worth handling:

  • 400 means something is wrong with the request, often a prompt too long for the mode you are using.
  • 401 means the API key is missing or wrong.
  • 402 means you are out of credits. Surface this to the user rather than retrying.
  • 429 means you are sending too many requests and should wait before trying again.
  • 503 means the service is briefly unavailable.

For 429 and 503, retry and wait a bit longer between each attempt. Treat 400 and 401 as bugs in your own request to fix rather than errors to retry, since sending the same broken request again will fail the same way.

Timeouts need different handling from the codes above. If a request times out, the reply never reached you, which is not the same as the job never starting. Send it again blindly and you may run a second generation and pay for both. Include an idempotency key so the platform can recognize a repeat of the same request. Genuine failures, where the job starts and then fails, are refunded on Apiframe, so you are not charged for those.

Prompt Tips

Combine genre and mood. "Sad piano ballad" gives the model more than "sad music" or "piano music" on their own.

Mark the sections if you are writing lyrics. Labels like [Verse 1], [Chorus], and [Bridge] help the model build a song with shape instead of one continuous block.

Say what you do not want. Udio takes a negative tags field for exactly this, which helps when output keeps drifting toward a genre you did not ask for. Check whether your chosen model supports it, since not all of them do.

Iterate rather than hunting for one perfect prompt. Generate a few variations with small changes. Since Suno returns two tracks per request, you already have two takes to compare before you spend anything more. If you are choosing between Suno versions, our Suno version comparison covers what changed across releases, and all versions cost the same.

Pricing and Limits

Music generation is usually billed from a prepaid credit balance, with each generation taking a set number of credits depending on the model. Two shapes exist and the difference matters. Suno charges a flat 11 credits per generation whatever the track length, with stem splitting at 20. Udio charges by length instead, from 9 credits for 32 seconds up to 35 for its maximum of 130 seconds. Credits are a cent each, so a Suno generation is $0.11 for two tracks. For how providers compare, see our AI music API pricing guide.

When you estimate monthly spend, multiply your expected number of generations by the per-generation cost, not by the number of tracks you want. Two tracks come back from one charge, so counting per track roughly doubles your estimate. On Udio, work from the length you actually plan to request rather than the base rate. Then add headroom, since usage climbs once a feature ships. Separately, check the provider's cap on requests per minute, though for most music work the limit you feel first is how many jobs run at once.

Common Pitfalls

Licensing and commercial use. Before shipping generated music in a paid product, confirm the terms for the model you are using. Rights vary by provider and sometimes by plan, and since you are reaching Suno or Udio through a reseller, it is that platform's terms that govern what you can do. Our guide on who owns AI-generated work covers the wider picture.

Audio format. Most providers hand back MP3 at a fixed quality. If your app needs WAV or a particular sample rate for further processing, convert it yourself after downloading.

Expect a wait. Generation is not instant. A full track takes anywhere from thirty seconds to a couple of minutes depending on model and length, so build your interface around a visible waiting state rather than assuming the track exists the moment the request returns.

To see how providers compare on cost, speed, and licensing before picking one, our comparison of the best AI music generation APIs covers that ground, and our Mureka API guide is worth a look if you need tracks longer than Udio's limit.

FAQ

Can I use AI-generated music commercially?

It depends on the model and the platform's terms, which vary and change. Check the current licensing for your specific model and plan before putting output in a commercial product.

What's the difference between Suno and Udio?

Both produce full tracks with vocals or instrumentals from a prompt, but they come from different companies and can give noticeably different results from the same prompt. Suno has more follow-up actions, including extending a track, making a cover, and splitting out stems, and its flat rate makes it cheaper for long tracks. Udio is often preferred for vocal quality and is cheaper for short clips, since it bills by length. Our Suno API guide and Udio API guide cover setup and pricing for each.

Is there a free tier?

Most platforms give a small credit grant for testing. Apiframe gives new accounts 100 credits, which covers about nine Suno generations, so eighteen tracks. Enough to judge quality, not enough to run a feature. Ongoing generation at any real volume is paid.

How long does a generation take?

Usually between thirty seconds and a couple of minutes, depending on track length and how busy the model is. Fast enough for a user to wait on a loading state, too slow for anything that has to respond in the moment.

Which model should I start with?

Suno, for most people. The flat rate makes costs easy to predict, it has the most follow-up actions if you later want to extend or remix a track, and it returns two takes per request. Try Udio alongside it on the same prompt if vocals are central to what you are building.

Power your next AI product with Apiframe.

Instant access to 70+ media models through a single API. Start free and scale when you're ready.