hyperproxy
Start free

Documentation / Swift · Claude

Connect Claude to your Swift app

This recipe connects Claude through a HyperProxy gateway using the published Swift SDK 0.5.0. You need Swift 6.2, iOS 15 or later, a provider account with API access and a HyperProxy service. Provider usage is billed separately. Start with a non-streaming request, then add device security before shipping.

Updated · HyperProxy team

Call Claude with the Messages API

Add the HyperProxyAnthropic product from HyperProxySwift 0.5.0. Use Swift 6.2 and iOS 15 or later. Create an Anthropic service in the dashboard, save the provider credential there and mint an app key. Use that service's gateway URL in the app.

The Messages API uses /v1/messages, a required max_tokens and an anthropic-version header. Choose an available model ID in your Claude account. The SDK sends the HyperProxy app key; the gateway supplies provider authentication.

Anthropic.swift · SDK 0.5.0
import Foundation
import HyperProxyAnthropic

// HyperProxySwift 0.5.0; choose a Claude model enabled for your account.
func requestClaude(
  gatewayURL: URL, appKey: String, model: String, prompt: String,
  session: URLSession = .shared
) async throws -> HyperProxyJSONValue {
  let service = HyperProxy.anthropic(
    gatewayURL: gatewayURL, appKey: appKey, session: session
  )
  let body: HyperProxyJSONValue = [
    "model": .string(model), "max_tokens": 256,
    "messages": [["role": "user", "content": .string(prompt)]]
  ]
  return try await service.messagesCreate
    .header("anthropic-version", "2023-06-01")
    .json(body)
    .jsonValue()
}

// Display a useful error without exposing response bodies or credentials.
func claudeErrorMessage(_ error: Error) -> String {
  if case HyperProxyError.httpStatus(let code, _, _) = error {
    switch code {
    case 401, 403: return "Check the service app key, device proof and provider access."
    case 429: return "Check quota and rate limits before retrying."
    default: return "AI request failed (HTTP \(code))."
    }
  }
  return "Check your connection and try again."
}

Call requestClaude(gatewayURL:appKey:model:prompt:) from an async task. Read text blocks from content[] where type is text. Tool-use blocks need their own UI and follow-up request. Update UI state on the main actor; show claudeErrorMessage for caught errors and cancel the task when the screen closes.

Check usage.input_tokens and usage.output_tokens in the response, then compare the request's model and token dimensions in HyperProxy. Cache-read and cache-creation tokens are separate billable dimensions; do not apply a single input rate to every token.

This recipe uses a completed JSON response. Streaming Messages usage arrives across events; consume the complete stream before evaluating the final total.

Protocol reference: Claude Messages API. Validation on 8 October 2026: the exact code compiled and ran against SDK tag 0.5.0 with controlled responses. Checks cover the route, app-key and version headers, JSON, decoded usage and HTTP 429. Verify a real request separately for your provider entitlement, device policy and billing.

Verify it works

Run one request with your own service gateway URL, app key and an available Claude model. Confirm the response, then open Project → Requests and find its HTTP status and request ID. In Overview, verify the model, token counts and cost coverage. Use Setup → Check recorded data to confirm the connection.

Troubleshooting

401 or 403

Verify that the app key belongs to this service, has not expired and meets the configured device proof requirements. Check provider entitlement separately; inspect the dashboard error code before rotating keys.

429 or exhausted quota

Inspect whether the limit is from HyperProxy or the provider. Check account quota, service limits and provider usage. Honor Retry-After when present and use bounded backoff; do not loop automatically.

Empty text or unknown cost

Inspect the provider response shape and content policy result. Missing text may be a valid tool or safety response. Missing prices or usage must stay unknown; do not report a free request.

Full error-code reference

Next steps

1,000 requests per month shared across projects. No card required. Provider charges are separate.