← thecodex.expert · The Codex Family of Knowledge
Swift

Server-Side Swift

The exact same async/await syntax used in an iOS app works identically in a Vapor server running on Linux — genuinely one language, one set of concurrency primitives, across both.

Swift 6 Same language, same async/await Last verified:
Canonical Definition

Vapor is built on SwiftNIO, Apple's own low-level, event-driven networking library, providing routing, middleware, and database integration (via Fluent, Vapor's ORM) for building real HTTP APIs and web applications. It runs on Linux in production via Swift's cross-platform toolchain (covered in swift-setup), with no Xcode or macOS required for either development or deployment. The genuinely notable point: this isn't a separate, cut-down "server Swift" — the same language features, including async/await and Codable, work identically whether the surrounding code targets iOS or a Linux server.

A basic Vapor route

The route handler is genuinely just an async function — the same async/await syntax from anywhere else in Swift, with Vapor's routing simply calling it when a matching request arrives.

Swiftroutes.swift
import Vapor

func routes(_ app: Application) throws {
    app.get("hello") { req async -> String in
        "Hello, World!"
    }

    app.get("users", ":id") { req async throws -> String in
        let id = req.parameters.get("id") ?? "unknown"
        return "User \(id)"
    }
}

Returning Codable data as JSON

Returning a Codable type directly from a route handler serializes it to JSON automatically — the exact same Codable mechanism covered elsewhere, working identically here in a server context.

Swiftjson_route.swift
struct User: Content {   // Content: Vapor's Codable-based response type
    let id: Int
    let name: String
}

app.get("api", "user") { req async -> User in
    User(id: 1, name: "Alice")   // automatically serialized to JSON
}

Running on Linux: the same toolchain, deployed

The exact same code, compiled with the exact same Swift toolchain used for local development, runs in production on a Linux server — no separate "server dialect" of Swift exists.

Swiftterminal
$ swift build -c release
$ ./.build/release/App serve --env production

Sources

1
Vapor. Vapor Documentation, docs.vapor.codes.