Skip to main content

claude_api/messages/
mod.rs

1//! The Messages API.
2//!
3//! The headline endpoint of the Anthropic API. Build a request via
4//! [`CreateMessageRequest::builder`], then send it through the
5//! [`Messages`] namespace handle obtained from
6//! [`Client::messages`](crate::Client::messages).
7//!
8//! # Endpoints
9//!
10//! | Method | Path | Function |
11//! |---|---|---|
12//! | `POST` | `/v1/messages` | [`Messages::create`] (non-streaming) |
13//! | `POST` | `/v1/messages` | [`Messages::create_stream`] (SSE) |
14//! | `POST` | `/v1/messages/count_tokens` | [`Messages::count_tokens`] |
15//!
16//! # Quick start
17//!
18//! ```no_run
19//! use claude_api::{Client, messages::CreateMessageRequest, types::ModelId};
20//! # async fn run() -> Result<(), claude_api::Error> {
21//! let client = Client::new("sk-ant-...");
22//! let resp = client.messages().create(
23//!     CreateMessageRequest::builder()
24//!         .model(ModelId::SONNET_4_6)
25//!         .max_tokens(256)
26//!         .system("Be concise.")
27//!         .user("What is the capital of France?")
28//!         .build()?,
29//! ).await?;
30//! # Ok(()) }
31//! ```
32//!
33//! # Module layout
34//!
35//! - [`request`] -- [`CreateMessageRequest`], [`CountTokensRequest`],
36//!   builders
37//! - [`response`] -- [`Message`], [`CountTokensResponse`],
38//!   [`ContainerInfo`]
39//! - [`content`] -- [`ContentBlock`] / [`KnownBlock`] union with
40//!   forward-compat fallthrough
41//! - [`stream`] -- [`StreamEvent`], [`ContentDelta`], the
42//!   `EventStream` aggregator + `on_*` callbacks
43//! - [`tools`] -- [`Tool`], [`BuiltinTool`], [`CustomTool`],
44//!   [`ToolChoice`]
45//! - [`cache`] -- [`CacheControl`] for prompt caching
46//! - [`thinking`] -- [`ThinkingConfig`] for extended thinking
47//! - [`mcp`] -- [`McpServerConfig`] for MCP server invocation
48//! - [`citation`] -- typed [`Citation`] enum
49//! - [`input`] -- [`MessageInput`], [`SystemPrompt`] helpers
50//! - [`metadata`] -- [`MessageMetadata`], [`RequestServiceTier`]
51//!
52//! For streaming, see [`stream`] for the wire-event types and
53//! [`api::Messages::create_stream`] for the namespace method.
54
55pub mod cache;
56pub mod citation;
57pub mod content;
58pub mod input;
59pub mod mcp;
60pub mod metadata;
61pub mod request;
62pub mod response;
63pub mod stream;
64pub mod thinking;
65pub mod tools;
66
67#[cfg(feature = "async")]
68#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
69pub mod api;
70
71pub use cache::CacheControl;
72pub use citation::{Citation, KnownCitation};
73pub use content::{
74    CitationConfig, ContentBlock, DocumentSource, ImageSource, KnownBlock, ToolResultContent,
75};
76pub use input::{MessageContent, MessageInput, SystemPrompt};
77pub use mcp::{McpServerConfig, McpToolConfiguration};
78pub use metadata::{MessageMetadata, RequestServiceTier};
79pub use request::{
80    CountTokensRequest, CountTokensRequestBuilder, CreateMessageRequest,
81    CreateMessageRequestBuilder,
82};
83pub use response::{
84    ClearThinkingEdit, ClearToolUsesEdit, ContainerInfo, ContextEdit, CountTokensResponse,
85    KnownContextEdit, KnownStopDetails, Message, RefusalStopDetails, ResponseContextManagement,
86    StopDetails,
87};
88pub use stream::{ContentDelta, KnownContentDelta, KnownStreamEvent, MessageDelta, StreamEvent};
89pub use thinking::ThinkingConfig;
90pub use tools::{BuiltinTool, CustomTool, KnownBuiltinTool, Tool, ToolChoice, UserLocation};
91
92#[cfg(feature = "async")]
93pub use api::Messages;