An anthropic api key lets your application authenticate requests to Anthropic’s developer API. Create one in the Anthropic Console, configure API billing if required, store the key as an environment variable, and use the official Python SDK to send a small test request. A Claude chat subscription is separate from API access, so don’t assume a paid chat plan covers your application’s usage.
Let’s start with the smallest useful version: one local request, no framework, and no secrets in source control. You’ll need a terminal, Python, a Console account, and access to a model available to that account.
Create an anthropic api key in the Console
Open console.anthropic.com and sign in or create an account. In the Console, find the API keys section, create a key, and copy it into a secure location. Console navigation and onboarding requirements can change, so use the Console’s search or Anthropic’s documentation if the labels have moved.
Check the billing section before running a request. API usage is generally billed separately from Claude subscriptions, and an account may need a payment method or available credits. Review the displayed pricing and any spending controls before generating traffic. Costs vary by model and usage; a successful test is not proof that an automated workload will be inexpensive.
Treat the key like a password. Don’t paste it into a ticket, commit it to Git, or send it to someone asking to debug your code. If a team needs access, use separate keys where practical so you can revoke a compromised credential without interrupting every application.
Set up a local Python environment
Create a project directory and a virtual environment. On macOS or Linux, run python3 -m venv .venv, then activate it with source .venv/bin/activate. On Windows PowerShell, run py -m venv .venv followed by .venv\Scripts\Activate.ps1. Install the official SDK with python -m pip install anthropic.

Set the anthropic api key as ANTHROPIC_API_KEY in your terminal environment. On macOS or Linux, use export ANTHROPIC_API_KEY='your-key-here'. In PowerShell, use $env:ANTHROPIC_API_KEY='your-key-here'. Replace the placeholder with the actual key; don’t include the quotes as part of it.
These commands affect the current shell session. Avoid saving the literal command in a shared script or shell history; for regular development, prefer your operating system’s credential store or a secret manager. If you use a local environment file, exclude it from Git and never treat that exclusion as a substitute for controlling access to the file.
Next, choose an exact model ID from Anthropic’s model documentation or your Console and set ANTHROPIC_MODEL to that value in the same way. Model availability and IDs change, so an old example string is a poor dependency for a new integration.
Send a test request
Run the following as a single terminal command after setting both environment variables:
python -c "import os, anthropic; client = anthropic.Anthropic(); response = client.messages.create(model=os.environ['ANTHROPIC_MODEL'], max_tokens=64, messages=[{'role': 'user', 'content': 'Reply with one short greeting.'}]); print(response.content)"
The SDK reads ANTHROPIC_API_KEY from the environment. The request sends a short user message and limits the maximum output to 64 tokens. Printing the content blocks should show a text response, although the exact greeting will vary. The model ID, not the API key, determines which model receives the request.
This is a connectivity check, not an application architecture. It doesn’t validate structured output, retry failures, or keep private data out of prompts. Once the request works, record the SDK version with python -m pip show anthropic and pin your project’s dependency after verifying it works with your application.
Troubleshoot an anthropic api key request
An authentication error is the first thing to investigate when the server rejects a credential. Confirm that the variable exists in the same shell as the Python process, that the key hasn’t been revoked, and that you copied the complete value. Don’t print the key in logs to diagnose it; checking whether the variable is set is enough for a first pass.

A billing or credit error calls for a different fix: inspect the Console’s billing and usage settings. Generating another key won’t resolve an account-level payment problem. If you get a model-not-found or access error, check the exact model ID and whether that model is available to your account. A typo in ANTHROPIC_MODEL can look like a service problem when it’s just configuration.
Rate-limit and temporary server errors need controlled retries. Use exponential backoff with a cap, respect any retry guidance returned by the API, and avoid retrying authentication failures indefinitely. Check the SDK’s retry behavior before adding your own retry loop; stacking both can produce more attempts than you intended.
Protect the key in production
An anthropic api key belongs on a trusted server, not in browser JavaScript, a mobile app bundle, or a public repository. Have your client call your backend, and have the backend authenticate the user, enforce limits, and make the Anthropic request. Hiding a key in a compiled app does not make it secret.
Inject production credentials through your hosting provider’s secret management system or a dedicated secret manager. Restrict who can read and change them, and use separate credentials for development and production where your setup allows. Monitor usage and spending so an unexpected increase gets attention before it becomes a larger bill.
If you suspect exposure, revoke the affected key in the Console, create a replacement, update the secret in your deployment environment, and restart or redeploy affected services as needed. Review usage around the incident and remove the secret from logs or shared files. Deleting a committed key from the latest revision alone does not remove it from Git history.
Finally, test the failure paths: an unset variable, a revoked key, an unavailable model, and a rate-limited request. Your app should return a useful error without exposing credentials or raw sensitive prompts. Build the smallest useful version, then test what breaks.

No feedback yet — submit the first.