---
title: "Claude Code API key: How to get, configure and secure access"
description: "Learn how to get, configure, and secure your Claude Code API key properly. Follow our expert guide to manage Anthropic credentials and avoid security risks."
---

# Claude Code API key: How to get, configure and secure access

Published: 2026-09-16T01:58:07.293Z

Author: [Goon Nguyen](/author/goon)

Reading time: 16 minutes

Learn how to get, configure, and secure your Claude Code API key properly. Follow our expert guide to manage Anthropic credentials and avoid security risks.

# Claude Code API key: How to get, configure, and secure it

A Claude Code API key is an Anthropic credential set via the `ANTHROPIC\_API\_KEY` environment variable. Depending on your plan, you may also use account-based authentication. This guide covers authentication options, key creation, cross-platform configuration (macOS, Linux, Windows), billing, secure storage, verification, key rotation, and troubleshooting.

![Image](https://cdn.agentkit.best/blog/cfai-media/bcb4a38eb3d200636bdcd70e9b3957d7c6a83c5247ba91c7673259747e51606b-60f0e497b271f793ad10a488f1f68965a7c828831e433a2214a2d77b1c37c12a.jpg)

> **Security warning:** Never paste an Anthropic API key into source code, prompts, screenshots, repositories, MCP configurations, support tickets, or public messages. Treat any exposed credential as compromised.

## Do you need an API key for Claude Code?

You may need an API key when using [Claude Code](https://agentkit.best/blog/claude-code-guides/what-is-claude-code-a-complete-guide-to-the-ai-coding-tool) through Anthropic’s metered API access. Individual interactive use may also support account-based authentication, depending on the current product flow, plan eligibility, organization policy, and deployment context.

Choose the appropriate authentication method:

Account sign-in, API credentials, and application subscriptions are related but not interchangeable concepts. The correct choice depends on who owns the credential, where it runs, and which billing context applies.

![Image](https://cdn.agentkit.best/blog/cfai-media/a3c23fd995398e7a9dc6836e626a201f68e69df3f7f796f3c3104da24c2b06a2-7cd2d52c6b8bcee6c5fcaaa3e8304aeb28d6eac5034a720df79274428c161794.jpg)

## How to create an Anthropic API Key

Create Anthropic API credentials only through the official platform. Before creating the key, confirm the active organization or workspace, intended owner, workload, environment, scope, and expiration policy.

According to [Anthropic’s API key management documentation](https://platform.claude.com/docs/en/get-api-key), the standard process is:

Interface labels can change. Verify the current navigation before publishing internal setup documentation.

### Step 1 - Sign In to the Claude Console

Open `https://platform.claude.com/` directly or use a trusted bookmark. Avoid credential pages reached through unsolicited messages, advertisements, or unofficial tutorials.

After signing in, confirm that the correct organization or workspace is active. This check prevents developers from creating a credential under the wrong billing account or access boundary.

### Step 2 - Open Settings → API Keys

Navigate to **Settings → API keys**. The page may display metadata for existing credentials, such as their names, owners, creation dates, expiration details, or redacted hints.

Use a name that identifies the credential without revealing the secret:

`owner-workload-environment
`

Examples include `alex-local-development` or `ci-documentation-production`. Do not include the API key itself in the name.

If Create key is disabled, your organization role may not permit credential creation. Ask an administrator to review your role or create an approved workload credential. Do not bypass organization controls by using an unrelated personal account.

### Step 3 - Choose the Key Type and Scope

Anthropic’s current documentation describes the following key types:

Choose the narrowest practical scope. Consider:

Do not select broader access solely for convenience.

### Step 4 - Copy and Store the Key Immediately

The current official documentation states that the complete key is displayed only once during creation. Anthropic API keys currently begin with `sk-ant-`, but documentation and interface behavior should be rechecked periodically.

Use only a redacted representation in documentation:

`sk-ant-...REDACTED
`

Store the credential in an approved:

Do not save it in a document, screenshot, prompt library, repository, chat message, or issue tracker. If the original value is lost, create a replacement and revoke the unused credential rather than attempting to reconstruct it.

![Image](https://cdn.agentkit.best/blog/cfai-media/d0a0b4b8caf5cec3462238fa5879617f86d96e8766ec22c557eb48fcbc79e00c-6a5200b63ec6981cb967398976aa26f4bf4975b0812a5b459481ba9c0916502c.jpg)

## Set `ANTHROPIC\_API\_KEY` on macOS, Linux and Windows

An environment variable is a named value made available to an application at runtime. In an API-key-based setup, `ANTHROPIC\_API\_KEY` is the exact variable name recognized by Anthropic tooling and client libraries.

A temporary variable exists only in the current process tree. A persistent variable becomes available to newly started processes after the relevant configuration is loaded.

> Replace the redacted placeholder locally. Never place a real credential in documentation, screenshots, shell history, issue trackers, or source control.

### macOS and Linux - Current terminal session

Run this command in Bash or Zsh:

`export ANTHROPIC\_API\_KEY="sk-ant-...REDACTED"
`

This setting lasts for the current terminal session. Processes launched from that shell can inherit the variable.

Start Claude Code from the same terminal. Closing the terminal removes the temporary setting.

### macOS and Linux - Persistent Shell Sessions

First, identify your active shell:

`echo $SHELL
`

Common profile locations include:

A persistent variable added to the appropriate profile becomes available after you open a new terminal or reload the configuration.

However, storing a secret directly in a shell profile creates security risks. The value may be exposed through backups, screenshots, broad file permissions, support bundles, or accidental sharing.

For team and production environments, use a secrets manager that injects the value at runtime. A shell profile provides convenience, not the protections of dedicated [secrets](https://agentkit.best/blog/claude-code-guides/claude-code-secrets-how-to-secure-env-files-and-credentials) management.

### Windows PowerShell - Current session

Run the following command in PowerShell:

`$env:ANTHROPIC\_API\_KEY = "sk-ant-...REDACTED"
`

The variable applies only to the active PowerShell session. Launch Claude Code from that PowerShell window so the new process inherits the value. Closing PowerShell removes the temporary setting.

### Windows - Persistent user environment variable

Use the Windows interface rather than adding the value through source files:

An existing application does not automatically receive newly created persistent variables. It must be restarted under a process that can access the updated environment.

User [environment variables](https://agentkit.best/blog/claude-code-guides/claude-code-environment-variables-setup-and-best-practices) may also be accessible to other processes running under the same account. They are not a substitute for a dedicated secrets manager on shared or managed devices.

### CI/CD and shared agent workloads

A CI/CD pipeline does not inherit secrets from a developer’s computer. The secret must be configured in the automation platform and explicitly mapped to the required runtime environment.

![Image](https://cdn.agentkit.best/blog/cfai-media/60a0cac584e852ad8310e035d24e2077444bb5d60c0408f7439c380dff867a31-3627516c2aee1d246babd00cb382955fc5cc785cdb80f58442c0480c578c5f96.jpg)

## Verify Claude Code authentication without exposing the key

Verify that the environment variable exists without printing its value. Variable detection proves that the current shell can see a value, but it does not prove that the credential is valid, active, authorized, or billable.

### Safe check on macOS or Linux

`if \[ -n "$ANTHROPIC\_API\_KEY" \]; then
  echo "ANTHROPIC\_API\_KEY is set"
else
  echo "ANTHROPIC\_API\_KEY is not set"
fi
`

Do not use commands that print the complete value, including `echo $ANTHROPIC\_API\_KEY`. Terminal output can enter logs, screenshots, recordings, clipboard history, and support transcripts.

### Safe check in Windows PowerShell

`if ($env:ANTHROPIC\_API\_KEY) {
  "ANTHROPIC\_API\_KEY is set"
} else {
  "ANTHROPIC\_API\_KEY is not set"
}
`

Open a new PowerShell window if you recently created a persistent user variable.

### Final authentication check

A set environment variable does not confirm:

> ***Validation flow:**** Variable exists → Process inherits variable → Credential is accepted → Usage is authorized.*

## Claude subscription vs. API key billing

Do not assume a paid Claude app subscription includes Anthropic API access. Claude application plans and API access generally operate in separate product and billing contexts unless current official terms explicitly state otherwise.

Review [Anthropic’s current pricing information](https://www.anthropic.com/pricing) and the billing settings for the relevant API organization before running high-volume workloads.

Account authentication and API authentication compared:

API cost depends on current model pricing, token consumption, organization settings, and applicable usage controls. Authentication success alone does not establish that sufficient billing access is available.

Before sustained usage:

## Secure, rotate and revoke Claude API keys

Every credential needs a documented owner, workload, environment, scope, storage location, expiration policy, and revocation path. Shared production systems should not depend on an employee’s personal credential where a workload-owned option is available.

> *Use this lifecycle: ****Create → Name → Scope → Store → Monitor → Rotate → Revoke***

### Where to store the key

Reusable configuration should reference `ANTHROPIC\_API\_KEY`, not contain its value. This separation keeps workflows portable and prevents source-controlled configuration from becoming a secrets repository.

### What to do if a key is exposed

Deleting a key from the latest Git commit is insufficient if the value remains in repository history, forks, build logs, or caches. Revocation invalidates the credential; repository cleanup only reduces continued exposure.

Rotation is also incomplete until every dependent process uses the replacement. Test local, staging, production, scheduled, and CI/CD workloads before closing the incident.

### Standardizing team and agent configurations

Skills, [plugins](https://agentkit.best/blog/claude-code-guides/claude-code-plugins-a-guide-to-workflows-setup-and-safety), MCP settings, and workflow definitions should contain variable references rather than secret values. Credentials should be injected at runtime through the approved environment.

AgentKit can optionally standardize reusable Claude Code configurations and repeatable security checks. It is not required for Claude Code, does not issue Anthropic credentials, and does not replace Anthropic’s authentication or billing controls.

![Image](https://cdn.agentkit.best/blog/cfai-media/e20a5a7c3cad5b8dcb4f17eef562b0cef53ca97f2b5e4806775e620ff6dd84e8-b7bf0522e1985ef10acda786ab59abaa3910fe83543bb54a0190bd71a1dcf6a5.jpg)

## Troubleshoot common Claude Code API key errors

Troubleshoot in a low-risk order: Check the variable name, process inheritance, credential status, organization or workspace, permissions, billing controls, CI/CD mapping, and current authentication behavior. Never expose the complete key while diagnosing an error.

### Authentication troubleshooting table

Do not recreate the credential as the first response to every problem. A replacement key will not fix an incorrect variable name, stale terminal, missing CI mapping, insufficient organization role, or unavailable billing.

### Safe escalation procedure

When official documentation does not resolve the issue:

Consult current Anthropic documentation before escalating. Contact the organization administrator for permission or workspace problems.

If logs, screenshots, or support messages might contain the credential, revoke the key before sharing any diagnostic material.

## Claude Code API key setup: Quick reference

## Configure your Claude Code API key safely

A safe Claude Code API key setup starts by confirming whether account sign-in or API authentication applies. If a key is required, create it on the official platform, assign appropriate ownership and scope, store it securely, and configure `ANTHROPIC\_API\_KEY` for the intended process.

Verify detection without printing the secret. Remember that a Claude subscription and API billing are not automatically interchangeable. Shared workloads should use workload-owned credentials where available.

If a key appears in Git, logs, prompts, screenshots, or support material, revoke it immediately. AgentKit remains an optional way to standardize reusable Claude Code configurations while keeping runtime credentials separate from skills, plugins, [MCP settings](https://agentkit.best/blog/claude-code-guides/claude-code-mcp-connect-servers-setup-guide-and-fix-errors), and workflows.

## Frequently asked questions

### Do I need a separate API key for Claude Code?

Not necessarily. The required authentication method depends on your specific workflow, organization policy, and plan. While some setups require an Anthropic API key configured via `ANTHROPIC\_API\_KEY`, others may support account-based authentication. Always check the official Claude Code documentation to see which method currently applies to your setup.

### Where can I generate an Anthropic API key?

You can generate API keys through the official Claude Console. Navigate to **Settings → API keys**, select the appropriate workspace or organization, and click **Create key**. Ensure you choose the correct key type-such as a personal or service account key-based on your specific workload requirements.

### How do I safely configure the `ANTHROPIC\_API\_KEY` environment variable?

You can set it for your current session using `export ANTHROPIC\_API\_KEY="sk-ant-...REDACTED"` (macOS/Linux) or `$env:ANTHROPIC\_API\_KEY = "sk-ant-...REDACTED"` (PowerShell). For persistent access, add the variable to your shell profile or Windows user environment variables. Always use a dedicated secrets manager or CI/CD secret vault for production workloads.

### How can I verify that Claude Code detects my API key?

You can verify detection by running a conditional check in your terminal (e.g., `if \[ -n "$ANTHROPIC\_API\_KEY" \]; then echo "Set"; fi`). This proves the variable is present but does not confirm key validity or authorization. Avoid using `echo` or `print` commands on the actual key, as this risks exposing your secret in plain text.

### Does a Claude application subscription include API-key usage?

No, Claude application subscriptions and API-key usage are generally separate billing contexts. API-key usage is metered and charged based on your organization's API billing configuration. Do not assume that a paid Claude Pro or Team subscription covers your API token consumption; always review the official billing and usage controls in your console.

### What should I do if my API key is exposed?

If you suspect your key has been exposed, revoke it immediately in the Claude Console. Create a new key to replace the compromised one and update all dependent systems. Additionally, ensure you remove the exposed value from repository history, logs, cached artifacts, and any shared communications to prevent further unauthorized access.

**Read more:**

## Conclusion

In summary, setting up a Claude Code API key requires choosing the right authentication method, configuring the ANTHROPIC\_API\_KEY variable, and strictly keeping credentials out of your source code. By storing keys in secure managers, monitoring usage, and knowing how to rotate or revoke exposed credentials immediately, you can ensure a seamless and safe integration across local, team, and CI/CD environments.
