Connect OpenAI to your Swift app
This recipe uses the published HyperProxySwift 0.5.0 API with Swift 6.2 and iOS 15 or later. Create an OpenAI gateway service first. Copy its gateway URL and app key into the example; keep the OpenAI provider credential out of the app bundle.
Updated · HyperProxy team
Add HyperProxySwift
Swift 6.2, iOS 15+, macOS 13+, visionOS 1+, and watchOS 9+.
dependencies: [
.package(
url: "https://github.com/Tsvihun/HyperProxySwift.git",
.upToNextMinor(from: "0.5.0")
)
]
Add the provider product your target uses, for example .product(name: "HyperProxyOpenAI", package: "HyperProxySwift"). The examples on this page use the published 0.5.0 API, available through Swift Package Manager or CocoaPods from the Git tag. The full 0.5.0 graph is not yet available in CocoaPods Trunk; use the Git instructions. Read the release changelog for catalog and API changes.
Before shipping. Review your app archive's privacy manifest and the SDK's provenance status. Use the release tag for reproducible builds.
Proxied mode
Use this for shipped mobile apps. The app receives only an app key; HyperProxy keeps the encrypted server half and reconstructs the credential per request.
import Foundation
import HyperProxyOpenAI
let gatewayURL = URL(string: "https://api.hyperproxyai.com/<project>/<service>")!
let appKey = "<app-key>"
let openAI = HyperProxy.openAI(gatewayURL: gatewayURL, appKey: appKey)
func requestGreeting() async throws -> String {
let response = try await openAI.responsesCreate(
OpenAIResponseRequest(input: "Hello", model: .gpt5)
)
var text = ""
for item in response.output {
guard case .outputMessage(let message) = item else { continue }
for content in message.content {
if case .outputTextContent(let part) = content { text += part.text }
}
}
return text
}
Replace both placeholders with values from the same OpenAI service. Keep the client for reuse and call try await requestGreeting() from an async throws function or a Task that handles errors. Select a model enabled in your provider account; provider charges are separate from HyperProxy billing. This helper collects text from the response's typed output items. For refusals or tool calls, inspect their content kinds separately. Published SDK guide.
Stream, upload, poll, and connect in realtime
Typed provider calls and generic requests share the same transport layer. Use the response mode the upstream documents without flattening its payload.
Typed SSE and JSONLDocumented event streams have generated …Stream methods. Raw event and line streams remain available for preview APIs.
Multipart and progressSend images, audio, documents, and forms with upload progress; consume large binary responses incrementally.
WebSocket and realtimeOpen provider-native realtime sockets, exchange typed JSON or binary frames, and use the optional PCM16 capture/playback module.
Polling and paginationPoll asynchronous jobs with bounded policies and traverse cursor-based collections while keeping response metadata.
for try await chunk in try openAI.chatCompletionsCreateStream(
OpenAICreateChatCompletionRequest(
messages: [
.chatCompletionRequestUserMessage(
OpenAIChatCompletionRequestUserMessage(
content: "Write one sentence",
role: .user
)
)
],
model: .gpt5,
streamOptions: .init(includeUsage: true)
)
) {
print(chunk.choices.first?.delta.content ?? "", terminator: "")
}
includeUsage: true requests the final provider usage chunk for cost accounting. Keep it enabled for supported chat streams. Cancel the owning Task when the user leaves; update UI on its appropriate actor. Provider recipes.
Verify it works
Call requestGreeting() from an async Task and display its returned text on the UI actor. Check the corresponding request in HyperProxy. For streaming, consume the stream to completion to receive final usage; cancel the Task when the user leaves the screen.
Troubleshooting
Package product is missing
Add HyperProxyOpenAI to the target using the 0.5.0 package. The Git-tag CocoaPods instructions are the supported alternative for the complete package graph.
Device proof required
Configure the service’s Apple identity and the SDK’s device security before enabling App Attest. Test App Attest on a supported physical device.
Stream shows text but incomplete usage
Use includeUsage where the provider supports it and inspect the terminal usage chunk. A cancelled stream may leave accounting incomplete; do not infer zero cost.
Next steps
1,000 requests per month shared across projects. No card required. Provider charges are separate.