How to Send Requests to Azure Flux 2.0 PROD Foundry

How to Fix Azure Student Subscription Region Error

Are you trying to generate an image using Azure’s AI services but feeling unsure about how to get started? Don’t worry! Here’s a simple step-by-step guide to help you create an image through a curl command or a Python script. Just follow these instructions, and you’ll be able to produce images quickly and easily.

Firstly, you’ll need your specific “foundryhubname” and your API key from Azure. Make sure to replace “foundryhubname” with your actual resource name and “AZURE_API_KEY” with your key in the code. The process involves two main options: using a curl command directly or writing a small Python script.

For the curl method, here’s the command to copy and customize:

bash
curl -X POST “https://.services.ai.azure.com/providers/blackforestlabs/v1/flux-2-pro?api-version=preview” \
-H “Content-Type: application/json” \
-H “Authorization: Bearer ” \
-d ‘{
“prompt”: “A photograph of a red fox in an autumn forest”,
“width”: 1024,
“height”: 1024,
“n”: 1,
“model”: “FLUX.2-pro”
}’ | jq -r ‘.data[0].b64_json’ | base64 –decode > generated_image.png

Make sure to replace <your_foundryhubname> and <your_api_key> with your real values. This command sends a request to create an image based on your prompt, decodes the response, and saves it as “generated_image.png” on your device.

If you prefer using Python, here’s a simple script that accomplishes the same task:

python
import requests
import base64

Set your endpoint URL here

ENDPOINT = “https://.services.ai.azure.com/providers/blackforestlabs/v1/flux-2-pro?api-version=preview”

Your API key

API_KEY = ““

Define your image prompt and specifications

payload = {
“prompt”: “A photograph of a red fox in an autumn forest”,
“width”: 1024,
“height”: 1024,
“n”: 1,
“model”: “FLUX.2-pro”,
}

headers = {
“Content-Type”: “application/json”,
“Authorization”: f”Bearer {API_KEY}”,
}

Send the POST request

response = requests.post(ENDPOINT, headers=headers, json=payload)
response.raise_for_status()

Get the base64 image data from the response

data = response.json()
b64_img = data[“data”][0][“b64_json”]
img_bytes = base64.b64decode(b64_img)

Save the image to your computer

with open(“generated_image.png”, “wb”) as f:
f.write(img_bytes)

print(“Image saved as generated_image.png”)

Again, make sure to update the URL and your API key accordingly. Running this script will create an image based on your prompt and save it in your current folder.

Give these steps a try, and let us know if you’re able to generate your image successfully! This straightforward approach is designed to help you get started quickly.