Skip to main content

Crate anthropic

Crate anthropic 

Source
Expand description

§anthropic — community Rust SDK for the Anthropic API

This crate is an early port of the official anthropic-sdk-go to Rust. v0 implements only the non-streaming Messages API; streaming, tool use, multimodal, files, batches, Bedrock, Vertex, and the beta surface are tracked in ROADMAP.md.

§Quickstart

use anthropic::types::{InputMessage, MessageCreateParams, model};

// Reads ANTHROPIC_API_KEY from the environment.
let client = anthropic::Client::from_env()?;

let response = client
    .messages()
    .create(
        MessageCreateParams::builder()
            .model(model::CLAUDE_HAIKU_4_5)
            .max_tokens(256)
            .messages(vec![InputMessage::user("Say hi in one short sentence.")])
            .build(),
    )
    .await?;

println!("{}", response.text());

§Multi-turn

use anthropic::types::{InputMessage, MessageCreateParams, model};

let client = anthropic::Client::from_env()?;
let messages = vec![
    InputMessage::user("My name is Edinaldo."),
    InputMessage::assistant("Hello, Edinaldo! How can I help?"),
    InputMessage::user("What name did I just give you?"),
];
let response = client
    .messages()
    .create(
        MessageCreateParams::builder()
            .model(model::CLAUDE_SONNET_4_6)
            .max_tokens(256)
            .messages(messages)
            .build(),
    )
    .await?;
println!("{}", response.text());

§Errors

All fallible operations return Result<T> (alias of Result<T, Error>). Non-2xx responses become Error::Api with the HTTP status, the parsed ErrorType, the server-supplied message, and the request-id header.

Re-exports§

pub use crate::client::Client;
pub use crate::client::ClientBuilder;
pub use crate::error::Error;
pub use crate::error::ErrorType;
pub use crate::error::Result;
pub use crate::messages::MessagesService;
pub use crate::types::messages::ApiErrorBody;
pub use crate::types::messages::ContentBlock;
pub use crate::types::messages::ImageSource;
pub use crate::types::messages::InputMessage;
pub use crate::types::messages::Message;
pub use crate::types::messages::MessageContent;
pub use crate::types::messages::MessageCreateParams;
pub use crate::types::messages::Metadata;
pub use crate::types::messages::Role;
pub use crate::types::messages::StopReason;
pub use crate::types::messages::SystemPrompt;
pub use crate::types::messages::ToolResultContent;
pub use crate::types::messages::Usage;
pub use crate::types::model;
pub use crate::types::model::Model;

Modules§

auth
Credential handling.
client
HTTP client for the Anthropic API.
config
Static configuration values and environment-variable resolution.
error
Error types.
http
Internal request/response plumbing.
messages
The Messages API surface.
retry
Retry policy.
stream
Streaming message responses.
types
Public data types: request bodies, response bodies, content blocks, models.