Claude API Tutorial: Python and JavaScript Getting Started

The Claude API gives you programmatic access to Claude in your own applications and scripts. This guide gets you from zero to a working integration in Python or JavaScript.

Prerequisites

  • Anthropic account at console.anthropic.com
  • API key from Console → API Keys
  • Python 3.7+ or Node.js 18+

Installation

# Python
pip install anthropic

# JavaScript
npm install @anthropic-ai/sdk

Your First API Call (Python)

import anthropic

client = anthropic.Anthropic(api_key="your-api-key-here")

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Explain APIs in plain English."}]
)
print(message.content[0].text)

Adding a System Prompt

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    system="You are a helpful customer support agent for Acme Corp.",
    messages=[{"role": "user", "content": "How do I reset my password?"}]
)

Streaming Responses

with client.messages.stream(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a 500-word blog post about AI."}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Model Selection

ModelStringBest For
Claude Opus 4.6claude-opus-4-6Complex reasoning, coding
Claude Sonnet 4.6claude-sonnet-4-6Balanced everyday tasks
Claude Haiku 4.5claude-haiku-4-5-20251001Fast lightweight tasks

Frequently Asked Questions

How much does the Claude API cost?

Pricing is per token (input and output separately). Check anthropic.com/pricing. Haiku is cheapest, Sonnet offers the best cost/quality balance for most applications.

Do I need a Claude subscription to use the API?

No. API access is separate. Create an Anthropic Console account and pay per token used.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *