Developers · Advanced · 10 min

How to make your first Claude API call (with a spend cap)

From API key to first response in ten minutes, without a surprise bill.

Where your API key should live
  1. Your websiteNo key here, ever
  2. Your backendA Cloudflare Worker holds the key as a secret
  3. Claude APISpend limit set in the Console
  4. Response

This guide shows how to call the Claude API for the first time, safely: get a key, set a spending limit, send a request, and keep the key off your website.

Step 1: Get a key

Create an account in the Claude Console, add billing and create an API key. Treat it like a password: never paste it into code you share or commit to a repository.

Step 2: Set a limit first

Before you write any code, set a monthly spend limit for your organisation or workspace in the Console. It’s the cheapest insurance you’ll ever buy.

Step 3: Make the call

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": "claude-sonnet-5-5",
    "max_tokens": 500,
    "messages": [
      {"role": "user", "content": "اكتب ترحيباً قصيراً لعملاء متجر في الدوحة."}
    ]
  }'

Step 4: Keep costs predictable

Set max_tokens on every request, use smaller models for simple jobs like sorting or tagging, and log token usage per request so you can see where the money goes.

Step 5: Keep the key server-side

Never call the API directly from a public website. Put the call behind your own backend, such as a Cloudflare Worker, and store the key there as a secret.

Common questions

Which model should I start with? Sonnet is a sensible default. Our guide on choosing a model explains when to switch.

Can I call the API from my website’s JavaScript? No. Anyone could copy your key. Always go through a backend.

Sources: Claude Platform documentation: Messages API and spend limits (checked November 2026)

Keep reading

The Sunday Signal

One email every Sunday morning: the week in AI, in five minutes. In English or Arabic.