SPB Git

Repository Public

generate.ts 2.7 KB 58 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : API Atlas8 * File             : examples/xai/images/generate.ts9 * 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 * Development      : dev@spboucher.ai23 * Support          : support@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Generate one image on xAI with plain fetch (no official xAI Node SDK; the `openai` client also works with baseURL https://api.x.ai/v1).43// STATUS: UNVERIFIED as a script (identical raw request LIVE_VERIFIED 2026-09-18 via scripts/live.py). Paid ($0.02).44// Run: node --env-file=.env --experimental-strip-types examples/xai/images/generate.ts "a plain white square"45import { writeFileSync } from "node:fs";4647const prompt = process.argv[2] ?? "a plain white square";48const res = await fetch("https://api.x.ai/v1/images/generations", {49  method: "POST",50  headers: { Authorization: `Bearer ${process.env.XAI_API_KEY}`, "Content-Type": "application/json" },51  body: JSON.stringify({ model: "grok-imagine-image", prompt, n: 1, aspect_ratio: "1:1", resolution: "1k", response_format: "b64_json" }),52});53const body = await res.json();54if (!res.ok) throw new Error(`HTTP ${res.status}: ${JSON.stringify(body)}`);55writeFileSync("tmp-live/xai-example-generate.jpg", Buffer.from(body.data[0].b64_json, "base64"));56console.log(JSON.stringify({ mime_type: body.data[0].mime_type, usage: body.usage }, null, 1));57console.log("saved tmp-live/xai-example-generate.jpg");58
Files main
Menu

Theme