Python Code Example
from google import genai
from google.genai import types
import os
# Get your CometAPI key from https://api.cometapi.com/console/token, and paste it here
COMETAPI_KEY = os.environ.get("COMETAPI_KEY") or "<YOUR_COMETAPI_KEY>"
BASE_URL = "https://api.cometapi.com"
client = genai.Client(
http_options={"api_version": "v1beta", "base_url": BASE_URL, "timeout": 600000},
api_key=COMETAPI_KEY,
)
prompt = (
"Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme"
)
response = client.models.generate_content(
model="gemini-2.5-flash-image",
contents=[prompt],
)
# Output directory
OUTPUT_DIR = os.path.join(os.path.dirname(__file__), "..", "output")
os.makedirs(OUTPUT_DIR, exist_ok=True)
for part in response.parts:
if part.text is not None:
print(part.text)
elif part.inline_data is not None:
image = part.as_image()
output_path = os.path.join(OUTPUT_DIR, "gemini-native-image.png")
image.save(output_path)
print(f"Image saved to: {output_path}")
JavaScript Code Example
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
// Get your CometAPI key from https://api.cometapi.com/console/token, and paste it here
const COMETAPI_KEY = process.env.COMETAPI_KEY || "<YOUR_COMETAPI_KEY>";
const BASE_URL = "https://api.cometapi.com";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function main() {
const ai = new GoogleGenAI({
apiKey: COMETAPI_KEY,
httpOptions: { baseUrl: BASE_URL },
});
const prompt =
"Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme";
const response = await ai.models.generateContent({
model: "gemini-2.5-flash-image",
contents: prompt,
});
// Output directory
const outputDir = path.join(__dirname, "..", "output");
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
for (const part of response.candidates[0].content.parts) {
if (part.text) {
console.log(part.text);
} else if (part.inlineData) {
const imageData = part.inlineData.data;
const outputPath = path.join(outputDir, "gemini-native-image.png");
const buffer = Buffer.from(imageData, "base64");
fs.writeFileSync(outputPath, buffer);
console.log(`Image saved to: ${outputPath}`);
}
}
}
main();
Curl Code Example
#!/bin/bash
# Get your CometAPI key from https://api.cometapi.com/console/token, and paste it here
# Output directory
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
OUTPUT_DIR="$SCRIPT_DIR/../output"
mkdir -p "$OUTPUT_DIR"
curl -s -X POST \
"https://api.cometapi.com/v1beta/models/gemini-2.5-flash-image:generateContent" \
-H "x-goog-api-key: $COMETAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [{
"parts": [
{"text": "Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme"}
]
}]
}' \
| grep -o '"data": "[^"]*"' \
| cut -d'"' -f4 \
| base64 --decode > "$OUTPUT_DIR/gemini-native-image.png"
echo "Image saved to: $OUTPUT_DIR/gemini-native-image.png"