Your First Claude API Call in TypeScript and Python
Large language model APIs all follow the same basic shape: you send a list of messages and get a message back. This guide uses the official Anthropic SDK to call Claude, but the ideas carry over to any provider.
1. Install the SDK and set your key
# TypeScript / Node.js
npm install @anthropic-ai/sdk
# Python
pip install anthropic
# Both SDKs read the key from the environment
export ANTHROPIC_API_KEY="sk-ant-..."
Never hard-code the key in source files. Keep it in an environment variable or your platform's secret manager.
2. Send a message (TypeScript)
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic(); // reads ANTHROPIC_API_KEY
const response = await client.messages.create({
model: 'claude-opus-5-5',
max_tokens: 16000,
messages: [{ role: 'user', content: 'Explain a REST API in two sentences.' }],
});
// content is an array of blocks; narrow to text blocks before reading .text
for (const block of response.content) {
if (block.type === 'text') console.log(block.text);
}
3. The same call in Python
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=16000,
messages=[{"role": "user", "content": "Explain a REST API in two sentences."}],
)
for block in response.content:
if block.type == "text":
print(block.text)
4. Add a system prompt
The system parameter sets the role, tone and rules for the whole conversation. It sits outside the messages array.
const response = await client.messages.create({
model: 'claude-opus-5-5',
max_tokens: 16000,
system: 'You are a senior backend engineer. Answer with short, practical examples.',
messages: [{ role: 'user', content: 'How do I paginate a SQL query?' }],
});
5. Multi-turn conversations
The API is stateless. To continue a chat, send the whole history every time, alternating user and assistant roles.
const messages: Anthropic.MessageParam[] = [
{ role: 'user', content: 'My name is Asha.' },
{ role: 'assistant', content: 'Nice to meet you, Asha!' },
{ role: 'user', content: 'What is my name?' },
];
const reply = await client.messages.create({ model: 'claude-opus-5-5', max_tokens: 16000, messages });
What comes back
content- an array of blocks (text,tool_use,thinking...).stop_reason- why generation stopped:end_turn,max_tokens,tool_useorrefusal.usage- input and output token counts, which is what you are billed for.
Always check stop_reason before trusting the output. A max_tokens stop means the answer was cut off.
Was this article helpful?
Comments
No comments yet. Have a question or a tip? Be the first to share it.