Rust SDK for Claude Code. See the Claude Code SDK documentation for more information.
Add this to your Cargo.toml:
[dependencies]
claude-code-sdk = "0.1.0"Prerequisites:
- Rust 1.70+
- Node.js
- Claude Code:
npm install -g @anthropic-ai/claude-code
use claude_code_sdk::query;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut stream = query("What is 2 + 2?", None).await?;
while let Some(message) = stream.next().await {
match message {
Ok(msg) => println!("{:?}", msg),
Err(e) => eprintln!("Error: {}", e),
}
}
Ok(())
}use claude_code_sdk::{query, ClaudeCodeOptions, Message};
// Simple query
let mut stream = query("Hello Claude", None).await?;
while let Some(message) = stream.next().await {
if let Ok(Message::Assistant(msg)) = message {
for block in &msg.content {
if let ContentBlock::Text(text_block) = block {
println!("{}", text_block.text);
}
}
}
}
// With options
let options = ClaudeCodeOptions {
system_prompt: Some("You are a helpful assistant".to_string()),
max_turns: Some(1),
..Default::default()
};
let mut stream = query("Tell me a joke", Some(options)).await?;let options = ClaudeCodeOptions {
allowed_tools: Some(vec!["Read".to_string(), "Write".to_string(), "Bash".to_string()]),
permission_mode: Some("acceptEdits".to_string()),
..Default::default()
};
let mut stream = query("Create a hello.rs file", Some(options)).await?;use std::path::PathBuf;
let options = ClaudeCodeOptions {
cwd: Some(PathBuf::from("/path/to/project")),
..Default::default()
};Main async function for querying Claude.
Parameters:
prompt: &str- The prompt to send to Claudeoptions: Option<ClaudeCodeOptions>- Optional configuration
Returns: Result<MessageStream, ClaudeSDKError> - Stream of response messages
See the library documentation for complete type definitions:
ClaudeCodeOptions- Configuration optionsMessage- Enum for different message typesContentBlock- Enum for different content block types
use claude_code_sdk::{
ClaudeSDKError,
query,
};
match query("Hello", None).await {
Ok(mut stream) => {
while let Some(message) = stream.next().await {
match message {
Ok(msg) => println!("{:?}", msg),
Err(e) => eprintln!("Stream error: {}", e),
}
}
}
Err(ClaudeSDKError::CLINotFound) => {
eprintln!("Please install Claude Code");
}
Err(ClaudeSDKError::ProcessError { exit_code, .. }) => {
eprintln!("Process failed with exit code: {}", exit_code);
}
Err(e) => eprintln!("Error: {}", e),
}See the Claude Code documentation for a complete list of available tools.
See the examples/ directory for complete working examples.
MIT