Setting Up Claude Code: Installation, API Endpoint Configuration, and Connectivity Testing

    This guide walks through installing Claude Code, a command-line coding assistant, and configuring its API endpoint, model identifier, and credentials. It explains how to separate the local tool setup from the remote API connection, how to store API keys safely, and how to confirm connectivity with a minimal curl request. It also covers common HTTP error codes and network issues to help you troubleshoot.

    1. Understand What Each Part Does

    A command-line coding assistant is an interactive program. It reads your project directory, assembles prompts, runs commands, and displays the results. The large language model on a remote server performs the actual reasoning. Setup therefore has two steps. First, install the tool on your machine and complete its initialization. Second, tell the tool where to call the model and provide your credentials.

    The two steps call for different troubleshooting approaches. If the tool fails to install, the problem is usually on your local machine. If requests fail, the cause is most likely the API endpoint, the key, or the model identifier. Checking these items in order is faster than reinstalling repeatedly.

    This article uses Claude Code as an example. Anthropic publicly released Claude Code as a command-line coding assistant. It runs on macOS, Linux, and Windows. If you use a different tool, the overall steps are similar. You only need to replace the installation command and the environment variable names. For exact names, refer to the official documentation for that tool.

    2. Check Your Environment

    Before you start, confirm the following:

    1. Terminal: On macOS, use Terminal or iTerm. On Windows, use PowerShell. On Linux, use the built-in terminal.
    2. Node.js: If you install through npm, you need Node.js version 18 or later. Run node --version to check your version.
    3. Network: Both installation and API calls require access to external services. If your organization uses a proxy, use the settings provided by your network administrator. Do not bypass your company’s or school’s network policies on your own.
    4. Editor: Choose an editor with an integrated terminal. This lets you view code and call the assistant in the same window.

    If node or npm does not run correctly, every installation step that follows will fail. Confirm that both work before you continue.

    3. Install and Verify the Version

    3.1 Install with npm

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

    If you see a permission error, do not add sudo directly. A safer approach is to use a Node version manager such as nvm, which installs global packages in your user directory. The official documentation also describes other options, such as the native installer. Choose one method.

    3.2 Restart the Terminal and Check the Version

    Close the current terminal, open a new window, and run:

    1. claude --version

    If the command prints a version number, the executable is in your system path. If you see a “command not found” error, run npm config get prefix to find your global installation directory. Then confirm that its bin directory is in your PATH. On Windows, global commands are usually located under that prefix directory. After you change PATH, restart the terminal again.

    4. Get an API Key and Confirm the API Protocol

    4.1 Choose a Service Source

    Model API services generally come from two types of sources:

    • Direct from the official provider: Billing, data handling, and service terms are the clearest. If your work involves internal code, this option is preferable.
    • Third-party relay: Pricing may differ, but your requests pass through the relay operator. Before you use a relay, confirm its data retention period, log retention practices, uptime commitments, and refund policy.

    4.2 Confirm the API Protocol

    Claude Code uses the Messages API protocol from Anthropic. If you connect through a relay or gateway, confirm that it supports the same protocol. There are three common cases:

    1. The provider offers this protocol directly.
    2. The provider offers a different format, and a gateway converts requests. In this case, the tool only needs the gateway address.
    3. The tool supports a custom API endpoint, and the request format matches the protocol.

    If the protocols do not match, the calls will fail even when the key and address are correct. To find this information, search the provider’s documentation for terms such as “API protocol,” “compatibility,” or “Anthropic.”

    4.3 Generate and Store the Key

    Keys are usually shown only once, at creation. After you close the page, you cannot view the key again. Follow these practices:

    • Store the key immediately in a password manager or your team’s secrets management system.
    • Create a separate key for each purpose so you can deactivate them individually.
    • Never put keys in code repositories, log files, chat logs, or screenshots.
    • Rotate keys regularly. If team members change roles or you suspect a leak, revoke the old key immediately.

    5. Configure Environment Variables

    5.1 Know the Variable Names

    Each tool reads different variable names. The table below lists common cases:

    Variable Purpose When to Use
    ANTHROPIC_API_KEY API key When Claude Code authenticates with a key
    ANTHROPIC_AUTH_TOKEN Bearer token When a gateway requires the Authorization header
    ANTHROPIC_BASE_URL API endpoint Set when using a relay or gateway. Leave unset for direct connections.
    ANTHROPIC_MODEL Default model identifier Must exactly match the model list from your provider
    OPENAI_API_KEY, OPENAI_BASE_URL For tools that use the OpenAI format Used by other tools. Claude Code does not read these.

    If a tool does not read these variables, check its configuration file or initialization command, and place the parameters where that tool expects them. Set the permissions of any configuration file so that only the current user can read and write it.

    5.2 Save Non-Sensitive Settings (macOS and Linux)

    The steps in this section apply to macOS and Linux terminals. Windows users should skip to Section 5.4.

    Open ~/.zshrc in your editor (or ~/.bashrc if you use bash), and add these two lines at the end:

    1. export ANTHROPIC_BASE_URL="<provider-supplied-base-url>"
    2. export ANTHROPIC_MODEL="<model-identifier-from-provider-docs>"

    If you connect directly to the official service, you can leave ANTHROPIC_BASE_URL unset. The model identifier must match the documentation exactly, including capitalization, version numbers, and hyphens. Otherwise, the API returns an error.

    5.3 Store the Key Separately

    Do not write the key into ~/.zshrc. That file is often synced to dotfiles repositories. Store the key in a separate file:

    1. mkdir -p ~/.config/ai-tool
    2. touch ~/.config/ai-tool/env
    3. chmod 600 ~/.config/ai-tool/env

    Open ~/.config/ai-tool/env in your editor and add one line:

    1. export ANTHROPIC_API_KEY="<your-api-key>"

    Then add the following line at the end of ~/.zshrc, so the terminal loads the key file on startup:

    1. [ -f ~/.config/ai-tool/env ] && source ~/.config/ai-tool/env

    Run source ~/.zshrc, or open a new terminal, to apply the changes.

    To verify, check only that the variables exist. Do not print the key:

    1. printenv ANTHROPIC_BASE_URL ANTHROPIC_MODEL
    2. [ -n "$ANTHROPIC_API_KEY" ] && echo "ANTHROPIC_API_KEY is set"
    3. ls -l ~/.config/ai-tool/env

    The output of ls -l should begin with -rw-------, which means only the owner can read and write the file.

    5.4 Configure Windows PowerShell

    You can write non-sensitive settings to user-level environment variables with these commands:

    1. [Environment]::SetEnvironmentVariable("ANTHROPIC_BASE_URL", "<provider-supplied-base-url>", "User")
    2. [Environment]::SetEnvironmentVariable("ANTHROPIC_MODEL", "<model-identifier-from-provider-docs>", "User")

    Do not set the key with a command. PowerShell saves commands in its history. Instead, open System Properties, go to Environment Variables, create ANTHROPIC_API_KEY under User variables, and paste the key there.

    After you finish, restart the terminal and your editor. The new variables take effect only after a restart. To verify the key, run the following command. It returns only True or False:

    1. [Environment]::GetEnvironmentVariable("ANTHROPIC_API_KEY", "User") -ne $null

    6. Test Connectivity with a Minimal Request

    Before you start the tool, call the API directly with curl. Curl does not go through Claude Code. This lets you determine whether the problem lies in the network, the key, or the tool itself.

    The example below follows the Anthropic Messages protocol. If ANTHROPIC_BASE_URL is not set, it uses the official endpoint:

    1. curl "${ANTHROPIC_BASE_URL:-https://api.anthropic.com}/v1/messages" \
    2. -H "x-api-key: $ANTHROPIC_API_KEY" \
    3. -H "anthropic-version: 2023-06-01" \
    4. -H "content-type: application/json" \
    5. -d "{\"model\":\"$ANTHROPIC_MODEL\",\"max_tokens\":16,\"messages\":[{\"role\":\"user\",\"content\":\"ping\"}]}"

    When the request succeeds, the terminal returns a JSON response. Its type is message, and its content array contains a block with type set to text. The structure looks like this:

    1. {"type":"message","role":"assistant","content":[{"type":"text","text":"..."}], ...}

    Keep these points in mind:

    • Do not add the -v flag to curl. Verbose output prints request headers, which include your key.
    • If the relay requires an Authorization: Bearer header, use the form that corresponds to ANTHROPIC_AUTH_TOKEN instead. Follow your provider’s documentation for the exact format.
    • If the request path or headers differ from the documentation, adjust them according to your provider’s instructions.

    After the connectivity test passes, go to your project directory and start the tool:

    1. cd <your-project-directory>
    2. claude

    The first time you enter a directory, the tool asks whether to trust the files in it. Grant trust only for projects whose contents you have reviewed. For a code repository from an unknown source, look at the directory structure before you grant access. To exit, type /exit. If the program stops responding, press Ctrl+C, and press it twice if necessary.

    7. Troubleshoot Common Errors

    7.1 HTTP Status Code Reference

    Status Code Common Causes What to Check
    400 The request body is malformed, or the model identifier is not accepted Check the JSON fields. Verify the spelling and capitalization of ANTHROPIC_MODEL.
    401 The key is invalid or revoked, or the authentication header name is wrong Confirm that the key is still active. Confirm whether the endpoint requires the x-api-key header or the Authorization header.
    403 The account lacks permission for the model, or it does not meet eligibility, regional availability, or plan requirements Check your account against the provider’s eligibility requirements. This article does not describe ways to bypass these checks. If the problem persists, contact the provider.
    404 The endpoint path is wrong, or the model identifier does not exist Check ANTHROPIC_BASE_URL and the /v1/messages path. Copy the model identifier from the provider’s model list.
    429 You have exceeded the request rate or quota limit Wait and try again. Check your account quota. Reduce the number of concurrent requests.
    5xx A server-side error or high load Wait and retry, increasing the interval between attempts each time. Check the provider’s status page.

    7.2 Network and Certificate Errors

    • curl returns exit code 28 (timeout): The network is unreachable, or the proxy settings are wrong. Check proxy variables such as HTTPS_PROXY. Enter the proxy address provided by your network administrator.
    • curl returns exit code 60 (certificate problem): This usually happens when a corporate proxy uses a self-signed certificate. Ask your administrator for the root certificate, and point the tool to it through its certificate configuration option. Do not disable certificate verification with -k, because that can expose your requests to interception.
    • The claude command is not found: Return to Section 3.2, and check that the global installation directory is in your PATH.
    • The tool reports that you are not authenticated after startup: Confirm that ANTHROPIC_API_KEY is set in the current terminal. After you change any configuration, open a new terminal.

    8. Final Checklist

    After you finish the setup, confirm each item:

    • claude --version prints a version number.
    • ls -l shows that the key file has 600 permissions, and the file is not inside any code repository.
    • printenv displays the endpoint URL and the model identifier.
    • The curl response JSON contains text content.
    • Running claude in a project directory lets you complete one full conversation.

    If all checks pass, the environment is ready for daily development. If you later change keys or models, update the corresponding variables, and run the minimal request in Section 6 again.