Crate anthropic_api

Source
Expand description

§Anthropic Rust SDK

An unofficial Rust library for interacting with the Anthropic API. This library provides an asynchronous interface to Anthropic’s services, allowing Rust developers to seamlessly integrate Anthropic’s AI capabilities into their applications.

§Features

  • Asynchronous API Requests: Leverage Rust’s async capabilities for efficient API interactions
  • Message API: Send and receive messages, similar to chat-based interactions
  • Tool Use: Integrate external tools that the AI can call during responses
  • Streaming Responses: Receive real-time streamed responses from the API

§Basic Usage

use anthropic_api::{messages::*, Credentials};

#[tokio::main]
async fn main() {
    // Load credentials from the environment
    let credentials = Credentials::from_env();

    // Create a message
    let messages = vec![Message {
        role: MessageRole::User,
        content: MessageContent::Text("Hello, Claude!".to_string()),
    }];

    // Send the message to the Anthropic API
    let response = MessagesBuilder::builder("claude-3-7-sonnet-20250219", messages.clone(), 1024)
        .credentials(credentials.clone())
        .create()
        .await
        .unwrap();

    // Print the assistant's response
    if let Some(ResponseContentBlock::Text { text }) = response.content.first() {
        println!("Assistant: {}", text.trim());
    }
}

Modules§

admin
messages
Messages API
models
Models API

Structs§

AnthropicError
Represents an error returned by the Anthropic API
AnthropicErrorResponse
Represents an error response from the Anthropic API
Credentials
Holds the API key and base URL for an Anthropic-compatible API.
Usage
Represents token usage statistics for a request and response

Enums§

ApiResponse
Represents a response from the Anthropic API, which can be either a success or an error

Statics§

DEFAULT_BASE_URL
Default base URL for the Anthropic API

Type Aliases§

ApiResponseOrError
Result type for Anthropic API responses