Hands-On with AI
Getting API Access and Making Your First Call
From zero to your first AI-powered application
From User to Builder
So far in this course, you've been learning how AI works conceptually. Now it's time to get hands-on. In this lesson, you'll go from using AI through a chat interface to calling an AI model programmatically — the first step toward building your own AI-powered applications.
What Is an API?
An API (Application Programming Interface) is a way for programs to talk to each other. When you use ChatGPT or Claude through a website, you're using a graphical interface designed for humans. When you use an API, you're sending structured requests directly to the AI model from your own code.
Think of it this way: the website is like calling a restaurant to place a takeout order by talking to a person. The API is like placing that order through an automated system that accepts structured input and returns structured output. Same food, different interface.
APIs let you:
- Integrate AI into your own applications
- Process hundreds or thousands of requests automatically
- Customize behavior with system prompts and parameters
- Build products and tools powered by AI
Getting Your API Key
To call the Claude API, you need an API key from Anthropic. Here's how:
- Go to console.anthropic.com and create an account.
- Navigate to API Keys in your account settings.
- Click Create Key. Give it a name like "my-first-app."
- Copy the key immediately — you won't be able to see it again.
- Add a small amount of credit to your account (API calls cost money per token, but it's very affordable for learning).
Important: Treat your API key like a password. Never put it in public code, share it, or commit it to version control. Store it as an environment variable.
Your First API Call with curl
The simplest way to test your API key is with curl, a command-line tool for making HTTP requests. Open your terminal and run:
curl https://api.anthropic.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: YOUR_API_KEY_HERE" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-sonnet-4-5-20250929",
"max_tokens": 256,
"messages": [
{"role": "user", "content": "What is the capital of France? Reply in one sentence."}
]
}'
You should get back a JSON response containing Claude's answer. Let's break down the request:
model: Which Claude model to use. Sonnet is a good balance of capability and cost.max_tokens: Maximum length of the response (in tokens).messages: The conversation history — an array of messages with roles ("user" or "assistant").
Your First API Call with Python
For building real applications, you'll want to use Anthropic's Python SDK. First, install it:
pip install anthropic
Set your API key as an environment variable:
export ANTHROPIC_API_KEY="your-key-here"
Now write a simple Python script:
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain photosynthesis in three sentences."}
]
)
print(message.content[0].text)
Run it, and you'll see Claude's response printed to your terminal. Congratulations — you've just made your first programmatic AI call!
Adding a System Prompt
System prompts let you set the AI's behavior for the conversation. Here's a more advanced example:
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=1024,
system="You are a helpful science tutor for middle school students. "
"Explain concepts simply and use fun analogies.",
messages=[
{"role": "user", "content": "Why is the sky blue?"}
]
)
print(message.content[0].text)
The system parameter shapes how the model responds. The same question produces very different answers with different system prompts — try changing it to "You are a physics professor" or "You are a poet" and see the difference.
Understanding the Response
The API returns structured data. Key fields:
content: The model's response (an array of content blocks).model: Which model was used.usage: Token counts —input_tokens(what you sent) andoutput_tokens(what the model generated). This is how billing works.stop_reason: Why the model stopped — usuallyend_turn(natural end) ormax_tokens(hit the limit).
Cost and Practical Tips
API pricing is per token (typically fractions of a cent). For learning and experimentation, you'll spend very little. A conversation that costs $0.01 on the API might teach you something that saves hours of work.
Tips for getting started:
- Start with Claude Sonnet — it's fast, capable, and cost-effective for learning.
- Set
max_tokensreasonably to control costs and response length. - Use environment variables for API keys, never hardcode them.
- Check the Anthropic documentation — it's thorough and well-written.
Key Takeaways
- An API lets you call AI models from your own code, enabling automation and custom applications.
- Get an API key from console.anthropic.com and treat it like a password.
- You can test with curl and build with the Anthropic Python SDK.
- Key parameters:
model,max_tokens,messages, andsystem. - API pricing is per token — very affordable for learning and experimentation.
Ask me anything about this lesson.
I have the full lesson content as context.