ReqAnthropic (ReqAnthropic v0.2.1)

Copy Markdown View Source

An Anthropic-focused API client built on Req.

Two layers are shipped together:

Authentication

An API key is resolved at request time in this order:

  1. The :api_key option passed to the call.
  2. Application.get_env(:req_anthropic, :api_key).
  3. 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

attach(request, options \\ [])

@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 then ANTHROPIC_API_KEY.
  • :base_url - defaults to "https://api.anthropic.com".
  • :anthropic_version - the anthropic-version header value, defaults to "2023-06-01".
  • :beta - a string or list of strings to send as anthropic-beta.
  • :decode_errors - when true (default), non-2xx responses are converted into {:error, %ReqAnthropic.Error{}}.

rate_limit(response)

@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, ...}

request(opts)

@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().