AIAI Club
← Back to articles
ENGLISH GUIDE

Error 400 Invalid Argument: API key not valid — Self-Check for Four Basic Mistakes

The key is clearly filled in but still reports invalid? A self-check list for basic pitfalls: leading/trailing spaces, environment variable loading failures, incorrect request header (Header) parameter passing, and a disabled project.

1. Four Most Common Beginner Oversights

1. Accidentally selecting extra leading/trailing half-width spaces or newline characters when copying the key;
2. Defining GEMINI_API_KEY in the .env environment variable file, but mistakenly reading OPENAI_API_KEY in the code;
3. Incorrectly appending the key to URL parameters (for example, missing the ?key= prefix);
4. The corresponding Google Cloud project has been deleted in the console, or the associated project has been disabled. Carefully check these four items, and 99% of 400 key errors can be instantly diagnosed and resolved.

This English translation is based on a Chinese source article. Prices are approximate where stated and conditions should be confirmed with the official provider or seller.