AI tools

Set an AI API key in your terminal

Save your Anthropic, OpenAI or Gemini API key once, so AI tools and scripts can find it.

Fill these in once
  1. Save the key in your shell settings and load it
    Terminal
    echo 'export ANTHROPIC_API_KEY="paste-your-key-here"' >> ~/.zshrc
    source ~/.zshrc

    Keep keys out of your code and out of Git. Anyone with the key can spend on your account.

Check it worked

  1. Check it's set, without printing it
    Terminal
    [ -n "$ANTHROPIC_API_KEY" ] && echo "ANTHROPIC_API_KEY is set"
Undo it
  1. Remove it
    Terminal
    sed -i '' '/^export ANTHROPIC_API_KEY=/d' ~/.zshrc && unset ANTHROPIC_API_KEY
  1. Save the key in your shell settings and load it
    Terminal · Ubuntu / Debian
    echo 'export ANTHROPIC_API_KEY="paste-your-key-here"' >> ~/.bashrc
    source ~/.bashrc

    Keep keys out of your code and out of Git. Anyone with the key can spend on your account.

Check it worked

  1. Check it's set, without printing it
    Terminal · Ubuntu / Debian
    [ -n "$ANTHROPIC_API_KEY" ] && echo "ANTHROPIC_API_KEY is set"
Undo it
  1. Remove it
    Terminal · Ubuntu / Debian
    sed -i '/^export ANTHROPIC_API_KEY=/d' ~/.bashrc && unset ANTHROPIC_API_KEY
  1. Save the key for your user
    PowerShell
    [Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", "paste-your-key-here", "User")

    Close and reopen PowerShell afterwards. Keep keys out of your code and out of Git.

Check it worked

  1. Check it's set, without printing it
    PowerShell
    if ([Environment]::GetEnvironmentVariable("ANTHROPIC_API_KEY", "User")) { "ANTHROPIC_API_KEY is set" }
Undo it
  1. Remove it
    PowerShell
    [Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", $null, "User")