Expand description
Type-safe Rust client for the Anthropic API.
Every documented Anthropic endpoint is reachable through a typed
namespace handle off Client. Forward-compatible by design –
unknown content blocks, stream events, citations, and similar
discriminated unions round-trip through Other(Value) arms so
new server-side variants don’t break older SDK builds.
§Quick start
use claude_api::{Client, messages::CreateMessageRequest, types::ModelId};
let client = Client::new(std::env::var("ANTHROPIC_API_KEY").unwrap());
let resp = client
.messages()
.create(
CreateMessageRequest::builder()
.model(ModelId::SONNET_4_6)
.max_tokens(256)
.user("Hello!")
.build()?,
)
.await?;
for block in &resp.content {
if let claude_api::messages::ContentBlock::Known(
claude_api::messages::KnownBlock::Text { text, .. },
) = block
{
println!("{text}");
}
}§Module map
Endpoints are organized by Anthropic resource. Each namespace
handle is reached via a method on Client:
| Module | Namespace | Default? |
|---|---|---|
messages | client.messages() | yes |
models | client.models() | yes |
batches | client.batches() | yes |
files | client.files() | yes |
skills | client.skills() | feature skills |
user_profiles | client.user_profiles() | feature user-profiles |
managed_agents | client.managed_agents() | feature managed-agents-preview |
admin | client.admin() | feature admin |
Cross-cutting machinery:
| Module | Purpose |
|---|---|
auth | API-key wrapper + auth::RequestSigner trait |
bedrock | AWS sigv4 bedrock::BedrockSigner (feature bedrock) |
beta | BetaHeader open-string enum for the anthropic-beta header |
retry | retry::RetryPolicy honoring Retry-After |
error | Error with request_id, retry classification |
pagination | Paginated and PaginatedNextPage |
types | Shared primitives: ModelId, Role, Usage, StopReason |
conversation | Multi-turn helper with cumulative usage (feature conversation) |
tool_dispatch | ToolRegistry, agent loop, parallel dispatch, approval gates |
pricing | PricingTable (feature pricing) |
cost_preview | Pre-flight USD estimates (features async + pricing) |
dry_run | Render the would-be HTTP request without sending |
sse | SSE wrapper used by streaming endpoints (feature streaming) |
§Forward compatibility
Every wire-level discriminated union has an Other arm:
messages::ContentBlock– text / image /tool_use/ thinking / … /Other(Value)messages::KnownBlock– the typed variantsmessages::stream::StreamEvent– SSE eventsmessages::citation::Citation– citation kindsBetaHeader– known beta header values +Other(String)
When Anthropic adds a new variant, your code that pattern-matches
on Known(...) continues to compile; the new variant lands in
Other(...) until you upgrade. See the upgrade contract in
the README before bumping past a release that promotes an Other
to Known.
§Error handling
All endpoint methods return Result<T> where
Error carries:
- HTTP status (when the API responded)
request-id(always populated when the server sent one)- Retry classification (
is_retryable()) and parsedRetry-After(retry_after()) - Structured kind (
error::ApiErrorKind) for known error types
The retry::RetryPolicy applied by the client respects
Retry-After by default and uses jittered exponential backoff
otherwise. Disable retries with RetryPolicy::none() if your
caller wraps its own retry logic.
§Observability
Every HTTP request emits a tracing span at debug level with
method, path, and request_id fields. Retries log at warn
with attempt, retry_in_ms, and status. No env-var gating
is required; install a tracing-subscriber to capture the
events.
Re-exports§
pub use beta::BetaHeader;pub use client::Client;asyncpub use client::ClientBuilder;asyncpub use error::Error;pub use error::Result;
Modules§
- admin
admin - Anthropic Admin API.
- auth
- Authentication primitives and the
RequestSignerhook. - batches
- The Batches API: submit a batch of message requests, poll for completion, stream per-request results.
- bedrock
bedrock - AWS Bedrock support: a
RequestSignerthat signs HTTP requests with sigv4. - beta
- Typed
anthropic-betaheader values. - blocking
sync - Blocking (synchronous) variants of the HTTP surface.
- client
async - HTTP client and builder.
- conversation
conversation - Multi-turn conversation helper.
- cost_
preview asyncandpricing CostPreview– estimate the USD cost of a request before sending it.- derive
derive - Procedural-macro re-exports for the
derivefeature. - dry_run
async DryRun– preview the HTTP request that would be sent, without firing it.- error
- Error type, result alias, and wire-format error payload.
- files
- The Files API (beta).
- managed_
agents managed-agents-preview - Anthropic Managed Agents API (preview).
- messages
- The Messages API.
- models
- The Models API.
- pagination
- Generic pagination wrapper used across paginated endpoints.
- pricing
pricing - Per-model pricing and cost calculation.
- retry
asyncorsync - Retry policy honoring
Retry-After. - skills
skills - The Skills API (beta).
- sse
streaming - Low-level Server-Sent Events parsing.
- tool_
dispatch async - Tool-dispatch foundation: the
Tooltrait, plus a registry and agent loop runner (the latter two land in v0.2 tasks #19 and #20). - types
- Foundational shared types:
ModelId,Role,Usage,StopReason. - user_
profiles user-profiles - The User Profiles API (beta).