Swift Vapor

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

The reference material consulted while preparing these pages is listed in the section bibliography.

This section is written against Vapor 4.122.2 (the latest 4.x release, published 2026-09-17) on Swift 6: the local toolchain is Swift 6.4 and the latest swift.org release is 6.4.1, and every example compiles in the Swift 6 language mode. Vapor 5 exists only as a beta (5.0.0-beta.4, 2026-10-13) and is not generally available, so what is already visible in it is marked as upcoming and never as current behaviour. Versions were checked on 2026-10-17.

What Vapor is

Vapor is an asynchronous, Swift-native web framework built on SwiftNIO. It routes HTTP requests to async handlers, decodes and encodes typed Content, and ships companion packages for the database layer (Fluent and SQLKit), HTML templates (Leaf), Redis, background jobs (Queues), JWT and push notifications (APNs). One language and one package manager (SwiftPM) cover the server, the shared models and the Apple clients, and the same binary runs on macOS and Linux.

The section is for developers who know some Swift and want to build production web services, JSON APIs and server-rendered sites. The language itself is covered in the Swift Reference and these pages link there instead of repeating it. If the server is the back end of an iPhone, iPad or Mac app, the Apple Platforms section covers the client side (SwiftUI, networking, notifications), and Full-Stack Swift with iOS Clients joins the two with a shared package.

The Bookshelf application

The Bookshelf application: iOS and macOS apps, browsers and API clients reach a reverse proxy, then the Vapor service with middleware, router, controllers and Fluent models, which uses PostgreSQL, Redis, S3 storage, APNs and an email provider, plus a Queues worker started from the same binary

Every page builds part of Bookshelf, an original library application: a catalogue of books and authors, loans with due dates, reviews, user accounts, a server-rendered web UI, live updates over WebSockets and Server-Sent Events, and the operational pieces of a real service. The Fluent models are Author, Book, Review, User, Tag, BookTag and Loan. The code is split into two packages. BookshelfShared holds the Codable, Sendable DTOs that the server and the clients both import, with no Vapor dependency. BookshelfServer is the Vapor package: its executable target App has entrypoint.swift, configure.swift, routes.swift and the Controllers/, Models/, Migrations/, DTOs/, Jobs/, Commands/, Middleware/ and Services/ folders under Sources/App/, with Resources/Views/, Public/ and the Tests/AppTests/ test target beside them. The layout is explained in Project Structure & SwiftPM, which also shows how to split the server into a library and a thin executable, and Full-Stack Swift with iOS Clients shows the iOS clients importing BookshelfShared.

New here? Read in this order

  1. Introduction & Architecture, Installation & Toolchain and Getting Started — the stack and a first service

  2. Project Structure & SwiftPM and Swift Concurrency in Vapor — how the code is laid out and why blocking matters

  3. Routing, Requests & Responses and Content & Codable — the request side

  4. Validation, Error Handling and Middleware — the rest of the request pipeline

  5. Fluent Overview & Drivers, Fluent Models, Fluent Relations and Migrations & Schema — a first real database

  6. Authentication and JSON Web Tokens (JWT) — who is calling

  7. Then the web UI, API, background work and operations groups below in any order; finish with Testing and Deployment with Docker.

Coming from Vapor 2 or 3, from an older book, or from another framework? Start with What’s New, Upgrading & Vapor 5 and Ecosystem & Framework Comparison.

mindmap root((Swift Vapor)) Foundations Architecture and SwiftNIO Toolchain and getting started Project structure and SwiftPM Lifecycle and concurrency Routing and HTTP Routing and controllers Requests, Content, validation Errors and middleware Files, HTTP client, configuration Data Fluent models and relations Migrations and queries SQLKit and MongoDB Redis and caching Views and real-time Leaf and HTML DSLs Forms and sessions WebSockets and SSE Security Authentication and sessions JWT and OAuth Authorization and hardening APIs and background work DTOs, versioning, OpenAPI Queues and scheduled jobs APNs, email, commands iOS clients Operations Testing Logging, tracing, metrics Docker and deployment Performance and microservices Reference Vapor 5 and upgrading Ecosystem Cheat sheet

What’s covered

Foundations

Routing and HTTP

  • Routing — methods, path parameters, wildcards, groups and body collection or streaming.

  • Controllers & Route Collections — organising routes with RouteCollection and a complete CRUD controller.

  • Requests & Responses — an HTTP refresher plus the Request and Response types, bodies and streaming.

  • Content & Codable — the Content protocol, JSON, forms and multipart bodies, query decoding and coders.

  • Validation — the Validatable protocol, validators and the 400 error shape.

  • Error Handling — Abort, AbortError, the error middleware and custom error responses.

  • Middleware — the responder chain, ordering, writing your own middleware and the built-ins.

  • Static Files & File I/O — FileMiddleware, caching headers, ranges and non-blocking file access.

  • File Uploads & Streaming — multipart uploads, streamed bodies, validation, S3 storage and safe names.

  • HTTP Client — calling other services with req.client, timeouts and testable clients.

  • Configuration & Environment — twelve-factor configuration, environments, typed settings and secrets.

  • Services, Storage & Dependency Injection — dependency injection without a container using Application and Request extensions.

  • Server Configuration — the HTTP server settings: binding, HTTP/2, TLS, compression and limits.

Data

Views, web apps and real-time

Security

APIs, integration and background work

Operations

Reference and evolution

Topic Where

Swift language, concurrency, packages and testing

Swift Reference

SwiftUI, URLSession networking and the clients of a Vapor API

Apple Platforms; in particular Networking

Push notifications on the device (the server side is APNs Push Notifications)

Notifications and Live Activities

OAuth 2.0, OpenID Connect and JWT in depth

OAuth

CORS in the browser

CORS

Container images and Compose

Docker

Running services in a cluster

Kubernetes

SQL and PostgreSQL

SQL

Redis data structures and operations

Redis

MongoDB documents, queries and operations

MongoDB

Prometheus, PromQL and alerting

Prometheus

OpenTelemetry concepts and exporters

There is no stand-alone OpenTelemetry section yet; Tracing, Metrics & OpenTelemetry covers it for Vapor, and OpenTelemetry in axum shows the same pipeline in Rust

Other backend frameworks

Backend Development and Ecosystem & Framework Comparison

A comparison of backend and web frameworks across languages is planned (issue #263), and further API-design, security, resilience and observability topics are tracked in issues #196 and #236. End-to-end browser testing with Playwright, Cypress and Selenium is covered in the E2E Testing in the Browser section. Chat, videoconference and WebSockets are covered in the WebRTC & WebSockets section.

Bibliography

Books

  • Tim Condon, Tanner Nelson, Jonas Schwartz and Logan Wright, Server-Side Swift with Vapor: Building Web APIs and Web Apps, 3rd edition. Razeware (raywenderlich.com, now Kodeco), 2020. ISBN 978-1-950325-27-6. Publisher page; companion code. Consulted in its early-access build, in which chapters 1-13 target Vapor 4.0 and the rest still show Vapor 3; the final edition is on the publisher page.

  • Angus Yeung, Hands-On Server-Side Web Development with Swift: Build dynamic web apps by leveraging two popular Swift web frameworks: Vapor 3.0 and Kitura 2.5, 1st edition. Packt, 2018 (November). ISBN 978-1-78934-117-1 (print), 978-1-78934-586-5 (eBook). Publisher page; companion code.

  • Ankur Patel, Hands-On Full-Stack Development with Swift: Develop full-stack web and native mobile applications using Swift and Vapor, 1st edition. Packt, 2018 (March). ISBN 978-1-78862-524-1 (print), 978-1-78862-627-9 (eBook). Publisher page; companion code.

The books are consulted references, not the primary source. All three predate current Vapor 4 practice (async/await, Swift 6, VaporTesting); on any discrepancy, and wherever a book is silent on a later feature, the official documentation wins, and the pages say so in prose (collected on What’s New, Upgrading & Vapor 5).

Community packages and services