Python integration guide
Python Music API
Python developers who need on-demand music generation in a backend, script, or pipeline without running an audio model.
The approach
MusicAPI is a plain JSON REST API, so any HTTP client works. This guide uses requests, the de facto standard. You submit a generation to one endpoint, then poll a task endpoint until the finished audio is returned. No SDK is required.
Install
pip install requestsHow it works
- 1
Install requests, or use any HTTP client you already have.
- 2
Create an API key in the MusicAPI dashboard and read it from the MUSICAPI_KEY environment variable.
- 3
POST to /sonic/create with a Bearer header and a JSON body to start a job, then read task_id from the response.
- 4
GET /sonic/task/{task_id} on an interval. A 202 status or a body with type not_ready means keep polling.
- 5
Once the terminal response arrives, read each song's title and audio_url from the data array.
Full working example
Copy this, set the MUSICAPI_KEY environment variable, and run it. It is a real request against the live API.
import os
import time
import requests
API = "https://api.musicapi.ai/api/v1"
KEY = os.environ["MUSICAPI_KEY"]
HEADERS = {"Authorization": f"Bearer {KEY}", "Content-Type": "application/json"}
# 1. Submit a generation job.
resp = requests.post(
f"{API}/sonic/create",
headers=HEADERS,
json={
"custom_mode": False,
"mv": "sonic-v5",
"gpt_description_prompt": "lofi hip hop for studying",
},
timeout=30,
)
resp.raise_for_status()
task_id = resp.json()["task_id"]
# 2. Poll until the audio is ready.
while True:
poll = requests.get(f"{API}/sonic/task/{task_id}", headers=HEADERS, timeout=30)
if poll.status_code == 202:
time.sleep(5)
continue
poll.raise_for_status()
body = poll.json()
if body.get("type") == "not_ready":
time.sleep(5)
continue
for song in body.get("data", []):
print(song["title"], song["audio_url"])
breakPricing
MusicAPI is pay-as-you-go with credit packs, plus predictable monthly subscriptions. The per-credit rate is the same across packs and subscriptions. See the pricing page for current rates, free credits, and volume options.
FAQ
Is there an official Python SDK?
The REST API is stable and language-agnostic, and this guide is a complete working integration with requests. A typed Python SDK is on the roadmap. Until then, the REST flow shown here is the supported path.
How do I know when a generation is finished?
Poll GET /sonic/task/{task_id}. While the job runs you get an HTTP 202 or a body with type set to not_ready. When it finishes, the response carries a data array where each item has a streamable audio_url.
Can I avoid polling entirely?
Yes. Register a webhook so MusicAPI notifies your endpoint when a job completes, then skip the loop. Polling is shown here because it works without any public endpoint to receive callbacks.
Build it in 5 minutes
Get free credits on signup and run real generations before any payment. No credit card required to start.
API details verified 2026-05-18. The API surface evolves; the pricing page always has current rates.