> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xhuoapi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Fish Audios Generation API Integration Instructions

> Fish music generation 集成指南 - XHuoAPI

This article will introduce the integration instructions for the Fish Audios Generation API, which can clone your own voice by inputting prompt words.

## Application Process

To use the API, you need to first apply for the corresponding service on the [Fish Audios Generation API](https://api.xhuoapi.ai/documents/e681b715-54fd-4464-a59a-d7f14500e095) page. After entering the page, click the "Acquire" button, as shown in the image below:

![](https://cdn.xhuoapi.ai/q6ytrc.png)

If you are not logged in or registered, you will be automatically redirected to the login page inviting you to register and log in. After logging in or registering, you will be automatically returned to the current page.

Upon your first application, there will be a free quota provided, allowing you to use the API for free.

## Basic Usage

First, understand the basic usage method, which involves inputting the prompt word `prompt`, the cloning action `action`, the voice ID `voice_id`, and the model `model` to obtain the processed result. You first need to simply pass a field `action` with the value `generate`, and then we also need to input the model `model`, which currently mainly includes the `fish-tts` model, as detailed below:

<p>
  <img src="https://cdn.xhuoapi.ai/0jnfqb.png" width="500" className="m-auto" />
</p>

Here we can see that we have set the Request Headers, including:

* `accept`: the format of the response result you want to receive, here filled in as `application/json`, which means JSON format.
* `authorization`: the key to call the API, which can be directly selected after application.

Additionally, the Request Body is set, including:

* `model`: the model for cloning the voice, mainly the `fish-tts` model.
* `action`: the action for this voice cloning task.
* `prompt`: the prompt word to be cloned.
* `voice_id`: the voice ID for cloning.
* `callback_url`: the URL for receiving the callback result.

After selection, you can find that the corresponding code is also generated on the right side, as shown in the image below:

<p>
  <img src="https://cdn.xhuoapi.ai/8uyos2.png" width="500" className="m-auto" />
</p>

Click the "Try" button to test, as shown in the above image, and we get the following result:

```json theme={null}
{
  "success": true,
  "task_id": "5872ab00-3cf4-4040-a798-8510aaa16756",
  "trace_id": "5eda3694-448a-4b72-af33-2acb3851ffe1",
  "data": [
    {
      "audio_url": "https://platform.r2.fish.audio/task/8a72ff9840234006a9f74cb2fa04f978.mp3"
    }
  ]
}
```

The returned result contains multiple fields, described as follows:

* `success`, the status of the voice cloning task at this time.
  * `data`, the result of the voice cloning task
    * `audio_url`, the audio link result of the voice cloning task at this time.

We can see that we have obtained satisfactory voice information, and we only need to retrieve the cloned voice based on the music link address in the `data` result.

Additionally, if you want to generate the corresponding integration code, you can directly copy the generated code, for example, the CURL code is as follows:

```shell theme={null}
curl -X POST 'https://api.xhuoapi.ai/v1/fish/audios' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "action": "speech",
  "prompt": "a white siamese cat",
  "model": "fish-tts",
  "voice_id": "d7900c21663f485ab63ebdb7e5905036"
}'
```

## Asynchronous Callback

Since the time taken by the Fish Audios Generation API to generate is relatively long, approximately 1-2 minutes, if the API does not respond for a long time, the HTTP request will keep the connection open, leading to additional system resource consumption. Therefore, this API also provides support for asynchronous callbacks.

The overall process is: when the client initiates a request, an additional `callback_url` field is specified. After the client initiates the API request, the API will immediately return a result containing a `task_id` field information, representing the current task ID. When the task is completed, the result of the generated task will be sent to the client-specified `callback_url` in the form of a POST JSON, which also includes the `task_id` field, allowing the task result to be associated by ID.

Let’s understand how to operate specifically through an example.

First, the Webhook callback is a service that can receive HTTP requests, and developers should replace it with the URL of their own HTTP server. For demonstration purposes, we use a public Webhook sample site [https://webhook.site/](https://webhook.site/), and opening this site will give you a Webhook URL, as shown in the image below:

![](https://cdn.xhuoapi.ai/tbcnai.png)

Copy this URL, and it can be used as a Webhook. The sample here is `https://webhook.site/4815f79f-a40f-4078-ac85-1cc126b6bb34`.

Next, we can set the `callback_url` field to the above Webhook URL while filling in the corresponding parameters, as shown in the image below:

<p>
  <img src="https://cdn.xhuoapi.ai/8rv4te.png" width="500" className="m-auto" />
</p>

Clicking run, you will immediately receive a result, as follows:

```
{
  "task_id": "2725a2d3-f87e-4905-9c53-9988d5a7b2f5"
}
```

After a moment, we can observe the result of the generated task at `https://webhook.site/4815f79f-a40f-4078-ac85-1cc126b6bb34`, as shown in the image below:

![](https://cdn.xhuoapi.ai/9utzve.png)

The content is as follows:

```json theme={null}
{
    "success": true,
    "task_id": "2725a2d3-f87e-4905-9c53-9988d5a7b2f5",
    "trace_id": "e2d308bc-4df8-4c69-9369-a60f3c54f2b3",
    "data": [
        {
            "audio_url": "https://platform.r2.fish.audio/task/b627c2f7d38a4083a837570ba6d0962f.mp3"
        }
    ]
}
```

We can see that the result contains a `task_id` field, and the other fields are similar to those mentioned above, allowing the task to be associated through this field.

## Error Handling

When calling the API, if an error occurs, the API will return the corresponding error code and message. For example:

* `400 token_mismatched`: Bad request, possibly due to missing or invalid parameters.
* `400 api_not_implemented`: Bad request, possibly due to missing or invalid parameters.
* `401 invalid_token`: Unauthorized, invalid or missing authorization token.
* `429 too_many_requests`: Too many requests, you have exceeded the rate limit.
* `500 api_error`: Internal server error, something went wrong on the server.

### Error Response Example

```json theme={null}
{
  "success": false,
  "error": {
    "code": "api_error",
    "message": "fetch failed"
  },
  "trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}
```

## Conclusion

Through this document, you have learned how to use the Fish Audios Generation API to clone voices by inputting prompt words. We hope this document helps you better integrate and use the API. If you have any questions, please feel free to contact our technical support team.
