Add to Claude Code
0 addsInstall this Claude Code skill.
Name: Claude API Integration
Save to: ~/.claude/skills/claude-api-integration.md
---
---
name: claude-api-integration
description: Set up Claude API / Anthropic SDK — streaming, tool use, prompt caching. Use when user says "integrate Claude API", "use Anthropic SDK", "add AI to my app", or "set up Claude".
---
Help the user integrate the Claude API into their application.
## Instructions
### Step 1: SDK Setup
Install the Anthropic SDK:
```bash
# TypeScript/JavaScript
npm install @anthropic-ai/sdk
# Python
pip install anthropic
```
Set the API key:
```
ANTHROPIC_API_KEY=sk-ant-...
```
### Step 2: Basic Usage
```typescript
import Anthropic from "@anthropic-ai/sdk"
const client = new Anthropic()
const message = await client.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello, Claude!" }],
})
console.log(message.content[0].text)
```
### Step 3: Streaming
For real-time responses:
```typescript
const stream = client.messages.stream({
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
messages: [{ role: "user", content: prompt }],
})
for await (const event of stream) {
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
process.stdout.write(event.delta.text)
}
}
```
### Step 4: System Prompts
```typescript
const message = await client.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
system: "You are a helpful coding assistant. Always respond with code examples.",
messages: [{ role: "user", content: "How do I sort an array in Python?" }],
})
```
### Step 5: Tool Use
Define tools Claude can call:
```typescript
const tools = [{
name: "get_weather",
description: "Get current weather for a location",
input_schema: {
type: "object",
properties: {
location: { type: "string", description: "City name" },
},
required: ["location"],
},
}]
const message = await client.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
tools,
messages: [{ role: "user", content: "What's the weather in Denver?" }],
})
// Check if Claude wants to use a tool
if (message.stop_reason === "tool_use") {
const toolUse = message.content.find(b => b.type === "tool_use")
// Execute the tool, then send result back
}
```
### Step 6: Prompt Caching
For repeated system prompts or large contexts:
```typescript
const message = await client.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
system: [{
type: "text",
text: largeSystemPrompt,
cache_control: { type: "ephemeral" },
}],
messages: [{ role: "user", content: "..." }],
})
```
### Step 7: Error Handling
```typescript
import Anthropic from "@anthropic-ai/sdk"
try {
const message = await client.messages.create({ ... })
} catch (error) {
if (error instanceof Anthropic.RateLimitError) {
// Wait and retry with exponential backoff
} else if (error instanceof Anthropic.APIError) {
console.error(`API error: ${error.status} ${error.message}`)
}
}
```
### Step 8: Best Practices
- Use the latest model (`claude-sonnet-4-20250514` or `claude-opus-4-20250514`)
- Set `max_tokens` appropriately (don't default to max)
- Use streaming for user-facing responses
- Implement retry logic with exponential backoff for rate limits
- Cache system prompts when reusing across requests
- Keep API keys server-side only (never expose in frontend)
- Log token usage for cost monitoring
Paste into Claude Code to add this skill.
How to add
Full guide →Click Add, then paste into Claude Code. Claude will save it to the right location for you.
Target: .claude/commands/<name>.md