PHP integration guide
PHP Music API
PHP developers, including Laravel and plain PHP backends, who want to generate music through an API instead of hosting a model.
The approach
MusicAPI is a JSON REST API that works with any PHP HTTP client. This guide uses Guzzle, the standard choice and the one most PHP frameworks already ship. You submit a generation, then poll a task endpoint until the finished audio comes back.
Install
composer require guzzlehttp/guzzleHow it works
- 1
Install Guzzle with Composer, or use the HTTP client your framework provides.
- 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 decode task_id from the response.
- 4
GET sonic/task/{task_id} on an interval. An HTTP 202 or a body with type not_ready means the job is still running.
- 5
When the terminal response arrives, read title and audio_url from each entry in 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.
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$api = 'https://api.musicapi.ai/api/v1';
$key = getenv('MUSICAPI_KEY');
$http = new Client([
'base_uri' => $api . '/',
'headers' => [
'Authorization' => 'Bearer ' . $key,
'Content-Type' => 'application/json',
],
]);
// 1. Submit a generation job.
$res = $http->post('sonic/create', [
'json' => [
'custom_mode' => false,
'mv' => 'sonic-v5',
'gpt_description_prompt' => 'epic orchestral trailer music',
],
]);
$taskId = json_decode((string) $res->getBody(), true)['task_id'];
// 2. Poll until the audio is ready.
while (true) {
$poll = $http->get('sonic/task/' . $taskId, ['http_errors' => false]);
if ($poll->getStatusCode() === 202) {
sleep(5);
continue;
}
$body = json_decode((string) $poll->getBody(), true);
if (($body['type'] ?? null) === 'not_ready') {
sleep(5);
continue;
}
foreach ($body['data'] ?? [] as $song) {
echo $song['title'] . ' ' . $song['audio_url'] . PHP_EOL;
}
break;
}Pricing
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
Does this work with Laravel?
Yes. The example uses Guzzle directly, which Laravel already depends on. You can also use Laravel's HTTP client. The request shape and the poll loop are the same regardless of framework.
Why set http_errors to false when polling?
While a job is still running the task endpoint can answer with HTTP 202. Disabling Guzzle's exception on non-2xx for the poll request lets you treat 202 as keep waiting rather than as an error.
How do I avoid a long-running poll loop in a web request?
Run the poll in a queued job or a worker rather than inside a web request, or register a webhook so MusicAPI calls your endpoint when the song is ready. The poll loop shown here is the dependency-free baseline.
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.