Claude for Clojure: the Claude Messages API as functions over plain data.
A request body is a map that mirrors the API's JSON, with keyword keys in the API's own spelling:
(def client (client)) (create client {:model "claude-opus-5-5" :max_tokens 1024 :messages [{:role "user" :content "Hello"}]})
The result is the response body as a map.
Claude for Clojure: the Claude Messages API as functions over plain data.
A request body is a map that mirrors the API's JSON, with keyword keys
in the API's own spelling:
(def client (client))
(create client {:model "claude-opus-5-5"
:max_tokens 1024
:messages [{:role "user" :content "Hello"}]})
The result is the response body as a map.(client)(client options)A client: an immutable map of configuration plus a shared HTTP client. Options override the defaults and the environment:
:api-key sent as x-api-key; default ANTHROPIC_API_KEY :auth-token sent as a bearer token; default ANTHROPIC_AUTH_TOKEN :base-url default ANTHROPIC_BASE_URL, else https://api.anthropic.com :version the anthropic-version header :betas beta names sent on every call :timeout-ms for non-streaming calls :retry {:max :base-ms :cap-ms}
An :api-key given here wins over an :auth-token. To vary a setting for some calls, assoc onto the client.
A client: an immutable map of configuration plus a shared HTTP client.
Options override the defaults and the environment:
:api-key sent as x-api-key; default ANTHROPIC_API_KEY
:auth-token sent as a bearer token; default ANTHROPIC_AUTH_TOKEN
:base-url default ANTHROPIC_BASE_URL, else https://api.anthropic.com
:version the anthropic-version header
:betas beta names sent on every call
:timeout-ms for non-streaming calls
:retry {:max :base-ms :cap-ms}
An :api-key given here wins over an :auth-token. To vary a setting for
some calls, assoc onto the client.(count-tokens client body)(count-tokens client body opts)Count the input tokens of a message request without creating it: POST /v1/messages/count_tokens. Returns {:input_tokens n}.
Count the input tokens of a message request without creating it:
POST /v1/messages/count_tokens. Returns {:input_tokens n}.(create client body)(create client body opts)Create a message: POST /v1/messages. Returns the message as a map.
Check :stop_reason before reading :content. opts are as for request.
Create a message: POST /v1/messages. Returns the message as a map. Check :stop_reason before reading :content. opts are as for `request`.
(execute client http-request)Send a request map from request and return response of the reply,
retrying as the client's :retry policy says.
Send a request map from `request` and return `response` of the reply, retrying as the client's :retry policy says.
(final-message events)Fold a stream of event payloads into the message that create would
have returned.
Fold a stream of event payloads into the message that `create` would have returned.
(lines client http-request xf)A reducible over the lines of a response body, passed through the transducer xf, for endpoints that answer with a stream of lines.
Nothing is sent until the result is reduced, and each reduction is a
new request. The connection closes when the reduction finishes, stops
early with reduced, or throws. A failure before the body starts is
retried like any other call; one after it is thrown. There is no total
timeout, since a long body is normal.
A reducible over the lines of a response body, passed through the transducer xf, for endpoints that answer with a stream of lines. Nothing is sent until the result is reduced, and each reduction is a new request. The connection closes when the reduction finishes, stops early with `reduced`, or throws. A failure before the body starts is retried like any other call; one after it is thrown. There is no total timeout, since a long body is normal.
(request client body)(request {:keys [api-key auth-token base-url version] :as client}
body
{:keys [path method query betas headers timeout-ms]
:or {path "/v1/messages" method :post}})The HTTP request for a call, as data: {:method :url :headers :body :timeout-ms}. Pure. body is the JSON body as a map, or nil for none. Options:
:path default "/v1/messages" :method default :post :query a map of query parameters :betas beta names for this call, added to the client's :headers extra headers, which win over the ones cl4cl sets :timeout-ms overrides the client's
With execute or lines this reaches any endpoint; with response it
lets a caller use any HTTP transport.
The HTTP request for a call, as data: {:method :url :headers :body
:timeout-ms}. Pure. body is the JSON body as a map, or nil for none.
Options:
:path default "/v1/messages"
:method default :post
:query a map of query parameters
:betas beta names for this call, added to the client's
:headers extra headers, which win over the ones cl4cl sets
:timeout-ms overrides the client's
With `execute` or `lines` this reaches any endpoint; with `response` it
lets a caller use any HTTP transport.(response {:keys [status body] :as http-response})The result of a call, from an HTTP response {:status :headers :body} whose :body is a string. A 2xx response gives the parsed body, with the status, headers and request id as metadata under :cl4cl/ keys. Any other status throws the ex-info described in cl4cl.error. Pure.
The result of a call, from an HTTP response {:status :headers :body}
whose :body is a string. A 2xx response gives the parsed body, with the
status, headers and request id as metadata under :cl4cl/ keys. Any
other status throws the ex-info described in cl4cl.error. Pure.(stream client body)(stream client body opts)Create a message and stream it. Returns a reducible of event payloads,
maps such as {:type "content_block_delta" :index 0 :delta {...}}.
It behaves as lines describes.
(run! prn (stream client body)) (final-message (stream client body)) (transduce (map observe) cl4cl.stream/accumulate (stream client body))
Create a message and stream it. Returns a reducible of event payloads,
maps such as {:type "content_block_delta" :index 0 :delta {...}}.
It behaves as `lines` describes.
(run! prn (stream client body))
(final-message (stream client body))
(transduce (map observe) cl4cl.stream/accumulate (stream client body))cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |