AI music generator API key: a Python script that polls and downloads

Use your SUME_API_KEY to generate a track with the Music Router, poll the job and read the audio URL. A 28-line Python script with the $0.125 price.

5 min readSume
All posts

"AI music generator API key" is on Google's autocomplete today (read 2026-10-07). On Sume there is one key for every product. Create it in the workspace settings, export it as SUME_API_KEY, and send it as a Bearer token. The music call is asynchronous: it returns a job, and you poll until result_ready is true.

The script below uses requests, an Idempotency-Key so a retry cannot bill twice, and a main() function. It makes one real call at $0.125 (read 2026-10-07), so it needs a funded key to run.

The script

import os, time, uuid, requests

BASE = "https://api.sume.com"
H = {"Authorization": "Bearer " + os.environ["SUME_API_KEY"]}

def main():
    r = requests.post(
        BASE + "/v1/music-router/generate",
        headers={**H, "Idempotency-Key": str(uuid.uuid4())},
        json={"prompt": "Warm lo-fi, 80 BPM, A minor. Soft piano, vinyl crackle, "
                        "brushed drums. A calm 60-second loop. Instrumental, no vocals."},
        timeout=60,
    )
    r.raise_for_status()
    job_id = r.json()["job_id"]
    while True:
        s = requests.get(f"{BASE}/v1/jobs/{job_id}/status", headers=H, timeout=30).json()
        if s.get("result_ready"):
            break
        time.sleep(5)
    res = requests.get(f"{BASE}/v1/jobs/{job_id}/result", headers=H, timeout=30).json()
    for a in res["result"]["artifacts"]:
        if a["type"] == "audio":
            print(a["url"])

if __name__ == "__main__":
    main()

What to know before you run it

  • A missing SUME_API_KEY raises KeyError on the first line of main(), before any request is sent.
  • Reuse the same Idempotency-Key to retry one logical request; a fresh key is a new job and a new charge.
  • The only request controls are prompt, optional image_url and optional engine. There is no duration field.
  • Sume media inputs must be public HTTPS URLs on media.sume.com; import other files first with POST /v1/media-imports.
  • Stop looping on a failed job: check the status field and print the error instead of waiting forever.

Cost per run

Every generation is $0.125, so ten test prompts cost $1.25. The status and result calls are free. Add max_spend_usd while you iterate so a loop bug cannot spend more than you planned.

Sources

Related posts

More in Developers

All Developers posts

Written by Sume