class

Anthropic::Client

Inherits Reference < Object

Constants

API_VERSION = "2023-06-01"
DEFAULT_BASE_URL = "https://api.anthropic.com"
DEFAULT_INITIAL_DELAY = 0.5
DEFAULT_MAX_DELAY = 8.0
DEFAULT_MAX_RETRIES = 2

Constructors

new(api_key : String | Nil = nil, auth_token : String | Nil = nil, base_url : String | Nil = nil, timeout : Time::Span = 600.seconds, max_retries : Int32 = DEFAULT_MAX_RETRIES, initial_retry_delay : Float64 = DEFAULT_INITIAL_DELAY, max_retry_delay : Float64 = DEFAULT_MAX_DELAY, default_headers : Hash(String, String) = {} of String => String, default_query : Hash(String, String) = {} of String => String, middleware : Array(Middleware) = [] of Middleware, webhook_key : String | Nil = nil, proxy : String | Nil = nil)
Source

Instance methods

beta

Beta namespace for beta features

client.beta.messages.create(
  betas: ["structured-outputs-2025-12-15"],
  ...
)
Source
completions

[Legacy] Text Completions API.

completion = client.completions.create(
  model: "claude-haiku-4-5-20251001",
  prompt: "\n\nHuman: Hello!\n\nAssistant:",
  max_tokens_to_sample: 256
)
Source
delete(path : String, extra_headers : Hash(String, String) | Nil = nil) : HTTP::Client::Response
Source
files

Files API for uploading and managing files.

file = client.files.upload(Path["document.pdf"])
Source
get(path : String, params : QueryParams | Nil = nil, extra_headers : Hash(String, String) | Nil = nil) : HTTP::Client::Response
Source
get_raw(path : String, extra_headers : Hash(String, String) | Nil = nil) : IO::Memory

GET request returning raw response body (for binary downloads)

Source
get_stream(path : String, extra_headers : Hash(String, String) | Nil = nil, &)
Source
messages

Resource accessors

Source
middleware

The configured middleware chain (outermost first).

Source
models
Source
post(path : String, body, extra_headers : Hash(String, String) | Nil = nil) : HTTP::Client::Response

POST with any JSON::Serializable body

Source
post_multipart(path : String, file : IO, filename : String, content_type : String = "application/octet-stream", extra_headers : Hash(String, String) | Nil = nil, form_fields : Hash(String, String) | Nil = nil) : HTTP::Client::Response

POST multipart form data (for file uploads)

Source
post_multipart_files(path : String, files : Array(FileUpload), form_fields : Hash(String, String) | Nil = nil, extra_headers : Hash(String, String) | Nil = nil) : HTTP::Client::Response

POST multipart form data with multiple files and optional form fields

Source
post_stream(path : String, body, extra_headers : Hash(String, String) | Nil = nil, &)

POST with streaming response

Source
skills

Skills API for managing skills.

skills = client.skills.list
Source
webhook_key

Webhook signing key for BetaWebhooks#unwrap. Explicit argument wins, falling back to ANTHROPIC_WEBHOOK_SIGNING_KEY.

Source
with_middleware(*middleware : Middleware) : Client

Returns a copy of this client with middleware appended after the client's existing middleware (which stays outermost).

scoped = client.with_middleware(LoggingMiddleware.new)
Source
with_options(timeout : Time::Span | Nil = nil, max_retries : Int32 | Nil = nil, extra_headers : Hash(String, String) | Nil = nil, extra_query : Hash(String, String) | Nil = nil, middleware : Array(Middleware) | Nil = nil, api_key : String | Nil = nil, auth_token : String | Nil = nil, base_url : String | Nil = nil, proxy : String | Nil = nil) : Client

Returns a copy of this client with per-request overrides applied.

Upstream SDKs take requestOptions on every call; the Crystal equivalent is a scoped copy, so every resource through it picks the overrides up. middleware replaces the chain; credentials, base URL, and proxy override the copy's connection settings (handy for per-tenant scoping):

scoped = client.with_options(timeout: 30.seconds, max_retries: 0)
scoped.messages.create(model: "...", max_tokens: 64, messages: [...])
Source

Nested types