Claude API iOS Swift SDK Integration Guide
Is there an official Claude Swift SDK?
No. Anthropic ships official SDKs for Python and TypeScript/JavaScript, but there is no first-party Swift package for Claude. If you're building an iOS app and searching for a "Claude API iOS Swift SDK," the honest answer is: you integrate it yourself using URLSession and plain HTTPS requests, or you route calls through a backend that handles the Claude protocol for you.
This isn't actually a bad thing. The Claude Messages API is a straightforward REST endpoint, and wrapping it in Swift takes maybe 40 lines of code. The real engineering problem for iOS apps isn't "which SDK do I import" — it's "how do I call an LLM API without putting a secret key inside a shipped binary." We'll cover both: the Swift integration itself, and the architecture you actually need in production.
Why you can't call Claude directly from your iOS app
Every API key you compile into an app bundle is extractable. Anyone with the IPA file can decompile it, grep the binary for strings, or intercept TLS with a jailbroken device and a proxy. If your Claude API key lives in your app, it will leak, and once it leaks, someone else is running (and billing) your usage.
This means the correct architecture for a consumer or team iOS app is almost always:
iOS App (Swift) → Your backend / API gateway → Claude API
Your backend holds the real secret. The app only holds a short-lived token or a scoped key that's cheap to rotate if it's ever exposed. This is standard practice for any mobile app calling a paid LLM API — it's not specific to Claude.
Building the Swift client
Assuming you have a backend endpoint (your own server, or a service like SubToAPI sitting between your app and Claude), here's a minimal, dependency-free Swift client using async/await and URLSession:
import Foundation
struct ChatMessage: Codable {
let role: String
let content: String
}
struct ClaudeRequest: Codable {
let model: String
let max_tokens: Int
let messages: [ChatMessage]
}
struct ClaudeContentBlock: Codable {
let text: String?
}
struct ClaudeResponse: Codable {
let content: [ClaudeContentBlock]
}
final class ClaudeClient {
let baseURL: URL
let apiKey: String
init(baseURL: URL, apiKey: String) {
self.baseURL = baseURL
self.apiKey = apiKey
}
func send(_ prompt: String, model: String = "claude-opus-4") async throws -> String {
var request = URLRequest(url: baseURL.appendingPathComponent("v1/messages"))
request.httpMethod = "POST"
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let body = ClaudeRequest(
model: model,
max_tokens: 1024,
messages: [ChatMessage(role: "user", content: prompt)]
)
request.httpBody = try JSONEncoder().encode(body)
let (data, response) = try await URLSession.shared.data(for: request)
guard let http = response as? HTTPURLResponse, http.statusCode == 200 else {
throw URLError(.badServerResponse)
}
let decoded = try JSONDecoder().decode(ClaudeResponse.self, from: data)
return decoded.content.first?.text ?? ""
}
}
This same struct layout works whether the backend is Anthropic's API directly (server-side), or a proxy like SubToAPI that normalizes requests and handles auth for you.
Handling streaming responses in Swift
Chat UIs feel broken without token-by-token streaming. Claude's API streams responses as Server-Sent Events, and URLSession.bytes(for:) gives you an AsyncSequence of lines you can parse without any third-party SSE library:
func streamMessage(_ prompt: String) async throws -> AsyncStream<String> {
var request = URLRequest(url: baseURL.appendingPathComponent("v1/messages"))
request.httpMethod = "POST"
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("text/event-stream", forHTTPHeaderField: "Accept")
let body: [String: Any] = [
"model": "claude-opus-4",
"max_tokens": 1024,
"stream": true,
"messages": [["role": "user", "content": prompt]]
]
request.httpBody = try JSONSerialization.data(withJSONObject: body)
let (bytes, _) = try await URLSession.shared.bytes(for: request)
return AsyncStream { continuation in
Task {
for try await line in bytes.lines {
if line.hasPrefix("data: "), let data = line.dropFirst(6).data(using: .utf8) {
if let chunk = try? JSONDecoder().decode(ClaudeResponse.self, from: data),
let text = chunk.content.first?.text {
continuation.yield(text)
}
}
}
continuation.finish()
}
}
}
Hook that AsyncStream up to a @Published string in your ObservableObject view model, and SwiftUI will re-render as tokens arrive.
Using SubToAPI instead of rolling your own backend
If you don't want to build and maintain a proxy server just to keep your key off the device, SubToAPI is designed exactly for this: it turns your Claude access into an HTTPS API with scoped application keys (sub_live_...) that you can issue per app build, rotate instantly, and monitor from a dashboard. Your iOS app talks to https://api.subtoapi.app/v1/messages instead of a server you have to patch and scale yourself.
Test the endpoint from the command line before wiring up Swift:
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-opus-4",
"max_tokens": 512,
"messages": [{"role": "user", "content": "Summarize this in one sentence."}]
}'
Once that works, drop the same URL and key pattern into the ClaudeClient above — no SDK required on either end. Streaming, tool use, and usage metadata work the same way; see /docs/streaming and /docs/tools for request shapes, and /docs/quickstart to get your first key. Plans start at €9/month on the Solo tier, with team seats on the pricing page for apps with multiple developers shipping builds.
questions
Does Anthropic publish an official iOS/Swift SDK for Claude? No. Official SDKs exist for Python and TypeScript. iOS integration is done via direct HTTPS calls with URLSession, or through a backend/proxy that exposes the same REST interface.
Can I put my Claude API key directly in my iOS app? You shouldn't. Compiled apps can be decompiled and keys extracted, so any key embedded in the binary is effectively public. Route requests through a backend or a scoped-key service instead.
How do I stream Claude responses in a SwiftUI chat view? Use URLSession.bytes(for:) to read the SSE stream line by line, parse each data: event as JSON, and publish the decoded text chunks to an AsyncStream or @Published property that your view observes.