API key not valid. Please pass a valid API key

What is the name of the modal you’re running?

This mandatory question in unclear. What do you mean by the “name of the modal”? If you mean the name of the model “gemini-2.5-flash”

What is the issue or error you’re encountering

Calls to Google Studio Gemini API returns “API key not valid. Please pass a valid API key”

What steps have you taken to resolve the issue?

We tried to generate a new API Key on Google AI Studio. Did not solve the issue.
We also verified that the API Key added in Cloudflare works by calling the API locally from our computer.

What are the steps to reproduce the issue?

Set up the AI Gateway with a Google Studio API key. Run a query against a Gemini model.

Screenshot of the error

Maybe related to BYOK not work with Unified API

Also seeing this, seems to be streaming specific to me

@marco31 Unable to replicate this issue, these permutations are working as expected

  • Google AI Studio + Unified API + API key in Authorization header (Bearer <key>)
  • Google AI Studio + Unified API + key stored with BYOK
  • Google AI Studio + Native API + API key in x-goog-api-key header (<key>)
  • Google AI Studio + Native API + key stored with BYOK

Can you DM and share account/gateway info or any other details that might be useful

Hello, it’s still not working for me. I’m using Google AI Studio + Native API + key stored with BYOK.

  • working: https://gateway.ai.cloudflare.com/v1/<xxx>/<yyy>/google-ai-studio/v1beta/models/gemini-2.5-flash:generateContent

  • NOT working: https://gateway.ai.cloudflare.com/v1/<xxx>/<yyy>/google-ai-studio/v1beta/models/gemini-2.5-flash:streamGenerateContent

error returned is

[{
  "error": {
    "code": 400,
    "message": "API key not valid. Please pass a valid API key.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "API_KEY_INVALID",
        "domain": "googleapis.com",
        "metadata": {
          "service": "generativelanguage.googleapis.com"
        }
      },
      {
        "@type": "type.googleapis.com/google.rpc.LocalizedMessage",
        "locale": "en-US",
        "message": "API key not valid. Please pass a valid API key."
      }
    ]
  }
}