C.W.K.
Stream
Lesson 01 of 06 · published

설치 · 타입 · 단일 클라이언트

~14 min · install, types, esm, cjs

Level 0Observer
0 XP0/64 lessons0/13 achievements
0/150 XP to next level150 XP to go0% complete

패키지 하나, ESM·CJS 양쪽

@anthropic-ai/sdk는 공식 TypeScript SDK. Dual 빌드(ESM·CommonJS), 즉시 사용 가능한 strict 타입, Node·Bun·Deno·Edge 런타임에서 동작(주의사항 있음). 패키지 하나; import 스타일로 픽.

Strict 타입이 친구

모든 요청·응답 모양이 export됨. Message 배열엔 Anthropic.MessageParam, 풀 응답엔 Anthropic.Message, 콘텐츠 블록엔 Anthropic.TextBlock / Anthropic.ToolUseBlock. Strict 타입이 Python에선 런타임에 조용히 깨질 tool-loop 버그를 컴파일 타임에 잡아.

클라이언트 하나, 디폴트 async

Python의 두-클라이언트 분리와 달리, TypeScript SDK는 async 클라이언트 하나. Node, Next.js Route Handler, worker, 어디서든 호출. 생성자가 env에서 ANTHROPIC_API_KEY 자동 읽음; per-call 키 회전 필요할 때만 명시 전달.

원칙: Export된 타입 써. Message 모양 손코딩이 잘못된 assistant content 리스트로 tool 루프 ship하는 길이야.

Code

설치와 검증·bash
npm install @anthropic-ai/sdk
# 또는: pnpm add @anthropic-ai/sdk / yarn add @anthropic-ai/sdk

# 검증
node -e "console.log(require('@anthropic-ai/sdk/package.json').version)"
node --input-type=module -e "import Anthropic from '@anthropic-ai/sdk'; const c = new Anthropic(); console.log(typeof c.messages.create);"
Typed Messages 호출 (모던 import)·typescript
import Anthropic from '@anthropic-ai/sdk';
import type { MessageParam, Message } from '@anthropic-ai/sdk/resources/messages';

const client = new Anthropic();

const messages: MessageParam[] = [
  { role: 'user', content: 'One sentence on why typed SDKs reduce production bugs.' },
];

const response: Message = await client.messages.create({
  model: 'claude-sonnet-4-6',
  max_tokens: 256,
  system: 'Answer in exactly one sentence.',
  messages,
});

const firstText = response.content.find(b => b.type === 'text');
if (firstText && firstText.type === 'text') {
  console.log(firstText.text);
}

External links

Exercise

TypeScript 프로젝트에 SDK 추가. User 문자열 받아서 typed string 답 반환하는 함수 작성, export 타입 end-to-end 사용. any X, as Message X.
Hint
as 손 가면 union narrow 대신 — discriminated content 배열이 그래서 있는 거야.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.