Anthropic (Claude)

Integrate Claude 3.5 Sonnet, Claude 3 Opus, and more with ProtectMyAPI.

🔮

Supported Models: Claude 3.5 Sonnet, Claude 3 Opus, Claude 3 Haiku

Features

  • ✅ Chat Completions
  • ✅ Vision (image analysis)
  • ✅ PDF document analysis
  • ✅ Function Calling / Tools
  • ✅ 200K context window
  • ✅ Streaming responses

Setup

1. Add your API key to Secrets

In the ProtectMyAPI dashboard:

  1. Go to your app → Secrets
  2. Add a secret named ANTHROPIC_API_KEY
  3. Paste your Anthropic API key as the value

2. Create an endpoint

Create an endpoint with:

  • Name: Anthropic Messages
  • Slug: anthropic-messages
  • Target URL: https://api.anthropic.com/v1/messages
  • Method: POST
  • Auth Type: Custom Header
  • Header Name: x-api-key
  • Header Value: {{ANTHROPIC_API_KEY}}

Add an additional header:

  • anthropic-version: 2023-06-01

Code Examples

import ProtectMyAPI
 
// Simple chat
let response = try await ProtectMyAPI.shared.request(
    endpoint: "anthropic-messages",
    method: .POST,
    body: [
        "model": "claude-3-5-sonnet-20241022",
        "max_tokens": 1024,
        "messages": [
            ["role": "user", "content": "Explain quantum computing simply"]
        ]
    ]
)
 
// With system prompt
let response = try await ProtectMyAPI.shared.request(
    endpoint: "anthropic-messages",
    method: .POST,
    body: [
        "model": "claude-3-5-sonnet-20241022",
        "max_tokens": 1024,
        "system": "You are a helpful coding assistant.",
        "messages": [
            ["role": "user", "content": "Write a Swift function to sort an array"]
        ]
    ]
)
 
// Vision (image analysis)
let visionResponse = try await ProtectMyAPI.shared.request(
    endpoint: "anthropic-messages",
    method: .POST,
    body: [
        "model": "claude-3-5-sonnet-20241022",
        "max_tokens": 1024,
        "messages": [
            [
                "role": "user",
                "content": [
                    [
                        "type": "image",
                        "source": [
                            "type": "base64",
                            "media_type": "image/jpeg",
                            "data": imageBase64
                        ]
                    ],
                    [
                        "type": "text",
                        "text": "What's in this image?"
                    ]
                ]
            ]
        ]
    ]
)

Models

ModelBest ForContextSpeed
claude-3-5-sonnet-20241022Best balance200KFast
claude-3-opus-20240229Complex reasoning200KSlower
claude-3-haiku-20240307Fast, simple tasks200KFastest

Function Calling (Tools)

let response = try await ProtectMyAPI.shared.request(
    endpoint: "anthropic-messages",
    method: .POST,
    body: [
        "model": "claude-3-5-sonnet-20241022",
        "max_tokens": 1024,
        "tools": [
            [
                "name": "get_weather",
                "description": "Get current weather for a location",
                "input_schema": [
                    "type": "object",
                    "properties": [
                        "location": [
                            "type": "string",
                            "description": "City name"
                        ]
                    ],
                    "required": ["location"]
                ]
            ]
        ],
        "messages": [
            ["role": "user", "content": "What's the weather in Tokyo?"]
        ]
    ]
)

PDF Analysis

Claude can analyze PDF documents:

let pdfData = // your PDF as base64
let response = try await ProtectMyAPI.shared.request(
    endpoint: "anthropic-messages",
    method: .POST,
    body: [
        "model": "claude-3-5-sonnet-20241022",
        "max_tokens": 4096,
        "messages": [
            [
                "role": "user",
                "content": [
                    [
                        "type": "document",
                        "source": [
                            "type": "base64",
                            "media_type": "application/pdf",
                            "data": pdfData
                        ]
                    ],
                    [
                        "type": "text",
                        "text": "Summarize this document"
                    ]
                ]
            ]
        ]
    ]
)