Skip to content

Poll Task Status

Shows how to poll the status of a composition task until the track is composed and the download URLs for the full track and individual stems are available.

This example assumes using Beatoven; is in scope and apiKey contains your Beatoven API key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Compose a short track and poll until it finishes, then read the track + stem URLs
// from the response metadata.

using var client = new BeatovenClient(apiKey);

var composeResponse = await client.Tracks.ComposeTrackAsync(
    prompt: new ComposeTrackPrompt { Text = "15 seconds calm piano melody" });

using var timeoutCts = new CancellationTokenSource(TimeSpan.FromMinutes(5));
TaskStatusResponse? status = null;
while (!timeoutCts.IsCancellationRequested)
{
    status = await client.Tasks.GetTaskStatusAsync(
        taskId: composeResponse.TaskId,
        cancellationToken: timeoutCts.Token);

    if (status.Status is Beatoven.TaskStatus.Composed or Beatoven.TaskStatus.Failed)
    {
        break;
    }

    await Task.Delay(TimeSpan.FromSeconds(5), timeoutCts.Token);
}