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:
- Go to your app → Secrets
- Add a secret named
ANTHROPIC_API_KEY - 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
| Model | Best For | Context | Speed |
|---|---|---|---|
claude-3-5-sonnet-20241022 | Best balance | 200K | Fast |
claude-3-opus-20240229 | Complex reasoning | 200K | Slower |
claude-3-haiku-20240307 | Fast, simple tasks | 200K | Fastest |
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"
]
]
]
]
]
)