Skip to content

Instantly share code, notes, and snippets.

@jakebrinkmann
Last active August 22, 2025 22:40
Show Gist options
  • Select an option

  • Save jakebrinkmann/5cfe0b5f4e34a0e5af6574b77c3f9766 to your computer and use it in GitHub Desktop.

Select an option

Save jakebrinkmann/5cfe0b5f4e34a0e5af6574b77c3f9766 to your computer and use it in GitHub Desktop.
Gemini API
node_modules
*.png
package-lock.json
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent" \
  -H 'Content-Type: application/json' \
  -H "X-goog-api-key: ${GEMINI_API_KEY}" \
  -X POST \
  -d '{
    "contents": [
      {
        "parts": [
          {
            "text": "Explain how AI works in a few words"
          }
        ]
      }
    ]
  }'
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "AI uses algorithms and data to mimic human intelligence.\n"
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "avgLogprobs": -0.32589561289007013
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 8,
    "candidatesTokenCount": 11,
    "totalTokenCount": 19,
    "promptTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 8
      }
    ],
    "candidatesTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 11
      }
    ]
  },
  "modelVersion": "gemini-2.0-flash",
  "responseId": "uO-oaNnmCeGj1MkP6rfqiAc"
}
#!/usr/bin/env node
import { GoogleGenAI, Modality } from "@google/genai";
import * as fs from "node:fs";
async function main() {
const ai = new GoogleGenAI({});
const contents =
"Hi, can you create a 3d rendered image of a pig " +
"with wings and a top hat flying over a happy " +
"futuristic scifi city with lots of greenery?";
// Set responseModalities to include "Image" so the model can generate an image
const response = await ai.models.generateContent({
model: "gemini-2.0-flash-preview-image-generation",
contents: contents,
config: {
responseModalities: [Modality.TEXT, Modality.IMAGE],
},
});
for (const part of response.candidates[0].content.parts) {
// Based on the part type, either show the text or save the image
if (part.text) {
console.log(part.text);
} else if (part.inlineData) {
const imageData = part.inlineData.data;
const buffer = Buffer.from(imageData, "base64");
fs.writeFileSync("gemini-native-image.png", buffer);
console.log("Image saved as gemini-native-image.png");
}
}
}
main();
{
"dependencies": {
"@google/genai": "^1.15.0"
}
}
import { GoogleGenAI } from "@google/genai";
const GEMINI_API_KEY = process.env.GEMINI_API_KEY;
const ai = new GoogleGenAI({ apiKey: GEMINI_API_KEY });
async function main() {
const response = await ai.models.generateContent({
model: "gemini-2.0-flash-001",
contents: "Why is the sky blue?",
});
console.log(response.text);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment