Skip to content

Server Quickstart (Vapor)

This guide shows how to add the Swift AI SDK to a Vapor app and expose a streaming endpoint.

5.10
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")
]
)
]
)

Set OPENAI_API_KEY in your environment.

import Vapor
import SwiftAISDK
import 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 pipeTextStreamToResponse with a small adapter that conforms to StreamTextResponseWriter.