anthropic-ai-sdk 0.0.7

An unofficial Rust SDK for Anthropic's Claude AI API
Documentation

Anthropic Rust SDK

Crates.io Documentation License: MIT

An unofficial Rust SDK for the Anthropic API.

Features

  • Full async/await support
  • Comprehensive error handling
  • Pagination support
  • Token counting utilities

Installation

cargo add anthropic-ai-sdk

Quick Start

use anthropic_ai_sdk::clients::AnthropicClient;
use anthropic_ai_sdk::types::message::{
    CreateMessageParams, Message, MessageClient, MessageError, RequiredMessageParams, Role,
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let anthropic_api_key = std::env::var("ANTHROPIC_API_KEY").unwrap();
    let client = AnthropicClient::new::<MessageError>(anthropic_api_key, "2023-06-01").unwrap();

    let body = CreateMessageParams::new(RequiredMessageParams {
        model: "claude-3-5-sonnet-20240620".to_string(),
        messages: vec![Message::new_text(Role::User, "Hello, Claude")],
        max_tokens: 1024,
    });

    match client.create_message(Some(&body)).await {
        Ok(message) => {
            println!("Successfully created message: {:?}", message.content);
        }
        Err(e) => {
            println!("Error: {}", e);
        }
    }

    Ok(())
}

Examples

Check out the examples directory for more usage examples:

API Coverage

  • List Models
  • Create Message
  • Count Message Tokens
  • Stream Message
  • Upload Files
  • Delete Files

Development

Prerequisites

  • Rust 1.81 or later
  • An Anthropic API key

Running Tests

cargo test

Running Examples

Set your API key

export ANTHROPIC_API_KEY="your-api-key"

Run an example

cargo run --example list-models

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Security

If you discover a security vulnerability within this package, please send an e-mail to the maintainers. All security vulnerabilities will be promptly addressed.

Support

For support questions, please use the GitHub Issues.