FLUX 3 Image bounding box API: a complete Python example
This is a working Python client for FLUX 3 Image bounding boxes: build the caption and JSON element table, POST it to https://api.bfl.ai/v1/flux-3-image with your x-key, poll polling_url until Ready, and save result.sample. A second snippet moves one element in an existing image with src_bbox and tgt_bbox.
What you need before you run it
- A Black Forest Labs API key in the environment variable
BFL_API_KEY. This site cannot issue keys; get one from your own BFL account. - Python 3 with
requestsinstalled (pip install requests). - The box rules from the official bounding-box guide (checked 19 October 2026): every box is
[top, left, bottom, right], integers 0–1000, measured from the top-left. The boxes go insideprompt; there is no separate box parameter.
This is an independent guide, not Black Forest Labs. Request shape and pricing tables are on the FLUX 3 Image bounding box API guide; pixel conversion is on the coordinates page.
Full Python example: generate a layout with boxes
import json
import os
import time
import requests
API = "https://api.bfl.ai/v1/flux-3-image"
KEY = os.environ["BFL_API_KEY"]
WAITING = {"Pending", "Reasoning", "Generating"}
def pixels_to_bbox(x, y, w, h, W, H):
"""Pixel rectangle (top-left x, y, width, height) -> [top, left, bottom, right] on 0-1000."""
clamp = lambda v: max(0, min(1000, round(v)))
return [clamp(y / H * 1000), clamp(x / W * 1000),
clamp((y + h) / H * 1000), clamp((x + w) / W * 1000)]
def run(payload, out_path, timeout_s=300):
r = requests.post(API, headers={"x-key": KEY}, json=payload, timeout=60)
r.raise_for_status()
task = r.json() # id, polling_url, cost (credits)
print("submitted", task.get("id"), "cost", task.get("cost"))
deadline = time.time() + timeout_s
while True:
time.sleep(1.5)
res = requests.get(task["polling_url"], headers={"x-key": KEY}, timeout=60).json()
if res["status"] not in WAITING:
break
if time.time() > deadline:
raise TimeoutError(task["id"])
if res["status"] != "Ready": # Error, Request Moderated, Content Moderated, Task not found
raise RuntimeError(res)
img = requests.get(res["result"]["sample"], timeout=120) # download within one hour
img.raise_for_status()
with open(out_path, "wb") as f:
f.write(img.content)
return out_path
caption = ("A square product card: a red ceramic mug <mug_1> on a pale oak table <table_1>, "
"with a short cream headline <title_1> across the top.")
rows = [
{"id": "table_1", "bbox": [600, 0, 1000, 1000], "desc": "Pale oak tabletop, soft daylight."},
# a mug drawn at x=300, y=200, 500x400 on a 1920x1080 sketch would be pixels_to_bbox(300, 200, 500, 400, 1920, 1080)
{"id": "mug_1", "bbox": [300, 330, 820, 670], "desc": "A red ceramic mug, handle on the right, soft shadow."},
{"id": "title_1", "bbox": [60, 120, 180, 880], "desc": 'Text reading "Morning List" in a cream serif, one line.'},
]
print(run({"prompt": caption + " " + json.dumps(rows), "aspect_ratio": "1:1", "grounding": False}, "card.png"))
Why each line is there: the caption names every element with an angle-bracket id; each row repeats that id with a bbox and a desc; caption and JSON array are joined with one space and sent as prompt. Send the aspect_ratio you designed the boxes for, because the 0–1000 grid stretches with the frame. grounding defaults to true on the API (it may search the web first); the example turns it off so only your prompt drives the layout.
Move one element in an existing image
For edits, put the source in images (the first one is ref_image_0) and replace bbox with from, src_bbox, and tgt_bbox. Keep rows repeat the same box twice; a move row gets a new tgt_bbox. This reuses run() from above.
def keep(eid, box, desc):
return {"id": eid, "from": "ref_image_0", "src_bbox": box, "tgt_bbox": box, "desc": desc}
instruction = ("In <ref_image_0>, move the red mug <mug_1> to the left third of the table <table_1>. "
"Keep the rest of the image unchanged, including the headline <title_1>.")
rows = [
{"id": "mug_1", "from": "ref_image_0", "src_bbox": [300, 330, 820, 670],
"tgt_bbox": [300, 60, 820, 400], "desc": "A red ceramic mug, handle on the right."},
keep("table_1", [600, 0, 1000, 1000], "Pale oak tabletop, soft daylight."),
keep("title_1", [60, 120, 180, 880], 'Text reading "Morning List" in a cream serif.'),
]
payload = {"prompt": instruction + " " + json.dumps(rows),
"images": ["https://example.com/card.png"]} # your own hosted source image or base64
print(run(payload, "card_moved.png"))
To remove an element, set its tgt_bbox to null (None in Python) and say so in the instruction. To replace or recolor, use "from": None, "src_bbox": None and describe the result in desc. The guide recommends stating every change in the instruction as well as in the rows.
Errors you will actually see
- HTTP 4xx on submit with a field like
boundingBoxes. The publishedFlux3ImageInputsschema rejects extra properties. Move the boxes intoprompt. Request Moderated/Content Moderated. The poll loop above raises with the full response. Change the prompt or reference; retrying the same input will not help.- Element missing in the output. The guide says new elements in boxes of about 40 × 25 pixels often did not appear in BFL’s tests. Make the box bigger.
- Element in the wrong place. Check the order is
[top, left, bottom, right], not[x, y, w, h]. See the coordinates page. - Download link expired. The overview says to fetch
result.samplewithin one hour ofReady.
Limits
Boxes guide placement and scale; they are not clipping masks, and an element can spill slightly past its box. Outside pixels usually stay identical on edits, but not guaranteed bit-for-bit; composite afterwards if you need that. Each call costs credits at the list price on the BFL pricing page (the submit response returns cost). The code here is an illustration written for this guide and checked against the documented request shape on 19 October 2026; run it against your own key before relying on it.