LearnThatStack Ace your next interview
LLM APIs & Integration · question
Question 4 of 52

How do you authenticate requests to the Anthropic, OpenAI, and Gemini APIs?

beginner
← All LLM APIs & Integration questions
Re-explain

All three send a long-lived secret key in a request header. Only the header name and a few extras differ.

curl https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01"

curl https://api.openai.com/v1/responses \
  -H "Authorization: Bearer $OPENAI_API_KEY"

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY"
  • Anthropic now takes the key as Authorization: Bearer, and still accepts the x-api-key header shown here. Every request also needs the anthropic-version header, or it fails. The SDK reads ANTHROPIC_API_KEY.
  • OpenAI takes Authorization: Bearer, and the SDK reads OPENAI_API_KEY.
  • Gemini takes x-goog-api-key, and the SDK reads GEMINI_API_KEY or GOOGLE_API_KEY. Gemini also accepts ?key= in the URL. URLs land in logs and proxies, so prefer the header.

The same models on Amazon Bedrock or Google Cloud use the cloud's own identity and access management (IAM) credentials instead. That means SigV4 request signing or a short-lived OAuth token.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for LLM APIs & Integration One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Your frontend needs LLM features. Where does the API key live, and why? A strong answer keeps the key on the server behind your own authenticated endpoint, because anything shipped to a browser or app can be read.
  • Why would a team call Claude or Gemini through Bedrock or Google Cloud instead of with a provider API key? Names short-lived credentials instead of a static secret, access controlled by existing IAM roles and policies, and billing on the cloud account.
  • A request fails with an authentication error. Should your client retry it, and what do you check first? Auth errors are permanent, so fail fast and alert instead of retrying. Check the key is set, not revoked, and sent in the header that provider expects.
  • How would you manage and rotate LLM API keys across several teams and environments? Covers a secrets manager, separate keys per team and environment, rotation without downtime, and spend limits per project or workspace.
  • What does the anthropic-version header do, and who sets it when you use the SDK? It pins the API behavior your code was written against, so later changes don't break it. The SDK sends it for you, and raw HTTP calls must add it.

What you can say

  1. All three use the same basic scheme. You send a long-lived secret API key in a request header, and only the header name and a few extras differ.
  2. OpenAI takes it as Authorization: Bearer, and the SDK picks it up from OPENAI_API_KEY.
  3. Anthropic's classic header is x-api-key, and it now accepts a Bearer token too. Every request also needs an anthropic-version header or it fails, and the SDK reads ANTHROPIC_API_KEY.
  4. Gemini uses x-goog-api-key, and its SDK reads GEMINI_API_KEY or GOOGLE_API_KEY. It also accepts ?key= in the URL, but I'd avoid that because URLs end up in logs and proxies.
  5. On Amazon Bedrock or Google Cloud, the same models use the cloud's IAM credentials instead, which means SigV4 request signing or a short-lived OAuth token.
  6. The trade-off is that a static key is simple, but a leaked one works until someone revokes it. So I keep it server-side, while IAM takes more setup but gives short-lived credentials.

Weak answers to avoid

  • Says every provider uses a Bearer token Gemini expects x-goog-api-key, and most Anthropic code sends x-api-key. Naming each header shows you have called these APIs yourself.
  • Forgets the anthropic-version header Anthropic rejects a request without it. Mention it along with the key, since it is the extra header that breaks hand-written HTTP calls.
  • Puts the key in the URL or in client code Query strings end up in logs and proxies, and client code ships the key to every user. Send it in a header from your own server.
  • Ignores Bedrock and Google Cloud access Many companies reach these models through their cloud account, where IAM replaces the provider key. Say it switches to SigV4 signing or short-lived OAuth tokens.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the LLM APIs & Integration cheatsheet.

← Back to all LLM APIs & Integration questions
Pro · $10/mo

45 of 52 LLM APIs & Integration answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime