1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//! # crimson-crab
//!
//! The production-grade Rust SDK for Anthropic's Claude API.
//!
//! This crate mirrors the Claude wire API exactly (see the type modules under
//! [`types`]) and is designed to be **forward compatible**: content blocks,
//! stop reasons, and other type-tagged or string-valued enums all carry a
//! catch-all variant, so values the SDK has never seen deserialize instead of
//! erroring.
//!
//! > crimson-crab is an independent open-source project and is not affiliated
//! > with Anthropic.
//!
//! ## Status
//!
//! The wire types, the HTTP [`Client`], the Messages, Models, and Batches
//! endpoints, and SSE [`streaming`] (`client.messages().stream(&req)`) are all
//! implemented.
//!
//! ## Quickstart
//!
//! ```no_run
//! use crimson_crab::model_ids::CLAUDE_OPUS_4_8;
//! use crimson_crab::prelude::*;
//!
//! # #[tokio::main]
//! # async fn main() -> crimson_crab::Result<()> {
//! // Reads the API key from the ANTHROPIC_API_KEY environment variable.
//! let client = Client::from_env()?;
//!
//! let request = MessagesRequest::builder()
//! .model(CLAUDE_OPUS_4_8)
//! .max_tokens(1024)
//! .messages(vec![MessageParam::user("Hello, Claude!")])
//! .build()?;
//!
//! let message = client.messages().create(&request).await?;
//! println!("{}", message.text());
//! # Ok(())
//! # }
//! ```
//!
//! ## Building request values
//!
//! ```
//! use crimson_crab::model_ids::CLAUDE_OPUS_4_8;
//! use crimson_crab::prelude::*;
//!
//! // A conversation turn.
//! let messages = vec![MessageParam::user("What is the weather in Paris?")];
//!
//! // A custom tool the model may call.
//! let tool = Tool::new(
//! "get_weather",
//! "Get the current weather for a location",
//! serde_json::json!({
//! "type": "object",
//! "properties": {"location": {"type": "string"}},
//! "required": ["location"]
//! }),
//! );
//!
//! assert_eq!(CLAUDE_OPUS_4_8, "claude-opus-4-8");
//! assert_eq!(messages[0].role, Role::User);
//! assert_eq!(tool.name, "get_weather");
//! ```
//!
//! ## Parsing a response
//!
//! ```
//! use crimson_crab::prelude::*;
//!
//! let body = serde_json::json!({
//! "id": "msg_01ABC",
//! "type": "message",
//! "role": "assistant",
//! "model": "claude-opus-4-8",
//! "content": [{"type": "text", "text": "It is sunny."}],
//! "stop_reason": "end_turn",
//! "stop_sequence": null,
//! "usage": {"input_tokens": 12, "output_tokens": 4}
//! });
//! let msg: Message = serde_json::from_value(body).unwrap();
//! assert_eq!(msg.text(), "It is sunny.");
//! assert_eq!(msg.stop_reason, Some(StopReason::EndTurn));
//! ```
// The HTTP client speaks HTTPS to `https://api.anthropic.com`, which requires a
// TLS backend on native targets. Fail the build early (rather than at runtime
// with an opaque connector error) if default features were disabled without
// selecting one. `wasm32` is exempt: there the browser's `fetch` provides TLS.
compile_error!;
pub use ;
pub use ;
pub use ;
/// Commonly used types, re-exported for `use crimson_crab::prelude::*;`.
///
/// # Examples
///
/// ```
/// use crimson_crab::prelude::*;
///
/// let _ = MessageParam::user("hi");
/// let _ = ToolChoice::auto();
/// let _ = ThinkingConfig::adaptive();
/// let _ = MessagesRequest::builder().model("claude-opus-4-8");
/// ```
/// Compiles the `README.md` code samples as part of `cargo test --doc` so the
/// crate's most-read snippets (quickstart, streaming, tool loop) cannot silently
/// drift from the public API. The item exists only during doctest builds.
;