All posts

Setting Up API Key Authentication for Claude Code in VS Code

How to run Claude Code in VS Code with your own Anthropic API key on macOS — export it in .zshrc and launch from the terminal so the editor inherits it.

  • #claude-code
  • #vscode
  • #tooling

Claude Code is Anthropic’s official CLI and IDE extension for Claude. If you’d rather authenticate with your own Anthropic API key instead of signing in through a Claude subscription, here’s how to wire it up on macOS so VS Code picks it up automatically.

Prerequisites

  • macOS with zsh as the default shell
  • VS Code installed (with code command available in your PATH)
  • The Claude Code extension installed in VS Code
  • An Anthropic account with billing enabled

Step 1: Get your API key

Go to the Anthropic Console dashboard:

👉 https://platform.claude.com/dashboard

From the API Keys section, click Create Key, give it a recognizable name (e.g. claude-code-local), and copy the key value. You will only see the full key once — store it somewhere safe before closing the dialog.

⚠️ Treat this key like a password. Anyone with it can make API calls billed to your account.

Step 2: Export the key in your shell

Open your ~/.zshrc file in any editor and add the following line at the bottom:

export ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxxxxxxxxxx

Replace the placeholder with the actual key you copied. Save the file, then reload your shell so the variable is picked up:

source ~/.zshrc

Verify it’s set:

echo $ANTHROPIC_API_KEY

You should see your key printed back.

Step 3: Launch VS Code from the terminal

This step matters. VS Code only inherits ANTHROPIC_API_KEY when it’s launched from a shell that already has the variable exported. Open a fresh terminal and run:

cd /path/to/your/project
code .

If you launch VS Code from Spotlight or the Dock instead, it may not see the environment variable and Claude Code will prompt you to log in.

Step 4: Confirm Claude Code is using the key

Open the Claude Code panel in VS Code. If the key was detected correctly, you should land directly in a working session without a login screen. You can also open a terminal inside VS Code and run:

echo $ANTHROPIC_API_KEY

to double-check the variable made it through.

Troubleshooting

Claude Code still asks me to log in. VS Code was probably launched outside your shell. Quit it fully (Cmd+Q, not just closing the window) and relaunch via code . from the terminal.

echo $ANTHROPIC_API_KEY prints nothing inside VS Code. Either .zshrc didn’t run, or VS Code’s integrated terminal is using a different shell profile. Check Settings → Terminal › Integrated › Default Profile: osx and make sure it’s set to zsh.

I want this only for a specific project. Skip .zshrc and create a .envrc (with direnv) or a .env file in the project directory instead. That keeps the key scoped to one workspace rather than every shell session.

Wrapping up

That’s the whole flow: grab the key, export it in .zshrc, and launch VS Code from a terminal so it inherits the environment. Once that’s in place, Claude Code runs against your own API key and you can track usage directly in the Anthropic Console.