Getting Started with a Command-Line AI Coding Assistant: Environment Setup and API Key Configuration

    Learn how to set up a command-line AI coding assistant, including installing Node.js and Git, installing Claude Code via npm, configuring the ANTHROPIC_API_KEY environment variable, verifying your first run, and following key security practices.

    1. Decide Whether You Need a Command-Line Tool

    Web-based chat tools are good for conceptual questions. If you want to work inside a local project — reading code structure, running tests, inspecting error logs, and modifying multiple files — a command-line AI coding assistant is a better fit because it operates directly in your project directory.

    Common use cases include:

    • Diagnosing failing tests from their output and suggesting fixes
    • Explaining the call relationships of unfamiliar modules when reading legacy code
    • Generating drafts of scripts, API documentation, configuration files, and unit tests
    • Reviewing diffs in version control and helping write commit messages

    These tools can read and write local files and execute commands. Before using one, understand its permission scope and keep a human review step in the loop.

    2. Prepare the Base Environment

    Before installation, you need a runtime, a version control tool, and a terminal.

    2.1 Runtime Environment

    Requirements vary by tool, so check the official installation requirements of your target tool first. Claude Code is distributed via npm and requires Node.js; refer to the official documentation for the minimum supported version. Download the version marked LTS (Long-Term Support) from the official Node.js website and keep the default options during installation.

    After installation, run in your terminal:

    1. node -v
    2. npm -v

    If both commands print version numbers, your environment variables are configured correctly. If you get a “command not found” error, reopening the terminal or restarting the system usually fixes it.

    2.2 Version Control Tool

    An AI coding assistant uses Git to read commit history, generate diffs, and help write commit messages. Even if you don’t use other features, install Git and confirm the following command produces output:

    1. git --version

    2.3 Terminal

    There are three common choices:

    • The built-in system terminal, which requires no extra installation
    • Third-party terminals with multi-tab support, useful for managing several projects at once
    • The integrated terminal panel in your code editor, useful for running commands while viewing code

    Terminal choice affects experience, not functionality. If your team has a convention, follow it.

    3. Install Claude Code

    Prerequisite: Node.js is installed. Run the global install command:

    1. npm install -g @anthropic-ai/claude-code
    2. claude --version

    A printed version number means the installation succeeded.

    3.1 Handling Permission Issues

    On macOS or Linux, if the global install fails with an EACCES permission error, avoid using sudo directly, since that broadens the blast radius of mistakes. A safer approach is to point npm’s global directory to a location writable by your user:

    1. mkdir -p "$HOME/.npm-global"
    2. npm config set prefix "$HOME/.npm-global"

    Then open ~/.zshrc (the default shell on macOS) or ~/.bashrc in an editor and add this line:

    1. export PATH="$HOME/.npm-global/bin:$PATH"

    Reopen the terminal and run the install command again.

    On Windows, npm’s global directory is usually under the user directory and typically doesn’t require administrator privileges. If you hit a permission error, check whether that directory is managed by corporate policy rather than running the terminal as administrator.

    3.2 Using a Mirror When the Network Is Slow

    npm downloads packages from the official registry by default. On a slow network, you can temporarily switch to a trusted mirror. The following uses npmmirror as an example:

    1. npm config get registry
    2. npm config set registry https://registry.npmmirror.com
    3. npm install -g @anthropic-ai/claude-code
    4. npm config set registry https://registry.npmjs.org/

    The first line records your current registry; the last line switches back to the official source after installation. Only use a mirror designated by your team or from a trusted organization. Mirror package versions occasionally lag behind the official registry — if the installed version is noticeably outdated, switch back and retry.

    4. Obtain an API Key and Configure Environment Variables

    4.1 Getting an API Key

    Log in to the Anthropic Console and create a new key on the API Keys page. Recommended practices:

    • Create a separate key per device or per purpose so each can be revoked independently
    • Set usage limits if the platform supports them
    • Rotate keys regularly and revoke immediately if anything looks abnormal

    4.2 Configuring via Environment Variables

    The environment variable Claude Code reads is ANTHROPIC_API_KEY. Never write the key into project code, configuration files, or commit history.

    On macOS or Linux, set it for the current session first:

    1. export ANTHROPIC_API_KEY="your-key"

    For persistence, open ~/.zshrc or ~/.bashrc in an editor, add the same line manually, and save. Editing the file directly keeps the key out of your shell history. Then run source ~/.zshrc or reopen the terminal.

    In Windows PowerShell, for the current session:

    1. $env:ANTHROPIC_API_KEY = "your-key"

    For persistence, open “System Properties,” go to “Environment Variables,” and add ANTHROPIC_API_KEY under “User variables.” This GUI approach doesn’t write the key into command history. Reopen the terminal afterward.

    If your provider supplies a custom endpoint, set ANTHROPIC_BASE_URL according to its documentation. Always verify variable names against the official documentation rather than relying on memory.

    4.3 Key Security Notes

    • Copy the key from a password manager and paste it, instead of typing it in plain text.
    • Don’t expose the full key in screenshots, screen recordings, or support tickets.
    • If the project contains files like .env that hold keys, add them to .gitignore to prevent accidental commits.

    5. First Run and Verification

    Prepare a test project and make sure it’s under Git management. If it isn’t, run git init. Then enter the directory and start the tool:

    1. cd ~/projects/test-demo
    2. claude

    For the first session, verify the workflow with low-risk tasks:

    • Ask the tool to summarize the project’s directory structure
    • Have it explain a function’s inputs and outputs
    • Run the existing tests and ask it to summarize the failures

    Whenever the tool requests to read/write files or execute commands, read the specifics before confirming. After changes are made, review them with git diff and only commit once you’re satisfied. If uncommitted changes to a file should be discarded, you can use git restore <filename> — but this drops all uncommitted changes to that file, so confirm before running it.

    6. Security Practices

    An AI coding assistant’s efficiency comes from deep access to your project — and so does its risk. The following practices apply to individuals and teams:

    1. Least privilege: Start the tool only in the project directory that needs it. Don’t run it in your home directory or a root directory containing production credentials.
    2. Isolate sensitive data: Database connection strings, certificates, and private keys should live where the tool can’t read them, or be injected through a secure secrets management service.
    3. Human review: Review generated code, scripts, and configuration — especially anything involving permissions, network requests, or data deletion.
    4. Audit trail: Keep complete commit history so every automated change is traceable.
    5. Regular cleanup: Revoke keys you no longer use and uninstall tools you no longer need.

    If you suspect a key has been leaked, handle it in this order:

    1. Revoke the key in the console immediately.
    2. Create a new key and update the local environment variable.
    3. Review billing and usage records for abnormal calls.
    4. Check the project’s commit history to confirm the key never entered the repository. If it was committed, clean the repository history even after revocation.

    7. Troubleshooting

    • claude command not found: npm’s global directory isn’t on the system PATH. On macOS/Linux, run npm config get prefix and confirm the bin directory under the output path is on your PATH. On Windows, confirm npm’s global directory is in the user PATH. Reopen the terminal after changes.
    • Permission error (EACCES): Don’t use sudo; set up a user-writable global directory as described in Section 3.
    • Download timeouts: Run npm config get registry to check the current source. On a poor network, switch to a trusted mirror or check your proxy settings.
    • Authentication failures: First confirm the environment variable exists in the current terminal. On macOS/Linux run echo ${#ANTHROPIC_API_KEY}; in PowerShell run $env:ANTHROPIC_API_KEY.Length. An output of 0 means the variable isn’t set. These commands show only the length, never the key itself. Also check that the key was copied completely, hasn’t expired or been revoked, and that quota isn’t exhausted. Reopen the terminal after modifying environment variables.
    • Abnormal responses: Confirm the current directory is a project directory. Run claude --version to check the version and update to the latest stable release if needed: npm install -g @anthropic-ai/claude-code@latest.

    8. Summary

    Go through this checklist item by item; once everything passes, the environment is ready:

    • node -v, npm -v, and git --version all print version numbers
    • claude --version prints a version number
    • In a newly opened terminal, the ANTHROPIC_API_KEY length check is non-zero
    • A low-risk task was completed in a test project, such as summarizing the directory structure or running existing tests
    • The key does not appear in code, commit history, screenshots, or logs

    When switching to a similar tool later, you can follow the same approach: verify the runtime, version control, and terminal; install through the official channel; configure credentials via environment variables; validate with a low-risk task; and keep review and cleanup habits in place.