Skip to main content

Minimal request

{
  "model": "gemini-2.5-flash",
  "contents": [
    {
      "role": "user",
      "parts": [{ "text": "Extract order fields and return JSON." }]
    }
  ],
  "generationConfig": {
    "responseMimeType": "application/json"
  }
}

cURL example

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [{ "text": "Extract order fields and return JSON." }]
      }
    ],
    "generationConfig": {
      "responseMimeType": "application/json"
    }
  }'

Python example

import requests

response = requests.post(
    "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent",
    headers={
        "x-goog-api-key": "<GEMINI_API_KEY>",
        "Content-Type": "application/json",
    },
    json={
        "contents": [
            {
                "role": "user",
                "parts": [{"text": "Extract order fields and return JSON."}],
            }
        ],
        "generationConfig": {
            "responseMimeType": "application/json",
        },
    },
)

print(response.json())

Node.js example

const response = await fetch(
  "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent",
  {
    method: "POST",
    headers: {
      "x-goog-api-key": process.env.GEMINI_API_KEY,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      contents: [
        {
          role: "user",
          parts: [{ text: "Extract order fields and return JSON." }]
        }
      ],
      generationConfig: {
        responseMimeType: "application/json"
      }
    }),
  }
);

console.log(await response.json());

Best practices

  • Set responseMimeType explicitly for structured output
  • Put the expected field shape into the prompt when output requirements are strict
  • Parse and validate JSON on the server before exposing it downstream