MyNixOS website logo
Description

Servant bindings to Anthropic's Claude API.

This package provides comprehensive and type-safe bindings to Anthropic's Claude API, providing both a Servant interface and non-Servant interface for convenience.

Read the README below for a fully worked usage example.

Otherwise, browse the Claude.V1 module, which is the intended package entrypoint.

claude

Haskell bindings to Anthropic's Claude API using servant.

Example usage

{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns        #-}
{-# LANGUAGE OverloadedLists       #-}
{-# LANGUAGE OverloadedStrings     #-}

import Claude.V1
import Claude.V1.Messages
import Data.Foldable (traverse_)
import qualified Data.Text as Text
import qualified Data.Text.IO as Text.IO
import qualified System.Environment as Environment

main :: IO ()
main = do
    key <- Environment.getEnv "ANTHROPIC_KEY"
    clientEnv <- getClientEnv "https://api.anthropic.com"
    let Methods{ createMessage } = makeMethods clientEnv (Text.pack key) (Just "2023-06-01")

    MessageResponse{ content } <- createMessage _CreateMessage
        { model = "claude-sonnet-4-5-20250929"
        , messages = [ Message{ role = User, content = [ textContent "Hello!" ] } ]
        , max_tokens = 1024
        }

    let display (ContentBlock_Text{ text }) = Text.IO.putStrLn text
        display _ = pure ()
    traverse_ display content

Setup

Using Nix with Flakes (Recommended)

  1. Ensure you have Nix with flakes enabled
  2. Copy the sample environment file:
cp .envrc.sample .envrc
  1. Edit .envrc with your Anthropic API key
  2. Run direnv allow

Manual Setup

cabal build

Environment Variables

Set your Anthropic API key as an environment variable:

# Option 1: Set directly in your shell
export ANTHROPIC_KEY="your-anthropic-api-key"

# Option 2: Using .envrc with direnv (recommended)
cp .envrc.sample .envrc
# Edit .envrc to add your API key
direnv allow

The API key is needed for running the test suite and example programs.

Testing

Run the test suite:

cabal test

Running the Examples

See examples/README.md for descriptions of each example.

cabal run claude-example
cabal run claude-stream-example
cabal run claude-tool-example
cabal run claude-vision-example
cabal run claude-tool-search-example
cabal run claude-programmatic-tool-calling-example
cabal run claude-structured-outputs-example

Advanced Examples

Several examples demonstrate beta features:

Tool Search & Programmatic Tool Calling (advanced-tool-use-2025-11-20):

Structured Outputs (structured-outputs-2025-11-13):

  • Structured Outputs: Constrain Claude's responses to follow a specific JSON schema, or validate tool parameters with strict mode

To enable beta features, use makeMethodsWith with the appropriate beta header:

let options = defaultClientOptions
        { apiKey = key
        , anthropicBeta = Just "structured-outputs-2025-11-13"
        }
let Methods{ createMessage } = makeMethodsWith clientEnv options
Metadata

Version

1.0.1

Platforms (78)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    uefi
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-uefi
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-uefi
  • x86_64-windows