Template Helpers

Use these reusable helpers with all template guides.

Authentication Setup (Environment Variable)

import os
import requests
from dotenv import load_dotenv

load_dotenv()

API_KEY = os.getenv("GENERIO_API_KEY")
if not API_KEY:
    raise RuntimeError("Missing GENERIO_API_KEY in environment")

BASE_URL = "https://flows.generio.ai"
HEADERS = {"Authorization": f"Bearer {API_KEY}"}

Polling Helper

import time

POLL_INTERVAL_SECONDS = 3
MAX_WAIT_SECONDS = 360

def wait_for_flow(flow_id: str):
    start = time.time()
    while True:
        res = requests.get(f"{BASE_URL}/flows/{flow_id}", headers=HEADERS)
        if res.status_code != 200:
            raise RuntimeError(f"Status check failed: {res.status_code} {res.text}")

        flow = res.json()
        state = flow.get("state")
        if state in ("completed", "failed", "aborted"):
            return flow

        if time.time() - start > MAX_WAIT_SECONDS:
            raise TimeoutError("Flow polling timed out")

        time.sleep(POLL_INTERVAL_SECONDS)

Output Download Helper (asset_id)

import base64
from pathlib import Path

def download_outputs(flow_id: str, out_dir: str = "out"):
    out_path = Path(out_dir)
    out_path.mkdir(parents=True, exist_ok=True)

    outputs_res = requests.get(f"{BASE_URL}/flows/{flow_id}/outputs", headers=HEADERS)
    if outputs_res.status_code != 200:
        raise RuntimeError(f"Output list failed: {outputs_res.status_code} {outputs_res.text}")

    outputs = outputs_res.json().get("outputs", [])
    for item in outputs:
        asset_id = item["asset_id"]
        asset_res = requests.get(
            f"{BASE_URL}/flows/{flow_id}/outputs/{asset_id}?include_data=true",
            headers=HEADERS
        )
        if asset_res.status_code != 200:
            raise RuntimeError(f"Asset download failed for {asset_id}: {asset_res.status_code}")

        data_uri = asset_res.json()["data"]
        raw = base64.b64decode(data_uri.split(",", 1)[1])
        file_path = out_path / f"{asset_id}.glb"
        file_path.write_bytes(raw)
        print(f"Saved: {file_path}")

Data URL Helpers

from pathlib import Path
from base64 import b64encode

def image_to_data_url(path: Path, mime: str = "image/png") -> str:
    encoded = b64encode(path.read_bytes()).decode()
    return f"data:{mime};base64,{encoded}"

def glb_to_data_url(path: Path) -> str:
    encoded = b64encode(path.read_bytes()).decode()
    return f"data:model/gltf-binary;base64,{encoded}"

Next Step

Use these helpers with: