Server Quickstart (Vapor)
This guide shows how to add the Swift AI SDK to a Vapor app and expose a streaming endpoint.
1) Package.swift
Section titled “1) Package.swift”import PackageDescription
let package = Package( name: "AIService", platforms: [.macOS(.v13)], dependencies: [ .package(url: "https://github.com/vapor/vapor", from: "4.96.0"), .package(url: "https://github.com/teunlao/swift-ai-sdk", branch: "main") ], targets: [ .executableTarget( name: "AIService", dependencies: [ .product(name: "Vapor", package: "vapor"), .product(name: "SwiftAISDK", package: "swift-ai-sdk"), .product(name: "OpenAIProvider", package: "swift-ai-sdk") ] ) ])2) Configure API Key
Section titled “2) Configure API Key”Set OPENAI_API_KEY in your environment.
3) Streaming route (SSE)
Section titled “3) Streaming route (SSE)”import Vaporimport SwiftAISDKimport OpenAIProvider
func routes(_ app: Application) throws { app.get("stream") { req async throws -> Response in let stream = try streamText( model: openai("gpt-4o"), prompt: req.query[String.self, at: "q"] ?? "Hello" )
let sse = makeStreamTextSSEStream(stream.fullStream, includeUsage: false)
let res = Response(status: .ok) res.headers.replaceOrAdd(name: .contentType, value: "text/event-stream")
// Pipe chunks for try await line in sse { try await res.body.write(.init(string: "data: \(line)\n\n")) try await res.body.flush() } return res }}Tip: You can also use
pipeTextStreamToResponsewith a small adapter that conforms toStreamTextResponseWriter.