How to Use the Claude API: 5 Steps to Your First Request (2026)

Blog / Developer tools

How to Use the Claude API: 5 Steps to Your First Request (2026)

The Claude API is Anthropic's programmatic interface. You create an API key in the official developer console (Console), send prompts to the model with HTTP requests or an official SDK, and pay for what you use. It is a separate entry point from the claude.ai web app: accounts, billing, and quotas are not shared between the two. This guide walks through how to use the Claude API in order, from getting a key to your first successful response.

What is the difference between the Claude API and the web app?

The web app (claude.ai) is a ready-made chat product that you subscribe to and use within your plan's quota. The API is meant to be called from code. It has no interface, and you decide how to wire the model into scripts, apps, or editors.

Web app / mobile appAPI
Entry pointclaude.aiCreate a key in Console, call it from code
BillingSubscription planPer input and output token
Best forEveryday chat, writingAutomation, app integration, batch jobs
CredentialAccount loginAPI key

For the billing differences, see Claude Pro vs. Claude API. If you are still deciding which route to take, start with how beginners can get started with Claude.

Step 1: How do you set up the official Console?

  1. Open Anthropic's developer console and sign up with an email address or another supported method.
  2. Complete the organization details and verification the page asks for.
  3. Add credit or a payment method on the billing page. The API needs available credit to work. Top-up rules and supported payment methods are defined in the official documentation.

Note: Anthropic maintains an explicit list of supported countries and regions. Check the official supported-regions page before you sign up, and follow the terms of service and local regulations. If registration or payment doesn't work for you, skip to the other compliant options at the end of this article. Don't try to get around the restrictions. For the usage policy, see Anthropic's usage policy explained.

Step 2: How do you create and store an API key?

Create a key on the API Keys page in Console. A key is usually shown in full only once, at creation, so save it right away in a password manager or an environment variable.

export ANTHROPIC_API_KEY="sk-ant-your-key"

A few basic habits:

  • Never commit a key to a code repository, and never paste it into chats or screenshots.
  • Use a separate key for each project and each team member, so you can revoke one without affecting the others.
  • If you suspect a leak, delete the key in Console immediately and create a new one. For team setups, see how to manage API keys across a team.

Step 3: Send your first request with curl

The Messages endpoint is https://api.anthropic.com/v1/messages. It requires three request headers: x-api-key, anthropic-version, and content-type.

curl https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "<model name, see the official model list>",
    "max_tokens": 256,
    "messages": [{"role": "user", "content": "用一句话介绍你自己"}]
  }'

Set model to a model identifier that is currently available, as listed in the official documentation. max_tokens is required and caps how many tokens this call can generate. In the returned JSON, the answer text is in the text field of the content array, and the usage field gives input and output token counts, which you can use to reconcile your bill. (The prompt in the example, 用一句话介绍你自己, means "Introduce yourself in one sentence.")

Step 4: Call it from Python

pip install anthropic
import anthropic

client = anthropic.Anthropic()  # reads the ANTHROPIC_API_KEY environment variable automatically

message = client.messages.create(
    model="<model name, see the official model list>",
    max_tokens=256,
    messages=[{"role": "user", "content": "用一句话介绍你自己"}],
)
print(message.content[0].text)
print(message.usage)

Get this minimal example working first, then add a system prompt, multi-turn messages, and streaming. If you send a long, repeated prefix, look into prompt caching to cut costs.

Step 5: How do you troubleshoot errors?

HTTP statusError typeCommon causeWhat to do
400invalid_request_errorMalformed request body, missing max_tokensCheck the JSON against the official parameter reference
401authentication_errorKey is wrong, expired, or deletedRecheck the environment variable; recreate the key if needed
403permission_errorThis key can't access the resourceCheck organization and permission settings
404not_found_errorWrong model name or pathVerify the model identifier and URL
413request_too_largeRequest body exceeds the size limitShorten the input
429rate_limit_errorRate or quota limit hitLower concurrency, retry with backoff
500api_errorInternal server errorRetry later
529overloaded_errorService temporarily overloadedRetry later

The official documentation is the authority on what each error code means. If you suspect a server-side problem, check cdnping's Claude status and the Anthropic API probe first. They combine the official status page with an API probe that runs every 5 minutes. When retrying, keep requests idempotent to avoid double billing; see how to prevent duplicate charges on API retries.

What compliant access options exist besides the official API?

If the official route doesn't work for you (for example, you don't have an overseas payment method for now), you can consider a third-party aggregation gateway. If your region is outside Anthropic's supported list, follow the terms of service and local regulations and don't try to bypass them. You can instead use model services available in your region, such as China-based model APIs. Before choosing a gateway, read Is a Claude API relay safe? for a self-check, and compare the payment options in how to use the Claude API without an overseas credit card.

AGI Plan is one such option. AGI Plan is a commercial partner of cdnping, so please read the description below as a statement from a partner. It is a multi-provider LLM aggregation gateway sold as a monthly subscription, billed in RMB (Chinese yuan), with no overseas credit card required. One subscription can switch between multiple capability groups. Tools such as Claude Code and Codex connect by changing a single endpoint URL, and the gateway is compatible with both the OpenAI and Anthropic API formats. The site states plainly that it is not an officially authorized channel, and some models are marked "overseas only." Check the models page for the models and regions actually available. To connect, you sign up with an emailed verification code, pick a plan and create a key, then change one setting; details are in the setup guide. To see how to change the endpoint in your tool, refer to configuring a custom API endpoint for Claude Code and Codex.

What should you do next?

  • If the official route works for you: follow the five steps above to get a key, run the curl example, then switch to Python.
  • If it doesn't: read the relay risk self-check article first, then decide whether to try a gateway.
  • If calls feel slow from mainland China: use this explanation of AI API latency tests to tell whether the problem is the connection or the model itself.

Related articles

Keep reading