Make images from a script
Pro
You’ll get: a script that writes an image file from a prompt, on your own Mac, through the same gateway as your other apps. Use it for batches, tests or automations.
You need:
- A local image model in Image Lab. See Create your first image.
- A client API key that can use the model. Make one in API keys.
- For curl:
jqandbase64. For Python: theopenaipackage (pip install openai).
Open Image Lab → Use in your apps to copy these examples with your address and model already in place.
Set the key
Section titled “Set the key”export NEXUS_API_KEY=msty_nexus_key_herePut your client API key in place of msty_nexus_key_here.
curl http://127.0.0.1:3939/v1/images/generations \ -H "Authorization: Bearer $NEXUS_API_KEY" \ -H "Content-Type: application/json" \ -d '{"model": "images/flux-3-image-klein-q4", "prompt": "A red bicycle against a white wall", "size": "1024x1024"}' \ | jq -r '.data[0].b64_json' | base64 --decode > image.pngPython
Section titled “Python”import base64, osfrom openai import OpenAI
client = OpenAI(base_url="http://127.0.0.1:3939/v1", api_key=os.environ["NEXUS_API_KEY"])result = client.images.generate( model="images/flux-3-image-klein-q4", prompt="A red bicycle against a white wall", size="1024x1024",)with open("image.png", "wb") as file: file.write(base64.b64decode(result.data[0].b64_json))print("seed", result.data[0].seed)Change an image from a script
Section titled “Change an image from a script”result = client.images.edit( model="images/flux-3-image-klein-q4", image=open("image.png", "rb"), prompt="Make the bicycle blue",)Save the images to the gallery too
Section titled “Save the images to the gallery too”Images from scripts aren’t saved in the Gallery. To save them, send the header:
result = client.images.generate( model="images/flux-3-image-klein-q4", prompt="A red bicycle against a white wall", extra_headers={"X-Msty-Nexus-Image-Gallery": "save"},)With curl, add -H "X-Msty-Nexus-Image-Gallery: save".
Check it worked
Section titled “Check it worked”image.png opens as a picture. With the save header, it also shows in Gallery.
- A 1024 × 1024 image takes about 100 seconds on an M3 Max. Use
512x512while you test. Raise the client’s timeout if it gives up early. - Run one request at a time per Mac. A request for a different local model while another image is being made gets a busy error (429) with a
Retry-Afterheader. Wait and retry. - Use a fixed
seed(inextra_bodyfor Python) to get the same image again.
All fields and errors: Use images in your apps.