# Replace Section

> Replace specific sections of generated music tracks with precision timing control. Modify choruses, verses, or any segment by specifying start and end times. Maintains overall coherence while allowing targeted changes to lyrics, style, or musical elements.

## Overview

- Submit: `https://api.poyo.ai/api/generate/submit`
- Status: `https://api.poyo.ai/api/generate/status/{task_id}`
- Page: https://poyo.ai/models/replace-section
- Model page ID: `replace-section`
- API model ID: `replace-section`
- Category: Music
- Auth: `Authorization: Bearer $POYO_API_KEY`
- Create a key at https://poyo.ai/dashboard/api-key

This model uses PoYo's unified async API. POST the JSON body below, then poll status with the returned `task_id` or receive a webhook on `callback_url`. Failed tasks are not charged.

## Pricing

- Standard generation: 10 credits ($0.050) per generation

For more details, see https://poyo.ai/models/replace-section.

## API Information

The request envelope is always `{ model, input, callback_url? }`. Input fields below are nested under `input`.

### Request envelope

- `model` (`ModelEnum`, required):
  API model identifier.
  
  Must be `replace-section` for this endpoint.
  - Options: `"replace-section"`
  - Examples: "replace-section"
- `callback_url` (`string`, optional):
  Webhook callback URL for result notifications
  - Examples: "https://your-domain.com/callback"

### Input Schema

Input parameters for section replacement

The API accepts the following `input` parameters:

- `task_id` (`string`, optional):
  Original task ID (parent task).
  
  Used to identify the source music for section replacement.
  - Examples: "5c79xxxxbe8e"
- `audio_id` (`string`, optional):
  Unique identifier of the audio track to replace.
  
  This ID is returned in the callback data after music generation completes.
  - Examples: "e231xxxx-xxxx-xxxx-xxxx-xxxx8cadc7dc"
- `upload_url` (`string`, optional):
  HTTP(S) uploaded source; mutually exclusive with task_id/audio_id.
- `mv` (`MvEnum`, optional):
  Required with upload_url.
  - Options: `"V4"`, `"V4_5"`, `"V4_5ALL"`, `"V4_5PLUS"`, `"V5"`, `"V5_5"`, `"V6"`, `"V6_MINI"`, `"V6_WILD"`
- `prompt` (`string`, optional):
  Text describing the replacement audio content.
  
  Describe the lyrics, melody, or musical content you want in the replaced section.
  - Examples: "A powerful chorus with soaring vocals"
- `tags` (`string`, required):
  Music style tags.
  
  Specify genres, moods, or styles for the replacement section (e.g., jazz, electronic, upbeat).
  - Examples: "pop, upbeat"
- `title` (`string`, required):
  Music title.
  
  The title for the resulting track.
  - Examples: "Summer Dreams"
- `infill_start_s` (`float`, required):
  Start time in seconds. The replacement interval must be at least 10 seconds and no more than 50% of the source duration. Supports up to 2 decimal places.
  - Range: `0` to `+∞`
  - Examples: `30`
- `infill_end_s` (`float`, required):
  End time in seconds. Must be greater than `infill_start_s`; the interval must be at least 10 seconds and no more than 50% of the source duration. Supports up to 2 decimal places.
  - Examples: `60`
- `negative_tags` (`string`, optional):
  Styles to exclude from replacement.
  
  Specify genres or characteristics to avoid in the generated section.
  - Examples: "rock, heavy metal"
- `full_lyrics` (`string`, required):
  Complete lyrics after modification.
  
  Combining both modified and unmodified lyrics for the entire track.
  - Examples: "Complete song lyrics including all sections"

Required Parameters Example:

```json
{
  "model": "replace-section",
  "input": {
    "tags": "pop, upbeat",
    "title": "Summer Dreams",
    "infill_start_s": 30,
    "infill_end_s": 60,
    "full_lyrics": "Complete song lyrics including the replacement chorus"
  }
}
```

Full Example:

```json
{
  "model": "replace-section",
  "input": {
    "task_id": "5c79xxxxbe8e",
    "audio_id": "e231xxxx-xxxx-xxxx-xxxx-xxxx8cadc7dc",
    "prompt": "A powerful chorus with soaring vocals",
    "tags": "pop, upbeat",
    "title": "Summer Dreams",
    "infill_start_s": 30,
    "infill_end_s": 60,
    "full_lyrics": "Complete song lyrics including the replacement chorus",
    "negative_tags": "rock, heavy metal"
  }
}
```

Replace Section with Full Lyrics:

```json
{
  "model": "replace-section",
  "input": {
    "task_id": "5c79xxxxbe8e",
    "audio_id": "e231xxxx-xxxx-xxxx-xxxx-xxxx8cadc7dc",
    "prompt": "New verse lyrics here",
    "tags": "jazz, smooth",
    "title": "Midnight Blues",
    "infill_start_s": 15.5,
    "infill_end_s": 45,
    "negative_tags": "rock, heavy",
    "full_lyrics": "Complete song lyrics including modified and unmodified sections"
  }
}
```

### Output Schema

Submit returns a `task_id`. Poll `GET /api/generate/status/{task_id}` until `status` is `finished` or `failed`. Completed media is in `data.files`.

Response fields:

- `code` (`CodeEnum`, required):
  Business status code. `200` means success; any other value means the request failed. Always check this field: some validation errors return HTTP 200 with `code: 400` in the body.
  
  - `200`: Success
  - `400`: Invalid request parameters
  - `401`: Missing or invalid API key
  - `402`: Insufficient credits
  - `403`: Permission denied
  - `404`: Resource not found
  - `408`: Request timeout
  - `429`: Rate limit exceeded
  - `500`: Internal server error
  - `502`: Upstream service error
  - `503`: Service unavailable
  
  See [Error Codes](/api-manual/error-codes) for details.
  - Options: `200`, `400`, `401`, `402`, `403`, `404`, `408`, `429`, `500`, `502`, `503`
  - Examples: `200`
- `data` (`object`, required):

Example Response:

```json
{
  "code": 200,
  "data": {
    "task_id": "task_example",
    "status": "finished",
    "files": [
      {
        "file_url": "",
        "file_type": "file"
      }
    ]
  }
}
```

## Usage Examples

### cURL

```bash
curl --request POST \
  --url https://api.poyo.ai/api/generate/submit \
  --header "Authorization: Bearer $POYO_API_KEY" \
  --header "Content-Type: application/json" \
  --data '{"model":"replace-section","input":{"task_id":"5c79xxxxbe8e","audio_id":"e231xxxx-xxxx-xxxx-xxxx-xxxx8cadc7dc","prompt":"A powerful chorus with soaring vocals","tags":"pop, upbeat","title":"Summer Dreams","infill_start_s":30,"infill_end_s":60,"full_lyrics":"Complete song lyrics including the replacement chorus"}}'
```

### Python

```python
import json
import os
import time
import requests

API = "https://api.poyo.ai"
headers = {
    "Authorization": f"Bearer {os.environ['POYO_API_KEY']}",
    "Content-Type": "application/json",
}
payload = json.loads("{\n  \"model\": \"replace-section\",\n  \"input\": {\n    \"task_id\": \"5c79xxxxbe8e\",\n    \"audio_id\": \"e231xxxx-xxxx-xxxx-xxxx-xxxx8cadc7dc\",\n    \"prompt\": \"A powerful chorus with soaring vocals\",\n    \"tags\": \"pop, upbeat\",\n    \"title\": \"Summer Dreams\",\n    \"infill_start_s\": 30,\n    \"infill_end_s\": 60,\n    \"full_lyrics\": \"Complete song lyrics including the replacement chorus\"\n  }\n}")

submit = requests.post(f"{API}/api/generate/submit", headers=headers, json=payload)
submit.raise_for_status()
task_id = submit.json()["data"]["task_id"]

while True:
    status = requests.get(f"{API}/api/generate/status/{task_id}", headers=headers)
    status.raise_for_status()
    data = status.json()["data"]
    if data["status"] in ("finished", "failed"):
        print(data)
        break
    time.sleep(2)
```

### JavaScript

```javascript
const headers = {
  Authorization: `Bearer ${process.env.POYO_API_KEY}`,
  "Content-Type": "application/json",
};

const submit = await fetch("https://api.poyo.ai/api/generate/submit", {
  method: "POST",
  headers,
  body: JSON.stringify({
  "model": "replace-section",
  "input": {
    "task_id": "5c79xxxxbe8e",
    "audio_id": "e231xxxx-xxxx-xxxx-xxxx-xxxx8cadc7dc",
    "prompt": "A powerful chorus with soaring vocals",
    "tags": "pop, upbeat",
    "title": "Summer Dreams",
    "infill_start_s": 30,
    "infill_end_s": 60,
    "full_lyrics": "Complete song lyrics including the replacement chorus"
  }
}),
});

const submitted = await submit.json();
const taskId = submitted.data.task_id;

while (true) {
  const statusResponse = await fetch(`https://api.poyo.ai/api/generate/status/${taskId}`, { headers });
  const status = await statusResponse.json();
  if (["finished", "failed"].includes(status.data.status)) {
    console.log(status.data);
    break;
  }
  await new Promise((resolve) => setTimeout(resolve, 2000));
}
```

## Additional Resources

### Documentation

- [Model Playground](https://poyo.ai/models/replace-section)
- [API Documentation](https://docs.poyo.ai/api-manual/music-series/replace-section)
- [API Documentation (Markdown)](https://docs.poyo.ai/api-manual/music-series/replace-section.md)
- [Task status](https://docs.poyo.ai/api-manual/task-management/query-task-status)
- [Webhooks](https://docs.poyo.ai/api-manual/task-management/webhooks)

### Other agent-readable surfaces

- [Platform overview](https://poyo.ai/llms.txt)
- [Documentation index](https://docs.poyo.ai/llms.txt)
- [Full documentation text](https://docs.poyo.ai/llms-full.txt)
- Any other model: `https://poyo.ai/models/<model-id>/llms.txt`
