🎨 Call an NVIDIA AI Image Model

Same idea as the Hugging Face lesson — slightly different request shape
🖥️ Terminal · 🐍 Python · 🔐 key in an env var

If you've done the Hugging Face image lesson, this one is almost the same — we're just swapping vendors. NVIDIA hosts the same FLUX.1-schnell image model, but their API wraps the picture in a tiny JSON envelope instead of sending it as raw bytes. One extra line to decode it, and that's the whole difference. 🌱

🔐 First: save your key once

Both examples below read your key from an environment variable called NVIDIA_API_KEY. Get a free key at build.nvidia.com (top-right "Get API Key"), then set it once and forget it.

MAC / LINUX TERMINALexport NVIDIA_API_KEY="nvapi-your_real_key_here" # Add this line to ~/.zshrc so every new Terminal already has it.
WINDOWS POWERSHELL$env:NVIDIA_API_KEY = "nvapi-your_real_key_here" # Or run once: setx NVIDIA_API_KEY "nvapi-..." (takes effect in new terminals)

1️⃣ From the Terminal (curl + JSON decoder)

NVIDIA returns a JSON object that looks like {"artifacts":[{"base64":"..."}]} instead of raw image bytes — so we pipe the response through a one-liner Python decoder that pulls out the picture and writes it to robot.jpg.

TERMINAL — macOS / Linuxcurl https://ai.api.nvidia.com/v1/genai/black-forest-labs/flux.1-schnell \ -X POST \ -H "Authorization: Bearer $NVIDIA_API_KEY" \ -H "Accept: application/json" \ -H "Content-Type: application/json" \ -d '{"prompt":"a friendly robot painting a colorful picture","mode":"base","cfg_scale":0,"width":1024,"height":1024,"seed":0,"steps":4}' \ | python -c "import sys,json,base64; open('robot.jpg','wb').write(base64.b64decode(json.load(sys.stdin)['artifacts'][0]['base64']))" # Then open robot.jpg to see your new picture! 🎨
make_image.sh ↓ make_image.bat ↓ Mac/Linux: .sh · Windows cmd: .bat

2️⃣ From a Python Script

Same idea, all in one Python file: send the request, decode the base64 from the JSON, save the file. The requests library handles the network call; the base64 module (built in) does the decode.

PYTHONimport base64, os, requests # 1. The ADDRESS (where). The KEY (who) is read from NVIDIA_API_KEY. API_URL = "https://ai.api.nvidia.com/v1/genai/black-forest-labs/flux.1-schnell" headers = { "Authorization": f"Bearer {os.environ['NVIDIA_API_KEY']}", "Accept": "application/json", } # 2. Send a POST with the ORDER (body). cfg_scale=0 and steps=4 are required for "schnell". response = requests.post(API_URL, headers=headers, json={ "prompt": "a friendly robot painting a colorful picture", "mode": "base", "cfg_scale": 0, "width": 1024, "height": 1024, "seed": 0, "steps": 4, }) # 3. If NVIDIA refused, SHOW why and stop — don't pretend success on an error. if not response.ok: print(f"NVIDIA rejected the request ({response.status_code}): {response.text}") raise SystemExit(1) # 4. The picture is hidden inside a JSON envelope as a base64 string. Decode and save. data = response.json() image_bytes = base64.b64decode(data["artifacts"][0]["base64"]) with open("robot.jpg", "wb") as file: file.write(image_bytes) print("Done! Open robot.jpg to see your picture.")
robot.py ↓

First time? Install the helper once: pip install requests. Then run with python robot.py.

🔍 What's different from the Hugging Face version?

Same model, same prompt — the differences are all in the API contract:

What Hugging Face NVIDIA
Address router.huggingface.co/hf-inference/... ai.api.nvidia.com/v1/genai/...
Request body {"inputs": "..."} — one short field {"prompt": "...", "cfg_scale": 0, "steps": 4, ...} — a few required knobs
Response shape Raw JPEG bytes — save straight to a file. JSON with the image as a base64 string — one extra decode step.
Env var name HF_TOKEN NVIDIA_API_KEY

The vendor chooses the contract; the model — FLUX.1-schnell — is identical underneath. Run both scripts side by side with the same prompt and the pictures look like siblings. 🎨

🛠️ Try It: Build the Request Body

Type a picture idea below and watch the JSON body change in real time. This is the exact body the two scripts above send. Nothing gets sent anywhere — it's just to play with! 👇

REQUEST BODY (JSON)