anthropic/lib.rs
1//! # anthropic — community Rust SDK for the Anthropic API
2//!
3//! This crate is an early port of the official
4//! [`anthropic-sdk-go`](https://github.com/anthropics/anthropic-sdk-go) to Rust.
5//! v0 implements only the non-streaming Messages API; streaming, tool use,
6//! multimodal, files, batches, Bedrock, Vertex, and the beta surface are
7//! tracked in `ROADMAP.md`.
8//!
9//! ## Quickstart
10//!
11//! ```no_run
12//! use anthropic::types::{InputMessage, MessageCreateParams, model};
13//!
14//! # async fn run() -> anthropic::Result<()> {
15//! // Reads ANTHROPIC_API_KEY from the environment.
16//! let client = anthropic::Client::from_env()?;
17//!
18//! let response = client
19//! .messages()
20//! .create(
21//! MessageCreateParams::builder()
22//! .model(model::CLAUDE_HAIKU_4_5)
23//! .max_tokens(256)
24//! .messages(vec![InputMessage::user("Say hi in one short sentence.")])
25//! .build(),
26//! )
27//! .await?;
28//!
29//! println!("{}", response.text());
30//! # Ok(()) }
31//! ```
32//!
33//! ## Multi-turn
34//!
35//! ```no_run
36//! use anthropic::types::{InputMessage, MessageCreateParams, model};
37//!
38//! # async fn run() -> anthropic::Result<()> {
39//! let client = anthropic::Client::from_env()?;
40//! let messages = vec![
41//! InputMessage::user("My name is Edinaldo."),
42//! InputMessage::assistant("Hello, Edinaldo! How can I help?"),
43//! InputMessage::user("What name did I just give you?"),
44//! ];
45//! let response = client
46//! .messages()
47//! .create(
48//! MessageCreateParams::builder()
49//! .model(model::CLAUDE_SONNET_4_6)
50//! .max_tokens(256)
51//! .messages(messages)
52//! .build(),
53//! )
54//! .await?;
55//! println!("{}", response.text());
56//! # Ok(()) }
57//! ```
58//!
59//! ## Errors
60//!
61//! All fallible operations return [`Result<T>`] (alias of `Result<T, Error>`).
62//! Non-2xx responses become [`Error::Api`] with the HTTP status, the parsed
63//! [`ErrorType`], the server-supplied message, and the `request-id` header.
64
65#![deny(unsafe_code)]
66#![warn(missing_docs)]
67// Field- and variant-level docs are added selectively where the name isn't
68// self-documenting; we don't blanket-warn on every `pub` field for v0.
69#![allow(clippy::missing_docs_in_private_items)]
70
71pub mod auth;
72pub mod client;
73pub mod config;
74pub mod error;
75pub mod http;
76pub mod messages;
77pub mod retry;
78pub mod stream;
79pub mod types;
80
81pub use crate::client::{Client, ClientBuilder};
82pub use crate::error::{Error, ErrorType, Result};
83pub use crate::messages::MessagesService;
84pub use crate::types::messages::{
85 ApiErrorBody, ContentBlock, ImageSource, InputMessage, Message, MessageContent,
86 MessageCreateParams, Metadata, Role, StopReason, SystemPrompt, ToolResultContent, Usage,
87};
88pub use crate::types::model::{self, Model};