Flux Images Generation API Integration Guide

This article introduces how to integrate the Flux Images Generation API, which can generate official Flux images by providing custom parameters.

Application Process

To use the Flux Images Generation API, first obtain your API Token from the Ace Data Cloud Console and keep it for later use.

If you have not logged in or registered, you will be automatically redirected to the login page to register and log in. Once completed, you will be automatically returned to the current page.

A general application's API Key can be used for services enabled for the account; a dedicated application's Key is used within that application's service scope. Please check the Key's permissions, validity period, and limits, and confirm that the application balance is sufficient to pay for this request. Available trial credits are subject to what is displayed in your account; if the balance is insufficient, click the "Recharge" button.

📘 Full documentation: Flux Images Generation API →

Basic Usage

First, let's look at the basic usage: enter the prompt prompt, generation action action, and image size size to obtain the processed result. First, simply pass an action field with the value generate, and then enter a prompt. The details are as follows:

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

  • accept: The format in which you want to receive the response. Here it is set to application/json, meaning JSON format.
  • authorization: The key used to call the API. After obtaining it, you can select it directly from the dropdown.

We have also set the Request Body, including:

  • action: The action for this image generation task.
  • size: The dimensions of the generated image. The flux-2-flex / flux-2-pro / flux-2-max series require an image aspect ratio (such as 1:1 or 16:9) and do not accept pixel dimensions such as 1024x1024. Omitting it will return a 400 error.
  • count: The number of images to generate. The default value is 1. This parameter is only valid for image generation tasks and is invalid for editing tasks.
  • prompt: The prompt.
  • model: The generation model, defaulting to flux-dev; the latest flagship models are flux-2-pro and flux-2-max (higher image quality, requiring an image aspect ratio for size).
  • callback_url: The URL to which the result should be sent.
  • async: Optional. When set to true, the API immediately returns a task_id without requiring a callback_url. The result can then be retrieved by polling the corresponding task query API.

The size parameter has some specific restrictions, mainly divided into two types: width x height dimensions and x:y image aspect ratios. The details are as follows:

Model Range
flux-dev Supports dimensions 1024x1024, 1024x1792, 1792x1024, or aspect ratios
flux-pro Supports dimensions 1024x1024, 1024x1792, 1792x1024, or aspect ratios
flux-2-flex Only supports image aspect ratios
flux-2-pro Only supports image aspect ratios
flux-2-max Only supports image aspect ratios
flux-kontext-pro Only supports image aspect ratios
flux-kontext-max Only supports image aspect ratios

Reference image aspect ratios: "21:9", "16:9", "4:3", "3:2", "1:1", "2:3", "3:4", "9:16", "9:21".

After selecting the parameters, the corresponding code will be automatically generated on the right. Before copying it, please confirm that the authorization header uses your own API Key. Real credentials should not appear in documents or screenshots.

Click the "Try" button to test it. Here we obtained the following result:

{
  "success": true,
  "task_id": "5456c749-3bbb-4f10-9eb8-cfbcac297500",
  "trace_id": "ae4eecb8-1dd6-45b4-bfb3-a1c48872536e",
  "data": [
    {
      "image_url": "https://cdn.acedata.cloud/assets/examples/flux/5456c749-3bbb-4f10-9eb8-cfbcac297500-d0ef60485f73.jpg"
    }
  ]
}

The returned result contains several fields, described as follows:

  • success, the status of the video generation task.
  • task_id, the ID of the video generation task.
  • trace_id, the tracking ID of the video generation task.
  • data, the list of results from the image generation task.
    • image_url, the link for the image generation task.
    • prompt, the prompt.

You can see that we obtained satisfactory image information. We only need to use the image link in data from the result to retrieve the generated Flux image.

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

curl -X POST 'https://api.acedata.cloud/flux/images' \
-H 'authorization: Bearer {token}' \
-H 'accept: application/json' \
-H 'content-type: application/json' \
-d '{
  "action": "generate",
  "prompt": "A photorealistic studio product shot of a frosted-glass perfume bottle on wet black slate, single softbox key light, water droplets, dark moody background, 85mm macro.",
  "model": "flux-2-pro",
  "size": "1:1"
}'

Image Editing Task

If you want to edit an image, first provide the link to the image to be edited in the image_url parameter. In this case, action only supports edit, and you can specify the following:

  • model: The model used for this image editing task. Supported models are flux-dev, flux-pro, flux-kontext-pro, flux-kontext-max, flux-2-flex, flux-2-pro, and flux-2-max.
  • image_url: Upload the image to be edited.

An example is shown below:

After completing the fields, the code is automatically generated as follows:

The corresponding code:

import requests

url = "https://api.acedata.cloud/flux/images"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "action": "edit",
    "prompt": "a white siamese cat",
    "model": "flux-kontext-pro",
    "image_url": "https://cdn.acedata.cloud/ytj2qy.png"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

Click Run, and you will see that a result is returned immediately, as follows:

{
  "success": true,
  "task_id": "2a7979ff-1f77-4380-92c6-a2dc37c3b4c8",
  "trace_id": "732b65c0-48d9-49f7-b568-64e5acffe4c0",
  "data": [
    {
      "prompt": "a white siamese cat",
      "image_url": "https://cdn.acedata.cloud/e724d7f13d.png",
      "timings": 1752744073
    }
  ]
}

As you can see, the generated result is an edited version of the original image, and the result is similar to the one above.

Asynchronous Callback

Since the Flux Images Generation API takes a relatively long time to generate an image, approximately 1–2 minutes, if the API does not respond for a long time, the HTTP request will keep the connection open, resulting in additional system resource consumption. Therefore, this API also supports asynchronous callbacks.

The overall process is as follows: when the client initiates a request, it additionally specifies a callback_url field. After the client initiates the API request, the API immediately returns a result containing a task_id field, which represents the current task ID. When the task is complete, the generated image result is sent as POST JSON to the callback_url specified by the client, and it also includes the task_id field, so the task result can be associated with the task by ID.

Below, we will use an example to understand how this works in practice.

First, a 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, this demonstration uses a public Webhook example website, https://webhook.site/. Open the website to obtain a Webhook URL, as shown below:

Copy this URL to use it as a Webhook. The example used here is https://webhook.site/3d32690d-6780-4187-a65c-870061e8c8ab.

Next, we can set the callback_url field to the Webhook URL above and fill in the corresponding parameters, as shown below:

Click Run, and you will see that a result is returned immediately, as follows:

{
  "task_id": "6a97bf49-df50-4129-9e46-119aa9fca73c"
}

After a short wait, we can see the generated image result at https://webhook.site/3d32690d-6780-4187-a65c-870061e8c8ab, as shown below:

The content is as follows:

{
  "success": true,
  "task_id": "6a97bf49-df50-4129-9e46-119aa9fca73c",
  "trace_id": "9b4b1ff3-90f2-470f-b082-1061ec2948cc",
  "data": [
    {
      "prompt": "a white siamese cat",
      "image_url": "https://cdn.acedata.cloud/e724d7f13d.png",
      "seed": 1698551532,
      "timings": {
        "inference": 3.328
      }
    }
  ]
}

As you can see, the result contains a task_id field, and the other fields are similar to those above. This field can be used to associate the result with the task.

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

{
  "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 Flux Images Generation API to generate images by entering prompts. 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.