I use my own API key in private projects, and it seems to work like this:

# .env file
ANTHROPIC_API_KEY=sk-...

In the project:

# .claude/settings.json
{
  "apiKeyHelper": "source .env && echo $ANTHROPIC_API_KEY"
}

Then invoke claude in inside the project root and verify with /status command:

Claude Code v1.0.85
  L Session ID: 69...
...
 Account
  L Auth Token: apiKeyHelper
  L API Key: apiKeyHelper

Source: https://github.com/anthropics/claude-code/issues/441#issuecomment-3207226042

If someone get this error: Error getting API key from apiKeyHelper (in settings or ~/.claude.json): /bin/sh: 1: source: not found

That error comes from the fact that your shell (likely /bin/sh) doesn’t recognize the source command — it’s a Bash built-in, not POSIX-standard.

Here's the fix (Force bash):

{ "apiKeyHelper": "bash -c 'source .env && echo $ANTHROPIC_API_KEY'" }

Never teach people to put a secret key in a non-popular env var convention file. Always put it in the popular environment variable file.

Answer from ImBIOS on Stack Overflow
🌐
Claude
docs.claude.com › en › api › admin-api › apikeys › get-api-key
Get API Key - Claude Docs
Learn more about extending Claude's capabilities with Agent Skills. ... curl "https://api.anthropic.com/v1/organizations/api_keys/apikey_01Rj2N8SVvo6BePZj99NhmiT" \ --header "anthropic-version: 2023-06-01" \ --header "content-type: application/json" \ --header "x-api-key: $ANTHROPIC_ADMIN_KEY"
🌐
Pickaxe
pickaxe.co › post › how-to-get-your-claude-api-key-a-step-by-step-guide
How to get your Claude API key: A step-by-step guide (2025) - Pickaxe Blog
To get a Claude API key, you must create an Anthropic account add securely generate your key within the Anthropic Console. The key is shown only once so store it safely.
🌐
Claude
support.claude.com › en › articles › 8114521-how-can-i-access-the-claude-api
How can I access the Claude API? | Claude Help Center
Organizations interested in building with the Claude API can create a Claude Console account. The Claude Console is where you can create API keys, add users to your team, set up billing, and experiment with Claude on the Workbench.
🌐
Apideck
apideck.com › blog › how-to-get-your-claude-anthropic-api-key
How to Get Your Claude (Anthropic) API Key
This step-by-step guide shows you exactly how to create your Anthropic account, generate and secure your API key, set up billing, and test your first request. ... Navigate to console.anthropic.com and click the "Sign up" or “Continue with ...
🌐
Social Intents
help.socialintents.com › social intents knowledge base › ai chatbots › how to find your anthropic claude api key
How to Find Your Anthropic Claude API Key - Social Intents Knowledge Base
In this article, we're going to show you how to easily generate an Anthropic Claude API Key to quickly integrate the Claude AI models into your projects and applications. With your Claude API Key, you can access Claude’s conversational AI capabilities through their secure, developer-friendly API and console
🌐
Reddit
reddit.com › r/claudeai › does anyone know how to configure claude code with just an api key?
r/ClaudeAI on Reddit: Does anyone know how to configure claude code with just an API key?
August 21, 2025 -

I've been trying to do something like `export ANTHROPIC_API_KEY=sk...` to set an api key for claude code but when I do I get a message telling me to either do `claude /logout` or unset the api key.

When I run `claude /logout` I am prompted to log back in and authenticate with anthropic. It seems as though I cannot just set an api key and use claude code. They are forcing me to login.

Does anyone know if I am mistaken or if there's a way around this?

Top answer
1 of 3
1
if you are using an API key for claude then you are logging into https://console.anthropic.com/ and pay for token usage. Now if you are logging into claude code you are using your chat subscription. This is using an Oauth bearer token and connecting to their beta backend. https://api.anthropic.com/v1/messages?beta=true . Now Oauth and client has their own client registered to only have its ID as allowed and authorized. so you cant really use the API and sdk, only claude cli with your suscription. Now if anyone wants to hack it any more here is some info: Claude Code auth flow in a nutshell: OAuth 2.0 with PKCE against console.anthropic.com → redirects to http://localhost:54545/callback and exchanges code for a bearer token. Client ID is hardcoded & locked → only the official Claude Code CLI can complete this flow. Tokens stored via keytar in your OS keychain (Keychain on macOS, Credential Manager on Windows, Secret Service on Linux). API calls go to https://api.anthropic.com/v1/messages?beta=true with headers like:Authorization: Bearer [access_token] anthropic-version: 2023-06-01 anthropic-beta: claude-code-20250219,... Responses stream back as Server-Sent Events (message_start, content_block_delta, message_stop). There’s also telemetry to statsig.anthropic.com for usage analytics, plus unified rate limiting headers in responses. Bottom line: API key = metered billing; Claude Code login = chat sub → OAuth → CLI-only access.
2 of 3
1
~/.claude-wrapper #!/bin/bash unset ANTHROPIC_API_KEY NODENV_VERSION=23.9.0 $HOME/.npm-global/bin/claude "$@" ~/.bash_aliases claude() { $HOME/.claude-wrapper "$@" }
Find elsewhere
🌐
Reddit
reddit.com › r/claudeai › how can i change the api key used by claude code? i see no way in the config
r/ClaudeAI on Reddit: How can I change the API Key used by Claude Code? I see no way in the config
March 6, 2025 - Claude code and use this command: /login You'll be directed through the web authentication. You can also use /logout ... Asked the AI, docs and the support team. No response from them yet. ... "Follow the one-time OAuth process with your Console account. You’ll need active billing at console.anthropic.com." ... I've spent all of about 30 minutes messing with this but it does not look like there's a way to throw in an artibrary key...
🌐
Zapier
zapier.com › productivity › app tips
Claude API: How to get a key and use the API
July 8, 2024 - In this guide, I'll give you a step-by-step tutorial on how to get a Claude API key, call the API, and get the responses where you want them.
🌐
Claude AI Hub
claudeaihub.com › claude-api-key
Claude API Key Guide | How to Get and Use It | Claude AI Hub
November 5, 2024 - You can obtain a Claude AI API key by creating an account on the Anthropic website, subscribing to a plan, and generating the key through the API console.
🌐
Claude API Reference
platform.claude.com › docs › en › api › admin › api_keys › retrieve
Get Api Key - Claude API Reference
Update Api Key · Usage Report · Cost Report · Completions · Create a Text Completion · Support & configuration · Rate limitsService tiersVersionsIP addressesSupported regionsOpenAI SDK compatibility · Console · Log in · Api keys · Retrieve · Copy page ·
🌐
Anthropic
anthropic.com › learn › build-with-claude
Anthropic Academy: Claude API Development Guide
Resources to help you get started with Claude, whether you're hacking with us at an event or on your own · Set up your developer account and generate API keys on the Anthropic Console.
🌐
Claude
support.claude.com › en › collections › 5370014-claude-api-and-console
Claude API and Console | Claude Help Center
I’ve been using other large language models (LLMs) to power my product. How should I alter my prompts when switching to Claude? API Key Best Practices: Keeping Your Keys Safe and Secure
🌐
Claude
support.claude.com › en › articles › 12304248-managing-api-key-environment-variables-in-claude-code
Managing API Key Environment Variables in Claude Code | Claude Help Center
When an API key is set as an environment variable, you'll be charged via API pay-as-you-go rates using the API account associated with that key. This happens even if you're logged into Claude Code with a claude.ai subscription or a different Console account.
🌐
Make
make.com › en › blog › claude-api-key
Claude API | How to Get an API Key and Start Using It | Make
March 7, 2025 - To useget started with the Claude API, create an Anthropic Console account. Then, in your account: Generate an Anthropic API key to access the Claude API.
🌐
Claude Docs
platform.claude.com › docs › en › api › overview
API Overview - Claude Docs
The API is made available via the web Console. You can use the Workbench to try out the API in the browser and then generate API keys in Account Settings.