daBongo LMS AI Training Courses

The Anthropic Developer Platform – From First Call to Production

Lesson 2: Authentication and Your First API Call

Lesson Objectives

By the end of this lesson, students should be able to:

  • Set up authentication using environment variables
  • Install and configure the Anthropic SDK for Python or TypeScript
  • Make a first successful Messages API call
  • Read and interpret the API response structure

Lesson Content

Setting up your API key.

  1. Create an Anthropic account at console.anthropic.com
  2. Navigate to API Keys and generate a new key
  3. Copy the key immediately – it is shown only once
  4. Store it in an environment variable: ANTHROPIC_API_KEY=sk-ant-...

Never hardcode the key in source files. Use environment variables in development and a secrets manager (AWS Secrets Manager, Google Secret Manager, etc.) in production.

Installing the SDK.

Python: pip install anthropic TypeScript/Node: npm install @anthropic-ai/sdk

Always verify current SDK versions and installation instructions at docs.anthropic.com/en/docs/quickstart.

Your first API call (Python example pattern).

“`python import anthropic

client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from environment

message = client.messages.create( model="claude-sonnet-4-6", # replace with current model ID from docs max_tokens=1024, messages=[ {"role": "user", "content": "Hello, Claude."} ] )

print(message.content[0].text) “`

This is a minimal working call. The model parameter should use a current model ID from docs.anthropic.com – not hardcoded values from this course.

The response structure.

The Messages API response contains:

  • id: unique message identifier
  • type: "message"
  • role: "assistant"
  • content: array of content blocks (typically a text block with .text property)
  • model: the model that generated the response
  • stop_reason: why generation stopped (end_turn, max_tokens, tool_use)
  • usage: input and output token counts for this request

The usage field is important: log token counts in production to track costs and debug unexpectedly large requests.

Adding a system prompt.

python message = client.messages.create( model="claude-sonnet-4-6", max_tokens=1024, system="You are a helpful assistant for a software development team.", messages=[ {"role": "user", "content": "What are the main risk factors in this PR?"} ] )

The system parameter sets the persistent instruction context for the model – equivalent to Custom Instructions in the chat interface.

Practical Example

A developer makes her first API call by following the quickstart documentation.

First attempt fails – environment variable not loaded in her current shell session.

She runs export ANTHROPIC_API_KEY=... and retries.

Second attempt succeeds.

She inspects the response object, logs message.usage.input_tokens and message.usage.output_tokens, and calculates the cost of the call: less than $0.001.

She builds her initial cost model: for her expected 10,000 requests per day at similar prompt lengths, estimated monthly cost is approximately $X.

Cost estimation before scaling is a required step for any production API integration.

Safety Notes

API keys grant billing access to your Anthropic account. Leaked API keys in public repositories can result in unexpected charges from unauthorized use. Run git secret scan or equivalent tools if you suspect a key may have been committed. Rotate compromised keys immediately in the Anthropic console and set usage limits as a safeguard. Verify current key management and billing controls at console.anthropic.com.

Log in and enroll to access lesson quizzes.

Scroll to Top