← Blog

Anthropic Claude Tutorial: Your First API Call

2026-09-13 · 5 min read · SubToAPI Team

If you searched "anthropic claude tutorial," you're probably trying to go from zero to a working integration — send a prompt, get a response, maybe stream text or call a tool. This guide walks through that path with real code, not marketing copy. By the end you'll have made a successful call to Claude and understand the core building blocks: messages, roles, system prompts, streaming, and tool use.

We'll use the Messages API format, which is the standard interface for talking to Claude models. The same shape works whether you're calling Anthropic directly or through a gateway like SubToAPI that wraps a Claude subscription in an HTTPS API.

Step 1: Get Access and an API Key

To follow this tutorial you need some way to authenticate against a Claude-compatible endpoint. Two common paths:

Either way, you'll end up with a bearer token you pass in an Authorization header. If you're using SubToAPI, generate a key from the dashboard after signing up at /signup — there's a free trial, and the /docs/quickstart page has copy-paste examples.

Step 2: Send Your First Message

The core request is a POST to a messages endpoint with a model name, a max token limit, and an array of messages. Each message has a role (user or assistant) and content.

curl https://api.subtoapi.app/v1/messages \
  -H "Authorization: Bearer $SUBTOAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-3-5-sonnet-latest",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "Explain what a race condition is in one paragraph."}
    ]
  }'

The response comes back as JSON with a content array containing text blocks, plus usage metadata showing input and output token counts. That usage data matters — it's what you'd use to track cost per request or per customer if you're building a product on top of Claude.

Step 3: Add a System Prompt

A system prompt sets behavior and constraints without being part of the conversational history. It's passed as a top-level field, not as a message:

curl https://api.subtoapi.app/v1/messages \
  -H "Authorization: Bearer $SUBTOAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-3-5-sonnet-latest",
    "max_tokens": 500,
    "system": "You are a terse code reviewer. Respond only with bullet points.",
    "messages": [
      {"role": "user", "content": "Review this function: function add(a,b){return a+b}"}
    ]
  }'

Use the system prompt for tone, format, and role definition. Keep it stable across a session so the model's behavior stays consistent. Full field reference is in /docs/messages.

Step 4: Stream the Response

For chat UIs or anything user-facing, streaming matters — nobody wants to stare at a spinner for eight seconds. Set "stream": true and the response comes back as server-sent events instead of one JSON blob.

const response = await fetch("https://api.subtoapi.app/v1/messages", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "claude-3-5-sonnet-latest",
    max_tokens: 1024,
    stream: true,
    messages: [{ role: "user", content: "Write a haiku about deployment pipelines." }],
  }),
});

const reader = response.body.getReader();
const decoder = new TextDecoder();

while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  process.stdout.write(decoder.decode(value));
}

Each event carries a small delta of text. You append these as they arrive to render output progressively. Details on event types and parsing are in /docs/streaming.

Step 5: Give Claude Tools

Tool use lets Claude call functions you define — fetch a record, run a calculation, hit an internal API — and incorporate the result into its answer. You describe tools with a JSON schema, and Claude decides when to invoke one.

{
  "model": "claude-3-5-sonnet-latest",
  "max_tokens": 1024,
  "tools": [
    {
      "name": "get_weather",
      "description": "Get current weather for a city",
      "input_schema": {
        "type": "object",
        "properties": {
          "city": { "type": "string" }
        },
        "required": ["city"]
      }
    }
  ],
  "messages": [
    { "role": "user", "content": "What's the weather in Lisbon?" }
  ]
}

When Claude wants to use the tool, the response contains a tool_use block instead of plain text. Your code executes the actual function, then sends the result back as a tool_result message in a follow-up request. This is the pattern behind most Claude-powered agents and assistants. Worked examples with multi-turn tool loops are in /docs/tools.

Step 6: Manage Keys and Usage

Once you're past the "hello world" stage, the practical questions become: how do I rotate keys, track usage per team member, and avoid a single leaked key taking down billing? If you're building on top of a Claude subscription rather than pay-per-token API credits, a layer like SubToAPI adds per-key usage metadata and team seats so you're not sharing one raw credential across a codebase. Plans start at €9/month solo, with Team and Scale tiers for shared usage — see /pricing for specifics.

Putting It Together

A minimal but complete integration touches four things: authentication, a messages payload with the right roles, error handling for rate limits or malformed requests, and — if you're building anything interactive — streaming. Tool use is optional but becomes necessary the moment your app needs Claude to act on live data instead of just reasoning over what's in the prompt.

From here, the fastest way to solidify this is to build something small: a CLI that summarizes text files, a Slack bot that answers FAQs, or a script that classifies support tickets. Each of those exercises the same primitives covered above.

questions

Do I need Anthropic's own API key to follow this tutorial? No. You need any endpoint that implements the Messages API format with valid authentication — that includes Anthropic's direct API or a subscription-based gateway like SubToAPI.

What's the difference between a system prompt and a user message? The system prompt sets persistent behavior and format instructions and isn't part of the visible conversation. User messages are the actual turns in the dialogue that the model responds to.

Is streaming required for a basic integration? No, it's optional. Non-streaming calls are simpler and fine for batch jobs or backend processing; streaming matters mainly for real-time, user-facing chat interfaces.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →