<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Fernando Astrada</title>
    <description>The latest articles on DEV Community by Fernando Astrada (@fernandoastrada).</description>
    <link>https://dev.to/fernandoastrada</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4262468%2Fb91d0b16-0677-4941-aae3-e4a3bf8cfe9c.png</url>
      <title>DEV Community: Fernando Astrada</title>
      <link>https://dev.to/fernandoastrada</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fernandoastrada"/>
    <language>en</language>
    <item>
      <title>How to Get a Free Gemini API Key in 2026 (and Fix the 3 Errors That Stop Most People)</title>
      <dc:creator>Fernando Astrada</dc:creator>
      <pubDate>Tue, 24 Nov 2026 14:26:29 +0000</pubDate>
      <link>https://dev.to/fernandoastrada/how-to-get-a-free-gemini-api-key-in-2026-and-fix-the-3-errors-that-stop-most-people-pec</link>
      <guid>https://dev.to/fernandoastrada/how-to-get-a-free-gemini-api-key-in-2026-and-fix-the-3-errors-that-stop-most-people-pec</guid>
      <description>&lt;p&gt;You can call Google's Gemini models from Python for &lt;strong&gt;$0&lt;/strong&gt;: no credit card, no GPU, no big downloads. Here's the whole setup in four steps, plus the three errors that stop most beginners.&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/Nt7wZXDt25U" width="710" height="399"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  What you need
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A Google account&lt;/li&gt;
&lt;li&gt;Python 3.10 or newer&lt;/li&gt;
&lt;li&gt;A terminal (PowerShell, macOS or Linux)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Create the key
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Open &lt;a href="https://aistudio.google.com" rel="noopener noreferrer"&gt;aistudio.google.com&lt;/a&gt; and sign in.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Get API key&lt;/strong&gt;, then &lt;strong&gt;Create API key&lt;/strong&gt;. Pick a project, or use the default one.&lt;/li&gt;
&lt;li&gt;Copy the key. Treat it like a password.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 2: Save it as an environment variable
&lt;/h2&gt;

&lt;p&gt;Never paste the key into your code (or into a screenshot, or a chat).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Windows (PowerShell or CMD):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;setx&lt;/span&gt; &lt;span class="no"&gt;GEMINI_API_KEY&lt;/span&gt; &lt;span class="s2"&gt;"paste-your-key"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then &lt;strong&gt;close the terminal and open a new one&lt;/strong&gt;. &lt;code&gt;setx&lt;/code&gt; only affects new windows. Also: run it on its own line, not chained with other commands.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;macOS / Linux:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;GEMINI_API_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"paste-your-key"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the same line to your &lt;code&gt;~/.zshrc&lt;/code&gt; or &lt;code&gt;~/.bashrc&lt;/code&gt; to keep it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Install the library
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip install google-genai
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Test it
&lt;/h2&gt;

&lt;p&gt;Create &lt;code&gt;test.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;google&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;genai&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;genai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# reads GEMINI_API_KEY automatically&lt;/span&gt;
&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"gemini-3.5-flash-lite"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Say hi to Astrada Codes viewers."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;python test.py&lt;/code&gt;. If an answer prints, your key works.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 3 errors that stop most people
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. &lt;code&gt;404 NOT_FOUND&lt;/code&gt;&lt;/strong&gt;: your code uses an old model name (very common when you copy an older tutorial). List the models your key can actually use and pick a Flash or Flash-Lite one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. &lt;code&gt;429 RESOURCE_EXHAUSTED&lt;/code&gt;&lt;/strong&gt;: you hit the free rate limit. Wait a minute and retry. Limits are &lt;strong&gt;per project, not per key&lt;/strong&gt;, so creating more keys won't help. Your exact limits are on the rate-limit page in AI Studio.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. "API key is missing"&lt;/strong&gt;: you're still in the old terminal after &lt;code&gt;setx&lt;/code&gt;. Open a new one and check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="no"&gt;GEMINI_API_KEY&lt;/span&gt;      &lt;span class="c1"&gt;# PowerShell&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$GEMINI_API_KEY&lt;/span&gt;          &lt;span class="c"&gt;# macOS / Linux&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The fine print (honest version)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It's free with no card, for selected models (Flash / Flash-Lite are the safe picks).&lt;/li&gt;
&lt;li&gt;Limits apply per project and are shown in AI Studio.&lt;/li&gt;
&lt;li&gt;On the free tier, Google can use your prompts to improve its products. Don't send passwords or private customer data.&lt;/li&gt;
&lt;li&gt;If your key ever leaks, delete it in AI Studio and create a new one.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Next: build an AI agent with this key
&lt;/h2&gt;

&lt;p&gt;This is part 0 of a free series: &lt;strong&gt;Build AI Agents in Python (Free, No GPU)&lt;/strong&gt;. All the code lives here:&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://github.com/fernandoastrada/astrada-codes-ai-agents" rel="noopener noreferrer"&gt;github.com/fernandoastrada/astrada-codes-ai-agents&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next up: a ~70-line Python agent that decides on its own which tools to use. Subscribe on YouTube (&lt;a href="https://www.youtube.com/@AstradaCodes" rel="noopener noreferrer"&gt;@AstradaCodes&lt;/a&gt;) so you don't miss it.&lt;/p&gt;

</description>
      <category>python</category>
      <category>ai</category>
      <category>gemini</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
