Repository Public
1#!/usr/bin/env bash2# =============================================================================3# SP BOUCHER | INFRASTRUCTURE & AUTOMATION4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project : API Atlas8# File : examples/xai/images/generate.sh9# Module : examples10# Repository : https://git.spboucher.ai/doc-api11# Created : 2026-09-1912# Last Modified : 2026-09-1913#14# Author : Simon-Pierre Boucher15# Position : Professor of Finance16# Institution : Université du Québec en Outaouais (UQO)17# Organization : SP Boucher18# Website : https://spboucher.ai19# Git : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Technical Contact: dev@spboucher.ai23# Security/Admin : admin@spboucher.ai24#25# Copyright (c) 202626# Simon-Pierre Boucher27# All rights reserved where applicable. This file is governed by the28# applicable project license, copyright notices and relevant law.29# Existing third-party rights and notices remain unaffected.30#31# License : None specified (no license file in this repository)32#33# Do not embed credentials, private keys, access tokens,34# certificates or confidential personal information.35#36# =============================================================================37# END OF SP BOUCHER SOURCE HEADER38# =============================================================================3940# STATUS: LIVE_VERIFIED 2026-09-18 (grok-imagine-image, 1:1, 1k, b64_json -> 200, JPEG ~110 KB, usage.cost_in_usd_ticks 200000000 = $0.02).41# Cost: $0.02 per run (cheapest Imagine model; grok-imagine-image-2.0 is $0.04-0.08 depending on quality/resolution). Not run automatically.42# Usage: bash examples/xai/images/generate.sh [prompt]43set -euo pipefail44cd "$(dirname "$0")/../../.."45source scripts/lib.sh46PROMPT="${1:-a plain white square}"47body=$(mktemp) # xai must not run inside $(...), otherwise XAI_STATUS is lost48xai POST /v1/images/generations -d "$(printf '{"model":"grok-imagine-image","prompt":"%s","n":1,"aspect_ratio":"1:1","resolution":"1k","response_format":"b64_json"}' "$PROMPT")" > "$body"49log_request xai POST /v1/images/generations "$XAI_STATUS" 0.02 "example images/generate.sh grok-imagine-image b64_json"50echo "HTTP $XAI_STATUS"51python3 -c 'import sys,json,base64; r=json.load(sys.stdin); d=r["data"][0]; open("tmp-live/xai-example-generate.jpg","wb").write(base64.b64decode(d["b64_json"])); print(json.dumps({"mime_type": d.get("mime_type"), "usage": r.get("usage")}, indent=1)); print("saved tmp-live/xai-example-generate.jpg")' < "$body"52