Runtime API
Comprehensive documentation of the SwiftX runtime and handler APIs.
Runtime API
SwiftX provides a clean and powerful API for writing web handlers. This page documents the primary objects and methods you'll use.
Request (Req)
The Req object represents an incoming HTTP request. It provides access to the path, method, headers, parameters, and body.
Prop
Type
Using Path Parameters
When you define a route like /user/:id, you can access the id value:
app.get("/user/:id") { req, _ in
let id = req.param("id") ?? "0"
return .text("Hello user \(id)")
}Response (Res)
The Res object is an enum that represents an HTTP response. You return this from your route handlers.
Prop
Type
Customizing Headers
You can add headers to any response type:
return .text("Hello", headers: ["X-Custom": "Value"])Application (SwiftXApp)
The main entry point for your server, typically used via the global app instance.
Routing Methods
app.get(path, handler)app.post(path, handler)app.put(path, handler)app.delete(path, handler)app.notFound(handler)
Configuration
app.start(port: 5100, development: true): Starts the server. In development mode, detailed logs and execution timings are shown.app.stop(): Gracefully shuts down the server.
Metrics
Monitor the health of your engine in real-time:
let activeWorkers = app.metrics.workers
let totalConnections = app.metrics.connectionsCompatibility Notice
SwiftX supports all standard Swift libraries that run on the target platform.
Important
Platform-specific libraries (e.g., macOS-only libraries using Cocoa/Darwin) are not SwiftX's responsibility. Ensure your dependencies are compatible with the platforms you intend to deploy to (Linux/Windows).
