Integrating the Claude API into your own application
Integrating the Claude API into your own application
This article explains how to integrate the Claude API into your application using the Anthropic SDK. Aimed at developers who want to set up a working API connection.
PREREQUISITES - Node.js 18+ or Python 3.8+ - An Anthropic account with an active API key (starting with `sk-ant-`) - Access to a command-line interface
STEPS
1. Create an API key in the Anthropic Console Go to console.anthropic.com, log in, and generate a new API key. Save it immediately — the key is only visible once. Never use hardcoding; store the key in an environment variable or a secret manager such as AWS Secrets Manager.
2. Install the Anthropic SDK
npm install @anthropic-ai/sdk
For Python:
pip install anthropic
3. Set the environment variable
export ANTHROPIC_API_KEY="sk-ant-...your-key-here..."
4. Make a first API call using the Messages API Create a file `test-claude.js` with the following code:
import Anthropic from '@anthropic-ai/sdk';</p><p> const client = new Anthropic();</p><p> async function main() {
const message = await client.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello, who are you?" }],
});
console.log(message.content[0].text);
}</p><p> main().catch(console.error);5. Run the script
node test-claude.js
The output shows Claude's response.
6. Manage conversation history using a message array Each subsequent API call includes the full history. Use an array to keep track of messages:
const messages = [
{ role: "user", content: "What is the capital of France?" },
{ role: "assistant", content: "Paris." },
{ role: "user", content: "And of Italy?" }
];7. Use the Token Counting API to estimate costs Add a pre-check using the `count_tokens` endpoint:
const count = await client.messages.countTokens({
model: "claude-sonnet-4-20250514",
messages: messages
});
console.log(`Token count: ${count.input_tokens}`);8. Implement Message Batches for large-scale processing For batch processing, use the asynchronous Batches API. This reduces costs by up to 50%:
const batch = await client.messages.batches.create({
requests: [
{ custom_id: "req1", params: { model: "claude-haiku-3-5-20241022", max_tokens: 1024, messages: [{ role: "user", content: "Translate to English: Hello" }] }},
{ custom_id: "req2", params: { model: "claude-haiku-3-5-20241022", max_tokens: 1024, messages: [{ role: "user", content: "Translate to English: Thank you" }] }}
]
});VERIFICATION Run the test script. If you receive a valid response from Claude (no `401 Unauthorized` or `404 Not Found`), the integration works.
COMMON ISSUES
1. API key does not work Check whether the key is still active in the Anthropic Console. Keys expire after 90 days without use. Generate a new one if necessary.
2. Rate limit reached (429 Too Many Requests) Reduce the request frequency. Use the Token Counting API to estimate in advance whether you are close to a limit. Anthropic applies per-workspace limits — create multiple workspaces for different use cases.
3. Invalid model name (400 Bad Request) Check the exact model name in the Anthropic documentation. Use `claude-sonnet-4-20250514` for Claude Sonnet 4.0 or `claude-haiku-3-5-20241022` for Claude Haiku 3.5. Model names change with updates.
Which model suits your use case — heavy for reasoning or light for speed?