> ## 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.

# AI ID Photo Production API Integration Instructions

> AI ID Photo Production 集成指南 - XHuoAPI

This article will introduce an AI ID photo production API integration instruction, which can create various styles of ID photos by inputting a portrait photo URL and a preferred template.

## Application Process

To use the API, you need to first apply for the corresponding service on the [AI ID Photo Production API](https://api.xhuoapi.ai/documents/bf7214a3-8447-4157-8a75-f5bb39d5ed1b) page. After entering the page, click the "Acquire" button, as shown in the image:

![](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 the 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 is to input the portrait image that needs to be processed and the preferred AI ID photo template to obtain the processed result. You first need to simply pass a `image_urls` field, which is an array of links to the portrait images that need to be processed, as shown in the image:

<p>
  <img src="https://cdn.zhishuyun.com/2024-11-03-d23744954ca4819503469f04f2268aa0.jpg" width="500" className="m-auto" />
</p>

Then, we also need to input our preferred template. This article provides eight popular templates, which can be referenced below:

```json theme={null}
{
   "male_portrait":  Male Portrait Photo,
   "male_portrait2":  Male Portrait Photo (Another Version),
   "kindergarten":  Kindergarten Admission Photo,
   "logo_tshirt": Corporate Logo T-shirt Photo,
   "wedding":  Marriage Registration Photo,
   "business_photo":  Business Style Photo,
   "bob_suit": Black Suit with Bob Hairstyle,
   "female_portrait":  Female Portrait Photo
}
```

Next, we can specify the generation speed parameter `mode`, which is generally divided into two types: slow `relax` and fast `fast`. The specific content is as follows:

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

Here we can see that we 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, we set the Request Body, including:

* `mode`: The channel for generating ID photos, mainly with two types: fast and relax. When using relax, it is strongly recommended to use the parameter `callback_url` below.
* `template`: The style of the ID photo template.
* `image_urls`: The links to the ID photo portraits that need to be uploaded.
* `callback_url`: The URL to receive the callback result.

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

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

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

```json theme={null}
{
  "success": true,
  "task_id": "ae1e4948-dba1-4a6f-87af-67961b647428",
  "data": [
    {
      "id": "202411031951124776",
      "image_url": "https://platform.cdn.xhuoapi.ai/headshots/ae1e4948-dba1-4a6f-87af-67961b647428.png",
      "template": "Male Portrait Photo"
    },
    {
      "id": "202411031951128490",
      "image_url": "https://platform.cdn.xhuoapi.ai/headshots/ae1e4948-dba1-4a6f-87af-67961b647428.png",
      "template": "Male Portrait Photo"
    }
  ]
}
```

The returned result contains multiple fields, described as follows:

* `success`: The status of the ID photo generation task at this time.
* `task_id`: The ID of the ID photo generation task at this time.
* `data`: The result list of the ID photo generation task at this time.
  * `id`: The photo ID of the ID photo generation task at this time.
  * `image_url`: The image link of the ID photo generation task at this time.
  * `template`: The name of the ID photo template for the ID photo generation task at this time.

We can see that we have obtained satisfactory ID photo information based on the template and portrait image. We only need to retrieve the ID photo using the image 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/headshots/generate' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "mode": "fast",
  "template": "male_portrait",
  "image_urls": ["https://cdn.zhishuyun.com/2024-11-03-d23744954ca4819503469f04f2268aa0.jpg"]
}'
```

## Asynchronous Callback

Since the AI ID photo generation time 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 ID photo 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 convenience in demonstration, we use a public Webhook sample site [https://webhook.site/](https://webhook.site/), where you can open the site to get a Webhook URL, as shown in the image:

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

Copy this URL, and it can be used as a Webhook. The sample here is `https://webhook.site/00f38b26-4289-4899-83d6-0cea7308850a`.

Next, we can set the `callback_url` field to the above Webhook URL, while filling in the portrait image link and template. This article recommends using asynchronous callbacks when the parameter `mode` is set to `relax`, as shown in the image:

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

Clicking run, you can find that an immediate result is obtained, as follows:

```
{
  "task_id": "763b1450-8804-434f-acc7-d713be73a28f"
}
```

After a short wait, we can observe the result of the generated song at `https://webhook.site/00f38b26-4289-4899-83d6-0cea7308850a`, as shown in the image:
![](https://cdn.xhuoapi.ai/ym6fw1.png)

The content is as follows:

```json theme={null}
{
    "success": true,
    "task_id": "763b1450-8804-434f-acc7-d713be73a28f",
    "data": [
        {
            "id": "202411032010131366",
            "image_url": "https://platform.cdn.xhuoapi.ai/headshots/763b1450-8804-434f-acc7-d713be73a28f.png",
            "template": "Male portrait"
        },
        {
            "id": "202411032010132420",
            "image_url": "https://platform.cdn.xhuoapi.ai/headshots/763b1450-8804-434f-acc7-d713be73a28f.png",
            "template": "Male portrait"
        }
    ]
}
```

You can see that the result contains a `task_id` field, and other fields are similar to the above text. This field can be used to associate tasks.

## 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 AI ID photo production API, which can create various styles of ID photos by inputting a portrait photo URL and your preferred template. 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.
