Swift SDK
Install and configure the HINOW Swift SDK to integrate over 100 AI models into iOS, macOS, and server-side Swift applications with async/await and Combine support.
The HINOW Swift SDK provides full access to the HINOW REST API with Swift concurrency, Codable models, and SwiftUI integration.
> Note: For API resource documentation with code examples, see the API Reference. This page covers Swift SDK-specific features and configurations.
Installation
Swift Package Manager
Add to your Package.swift:
swift
dependencies: [
.package(url: "https://github.com/hinow-ai/sdk-swift.git", from: "1.0.0")
]Or in Xcode: File -> Add Package Dependencies -> Enter the repository URL.
Requirements
- Swift 5.9+
- iOS 15+ / macOS 12+ / watchOS 8+ / tvOS 15+
Configuration
Basic Setup
swift
import HinowAI
let client = Hinow(apiKey: "your-api-key")Environment Variable
bash
export HINOW_API_KEY=your-api-keyswift
// API key loaded automatically from environment
let client = Hinow()Advanced Configuration
swift
let client = Hinow(
apiKey: "your-api-key",
baseURL: URL(string: "https://api.hinow.ai")!,
timeout: 120,
maxRetries: 3
)Basic Usage
Chat Completions
swift
import HinowAI
let client = Hinow()
let response = try await client.chat.completions.create(
model: "gpt-4o",
messages: [
.system("You are a helpful assistant."),
.user("What is the capital of France?")
],
temperature: 0.7,
maxTokens: 1024
)
print(response.choices[0].message.content)Using Different Models
swift
// OpenAI GPT-4o
let response = try await client.chat.completions.create(
model: "gpt-4o",
messages: [.user("Explain machine learning")]
)
// Anthropic Claude
let response = try await client.chat.completions.create(
model: "claude-sonnet-4-20250514",
messages: [.user("Explain machine learning")]
)
// Meta Llama
let response = try await client.chat.completions.create(
model: "meta-llama/llama-3.3-70b-instruct",
messages: [.user("Explain machine learning")]
)Streaming
swift
let stream = try await client.chat.completions.createStream(
model: "gpt-4o",
messages: [.user("Write a story about a robot")]
)
for try await chunk in stream {
if let content = chunk.choices.first?.delta.content {
print(content, terminator: "")
}
}Function Calling (Tool Use)
swift
let response = try await client.chat.completions.create(
model: "gpt-4o",
messages: [
.user("What is the weather in New York?")
],
tools: [
Tool(
type: .function,
function: FunctionDefinition(
name: "get_weather",
description: "Get the current weather for a location",
parameters: [
"type": "object",
"properties": [
"location": [
"type": "string",
"description": "City and state, e.g., New York, NY"
],
"unit": [
"type": "string",
"enum": ["celsius", "fahrenheit"]
]
],
"required": ["location"]
]
)
)
],
toolChoice: .auto
)
if let toolCalls = response.choices[0].message.toolCalls {
for toolCall in toolCalls {
print("Function: \(toolCall.function.name)")
print("Arguments: \(toolCall.function.arguments)")
}
}Image Generation
swift
let response = try await client.images.generate(
model: "black-forest-labs/flux-1-schnell",
prompt: "A programmer cat wearing glasses, cartoon style",
size: "1024x1024",
quality: "hd"
)
for image in response.data {
print("URL: \(image.url)")
}Text-to-Speech
swift
let response = try await client.audio.speech.create(
model: "openai/tts-1-hd",
input: "Hello! Welcome to HINOW AI.",
voice: "alloy",
speed: 1.0,
responseFormat: "mp3"
)
print("Audio URL: \(response.url)")Embeddings
swift
let response = try await client.embeddings.create(
model: "BAAI/bge-base-en-v1.5",
input: "Machine learning is fascinating"
)
let embedding = response.data[0].embedding
print("Dimensions: \(embedding.count)")Error Handling
swift
import HinowAI
do {
let response = try await client.chat.completions.create(
model: "nonexistent-model",
messages: [.user("Hello")]
)
} catch HinowError.authentication {
print("Invalid API key")
} catch HinowError.insufficientBalance {
print("Insufficient balance")
} catch HinowError.rateLimit(let retryAfter) {
print("Rate limit reached. Retry after: \(retryAfter ?? 0)")
} catch HinowError.badRequest(let message) {
print("Invalid request: \(message)")
} catch HinowError.api(let statusCode, let message) {
print("API Error [\(statusCode)]: \(message)")
} catch {
print("Error: \(error)")
}SwiftUI Integration
swift
import SwiftUI
import HinowAI
struct ChatView: View {
@State private var client = Hinow()
@State private var input = ""
@State private var response = ""
@State private var isLoading = false
var body: some View {
VStack {
ScrollView {
Text(response)
.padding()
}
HStack {
TextField("Message", text: \$input)
.textFieldStyle(.roundedBorder)
Button("Send") {
Task {
isLoading = true
do {
let result = try await client.chat.completions.create(
model: "gpt-4o",
messages: [.user(input)]
)
response = result.choices[0].message.content
input = ""
} catch {
response = "Error: \(error.localizedDescription)"
}
isLoading = false
}
}
.disabled(isLoading || input.isEmpty)
}
.padding()
}
}
}Combine Support
swift
import Combine
import HinowAI
class ChatService {
private let client = Hinow()
private var cancellables = Set<AnyCancellable>()
func sendMessage(_ message: String) -> AnyPublisher<String, Error> {
Future { promise in
Task {
do {
let response = try await self.client.chat.completions.create(
model: "gpt-4o",
messages: [.user(message)]
)
promise(.success(response.choices[0].message.content))
} catch {
promise(.failure(error))
}
}
}
.eraseToAnyPublisher()
}
}

