AI API Keys Explained: How to Get and Use OpenAI, Anthropic, and Google Keys

Step-by-step guide to setting up AI API access

Ad placeholder (leaderboard)

What an API key is

An API key is a secret token that proves a request to an AI provider is coming from your account. When your code calls OpenAI, Anthropic, or Google, it includes the key in the request; the provider checks it, applies your rate and spending limits, and bills your account. Think of it as a password specifically for programmatic access.

The single most important thing to understand up front: the API is separate from the chat website. A ChatGPT Plus or Claude Pro subscription pays for the consumer site only. The API is pay-as-you-go and billed to a different account that you set up yourself. People are frequently surprised that their Plus subscription gives them no API credit at all.

How to get a key from each provider

The flow is similar across providers:

OpenAI — Sign up at platform.openai.com (this is the developer platform, not chatgpt.com). Add a payment method under Billing, then go to API keys and create a new secret key. Copy it immediately; you cannot view it again after closing the dialog.

Anthropic — Sign up at console.anthropic.com, add billing, and create a key under API Keys. Same rule: copy it at creation time.

Google (Gemini) — Go to ai.google.dev / Google AI Studio, sign in with a Google account, and click Get API key. Google AI Studio offers a free tier that is generous for learning, with paid usage through a linked Google Cloud project.

In every case, set a billing limit / budget right away so a mistake or a leaked key cannot run up an unbounded bill.

Storing keys securely

A leaked key is a real financial and security risk, so treat it like a password:

  • Never commit a key to Git, paste it into client-side JavaScript, or put it in a public webpage — bots scan public repos and abuse exposed keys within minutes.
  • Store the key in an environment variable (for example OPENAI_API_KEY) or a proper secrets manager, and read it at runtime.
  • For production, use a secrets service (cloud secret manager, Vault, or your platform’s environment settings) rather than a file.
  • If a key is ever exposed, revoke it immediately in the dashboard and issue a new one — revocation is instant.

Your first API call

Once your key is in an environment variable, calling the API is short. With curl and OpenAI:

curl https://api.openai.com/v1/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"Hello!"}]}'

With Python, install the SDK (pip install openai) and let it read the key from the environment automatically:

from openai import OpenAI
client = OpenAI()  # reads OPENAI_API_KEY from the environment
resp = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}],
)
print(resp.choices[0].message.content)

The Anthropic and Google SDKs follow the same pattern: install the package, set the key as an environment variable, create a client, and send a message. Start with the smallest, cheapest model while you learn — it costs a fraction of a cent per call and behaves the same way as the larger ones.

Managing cost and limits

Pricing is per token, counting both what you send and what the model returns, usually quoted per million tokens. To stay in control: set a hard monthly spending cap in billing, prefer the smallest model that meets your needs, monitor the usage dashboard, and rotate keys periodically. With a budget set and your key stored safely, the API is the cheapest and most flexible way to build with AI.

Ad placeholder (rectangle)