An Anthropic-focused API client built on Req.
Two layers are shipped together:
The plugin layer —
attach/2registers Anthropic-aware options on a%Req.Request{}, sets auth headers, normalizes errors, and resolves relative URLs against the API base URL. Power users can drop this on any Req struct and call the API directly.The resource layer — modules like
ReqAnthropic.Messages,ReqAnthropic.Models,ReqAnthropic.Files, andReqAnthropic.Batchesbuild a Req viaattach/2and expose a small, Anthropic-shaped API.
Authentication
An API key is resolved at request time in this order:
- The
:api_keyoption passed to the call. Application.get_env(:req_anthropic, :api_key).System.get_env("ANTHROPIC_API_KEY").
If no key can be resolved, ReqAnthropic.AuthError is raised before the
request is sent.
Examples
ReqAnthropic.Messages.create(
model: "claude-haiku-4-5",
max_tokens: 256,
messages: [%{role: "user", content: "ping"}]
)
Req.new()
|> ReqAnthropic.attach(api_key: "sk-...")
|> Req.post!(url: "/v1/messages", json: %{...})
Summary
Functions
Attach the ReqAnthropic plugin to a %Req.Request{}.
Extract rate-limit metadata from a %Req.Response{}.
Send a request through a freshly-built ReqAnthropic client.
Functions
@spec attach( Req.Request.t(), keyword() ) :: Req.Request.t()
Attach the ReqAnthropic plugin to a %Req.Request{}.
Registers the ReqAnthropic-specific options and appends request/response steps to set auth headers, resolve the base URL, and normalize errors.
Options
:api_key- the Anthropic API key. Falls back to the application environment and thenANTHROPIC_API_KEY.:base_url- defaults to"https://api.anthropic.com".:anthropic_version- theanthropic-versionheader value, defaults to"2023-06-01".:beta- a string or list of strings to send asanthropic-beta.:decode_errors- whentrue(default), non-2xx responses are converted into{:error, %ReqAnthropic.Error{}}.
@spec rate_limit(Req.Response.t()) :: ReqAnthropic.RateLimit.t() | nil
Extract rate-limit metadata from a %Req.Response{}.
The ReqAnthropic response step parses anthropic-ratelimit-* and
retry-after headers on every response and stashes a
%ReqAnthropic.RateLimit{} in the response's private data.
{:ok, resp} = ReqAnthropic.Client.build(opts) |> Req.post(...)
ReqAnthropic.rate_limit(resp)
#=> %ReqAnthropic.RateLimit{requests_remaining: 42, ...}
@spec request(keyword()) :: {:ok, Req.Response.t()} | {:error, Exception.t()}
Send a request through a freshly-built ReqAnthropic client.
Equivalent to ReqAnthropic.Client.build(opts) |> Req.request().