How to add the Claude API to an iOS app
What the Claude API does for an iOS app
The Claude API turns text in and text out into a product feature: a chat assistant, a summarizer, a writing helper, structured extraction from messy input. You send a list of messages to the Anthropic Messages API and get back the model's reply. The work for an iOS app is plumbing the request and reply safely, which means keeping the API key off the device and handling the network call.
Adding it with AppFlight, and manually
In AppFlight you describe the AI feature, and the AI generates the SwiftUI plus a Supabase edge function that calls Claude. AppFlight classifies the Anthropic key as a backend secret, so it lives server-side in your own Supabase and never reaches the app. The app talks only to your edge function. Note this is separate from the AI that builds your app: AppFlight uses Claude Code or Codex locally on your Mac to write the Swift, while this connector is the runtime AI feature inside the shipped app.
Manually, you stand up an endpoint (a Supabase edge function, a small server, a serverless function) that reads the Anthropic key from its environment, posts to https://api.anthropic.com/v1/messages with the anthropic-version header, and returns the reply. Your iOS app calls that endpoint, not Anthropic.
A SwiftUI example
The app calls your backend, which holds the key and talks to Claude:
import SwiftUI
struct ChatResponse: Decodable { let reply: String }
func askClaude(_ prompt: String) async throws -> String {
// Your backend (e.g. a Supabase edge function) holds the Anthropic key
// and forwards the prompt to the Messages API. The app never sees the key.
var request = URLRequest(url: URL(string: "https://your-project.functions.supabase.co/chat")!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try JSONEncoder().encode(["prompt": prompt])
let (data, _) = try await URLSession.shared.data(for: request)
return try JSONDecoder().decode(ChatResponse.self, from: data).reply
}
Common pitfalls
The most serious mistake is hardcoding the Anthropic API key in the app, since it can be extracted from the binary and used to run up charges on your account. Calling Anthropic directly from the device leaks the key even over HTTPS. Not handling slow responses leaves the UI frozen, so call the backend with async URLSession and show a loading state. Skipping a timeout and an error path means a dropped network leaves the user stuck. If you want analytics, retries, and provider switching, that logic belongs in your backend, not the app.
FAQ
Can I put my Anthropic API key in the iOS app?
No. The key is a secret, and anything shipped in an app binary can be extracted. Route Claude calls through a backend you control, for example a Supabase edge function, and keep the key there. The app calls your backend, your backend calls Anthropic.
Which model should I use?
Claude Opus 4.8 is the most capable for hard reasoning, with Claude Sonnet 4.6 for a faster and cheaper balance and Claude Haiku 4.5 for simple, latency-sensitive tasks. Use the exact model IDs claude-opus-4-8, claude-sonnet-4-6, and claude-haiku-4-5 in the request.
What endpoint does the iOS app call?
Your app calls your own backend endpoint. The backend calls the Anthropic Messages API at /v1/messages with the API key and anthropic-version header, then returns the text to the app.