npm-anthropic-ai--sdk

Description
The official TypeScript library for the Anthropic API providing comprehensive client functionality for Claude AI models.
Author
tessl
Last updated

How to use

npx @tessl/cli registry install tessl/npm-anthropic-ai--sdk@0.61.0

index.md docs/

1
# Anthropic SDK
2
3
The official TypeScript library for the Anthropic API, providing comprehensive client functionality for Claude AI models. This SDK enables developers to integrate Claude's conversational AI capabilities into their applications with full type safety, streaming support, and built-in error handling.
4
5
## Package Information
6
7
- **Package Name**: @anthropic-ai/sdk
8
- **Package Type**: npm
9
- **Language**: TypeScript
10
- **Installation**: `npm install @anthropic-ai/sdk`
11
12
## Core Imports
13
14
```typescript
15
import Anthropic from "@anthropic-ai/sdk";
16
```
17
18
For named imports:
19
20
```typescript
21
import { Anthropic, type ClientOptions } from "@anthropic-ai/sdk";
22
```
23
24
CommonJS:
25
26
```javascript
27
const Anthropic = require("@anthropic-ai/sdk");
28
```
29
30
## Basic Usage
31
32
```typescript
33
import Anthropic from "@anthropic-ai/sdk";
34
35
const client = new Anthropic({
36
apiKey: process.env.ANTHROPIC_API_KEY, // defaults to process.env["ANTHROPIC_API_KEY"]
37
});
38
39
async function main() {
40
const message = await client.messages.create({
41
max_tokens: 1024,
42
messages: [{ role: "user", content: "Hello, Claude!" }],
43
model: "claude-3-5-sonnet-latest",
44
});
45
46
console.log(message.content);
47
}
48
49
main();
50
```
51
52
## Architecture
53
54
The Anthropic SDK is built around these key components:
55
56
- **Client Class**: Main entry point (`Anthropic`) with authentication, HTTP handling, and resource management
57
- **Resource APIs**: Specialized interfaces for different API endpoints (Messages, Models, Completions, Beta)
58
- **Type System**: Comprehensive TypeScript definitions for all API parameters and responses
59
- **Streaming Support**: Server-sent events handling for real-time responses
60
- **Error Handling**: Rich error hierarchy with detailed API error information
61
- **Authentication**: Flexible authentication with API keys or auth tokens
62
63
## Capabilities
64
65
### Client Configuration
66
67
Core client initialization and configuration options for authentication, timeouts, retries, and custom request handling.
68
69
```typescript { .api }
70
class Anthropic extends BaseAnthropic {
71
constructor(options?: ClientOptions);
72
73
completions: Completions;
74
messages: Messages;
75
models: Models;
76
beta: Beta;
77
}
78
79
interface ClientOptions {
80
apiKey?: string | null;
81
authToken?: string | null;
82
baseURL?: string | null;
83
timeout?: number;
84
maxRetries?: number;
85
fetch?: Fetch;
86
dangerouslyAllowBrowser?: boolean;
87
}
88
```
89
90
[Client Configuration](./client-configuration.md)
91
92
### Messages API
93
94
Primary interface for conversational AI interactions with Claude models. Supports both streaming and non-streaming responses, with comprehensive message formatting and tool usage capabilities.
95
96
```typescript { .api }
97
class Messages extends APIResource {
98
create(params: MessageCreateParamsNonStreaming): APIPromise<Message>;
99
create(params: MessageCreateParamsStreaming): APIPromise<Stream<RawMessageStreamEvent>>;
100
stream(params: MessageStreamParams): MessageStream;
101
countTokens(params: MessageCountTokensParams): APIPromise<MessageTokensCount>;
102
}
103
104
interface MessageCreateParams {
105
model: Model;
106
messages: MessageParam[];
107
max_tokens: number;
108
system?: string;
109
tools?: Tool[];
110
tool_choice?: ToolChoice;
111
temperature?: number;
112
stream?: boolean;
113
}
114
```
115
116
[Messages API](./messages-api.md)
117
118
### Models API
119
120
Access to model information and capabilities, allowing applications to discover available models and their specifications.
121
122
```typescript { .api }
123
class Models extends APIResource {
124
retrieve(modelID: string, params?: ModelRetrieveParams): APIPromise<ModelInfo>;
125
list(params?: ModelListParams): PagePromise<ModelInfosPage, ModelInfo>;
126
}
127
128
interface ModelInfo {
129
id: string;
130
display_name: string;
131
created_at: string;
132
context_length: number;
133
max_tokens?: number;
134
}
135
```
136
137
[Models API](./models-api.md)
138
139
### Completions API (Legacy)
140
141
Legacy text completion interface for backwards compatibility. New applications should use the Messages API.
142
143
```typescript { .api }
144
class Completions extends APIResource {
145
create(params: CompletionCreateParamsNonStreaming): APIPromise<Completion>;
146
create(params: CompletionCreateParamsStreaming): APIPromise<Stream<Completion>>;
147
}
148
149
interface CompletionCreateParams {
150
model: Model;
151
prompt: string;
152
max_tokens_to_sample: number;
153
temperature?: number;
154
stream?: boolean;
155
}
156
```
157
158
[Completions API](./completions-api.md)
159
160
### Beta Features
161
162
Access to experimental and beta features including file handling, advanced model capabilities, and new API endpoints.
163
164
```typescript { .api }
165
class Beta extends APIResource {
166
files: Files;
167
models: Models;
168
messages: Messages;
169
}
170
```
171
172
[Beta Features](./beta-features.md)
173
174
### Error Handling
175
176
Comprehensive error hierarchy with detailed information for debugging and error recovery.
177
178
```typescript { .api }
179
class AnthropicError extends Error {}
180
class APIError extends AnthropicError {
181
status: number;
182
headers: Headers;
183
}
184
class AuthenticationError extends APIError {}
185
class RateLimitError extends APIError {}
186
class BadRequestError extends APIError {}
187
```
188
189
[Error Handling](./error-handling.md)
190
191
## Core Types
192
193
```typescript { .api }
194
type Model =
195
| "claude-3-7-sonnet-latest"
196
| "claude-3-7-sonnet-20250219"
197
| "claude-sonnet-4-20250514"
198
| "claude-sonnet-4-0"
199
| "claude-4-sonnet-20250514"
200
| "claude-3-5-sonnet-latest"
201
| "claude-3-5-sonnet-20241022"
202
| "claude-3-5-sonnet-20240620"
203
| "claude-3-5-haiku-latest"
204
| "claude-3-5-haiku-20241022"
205
| "claude-opus-4-0"
206
| "claude-opus-4-20250514"
207
| "claude-4-opus-20250514"
208
| "claude-opus-4-1-20250805"
209
| "claude-3-opus-latest"
210
| "claude-3-opus-20240229"
211
| "claude-3-haiku-20240307"
212
| "claude-2.1"
213
| "claude-2.0"
214
| "claude-instant-1.2";
215
216
interface Message {
217
id: string;
218
type: "message";
219
role: "assistant";
220
content: ContentBlock[];
221
model: Model;
222
stop_reason: StopReason | null;
223
stop_sequence: string | null;
224
usage: Usage;
225
}
226
227
interface MessageParam {
228
role: "user" | "assistant";
229
content: string | ContentBlockParam[];
230
}
231
232
interface Usage {
233
input_tokens: number;
234
output_tokens: number;
235
}
236
237
type StopReason = "end_turn" | "max_tokens" | "stop_sequence" | "tool_use";
238
```