Building a REST API using Vapor and Fluent
Swift on the server enables seamless app development from client-side to server-side. Vapor, a popular framework for building web services in Swift, makes it easy to create REST APIs. This guide walks you through setting up a REST API for managing tasks using Vapor and Fluent.
Details
URL: π https://vapor.codes
Source: π Book: Server-Side Swift
Author: [Author Name]
Tags:
REST API
, Vapor
, Fluent
, Swift
, Server-Side Swift
Platforms Supported: macOS, Linux
Swift Version: 5.x
Getting Started with Vaporβ
Step 1: Install Vapor and Setup the Projectβ
-
Check Swift Version:
swift --version
Ensure you have Swift 5.2 or later.
-
Install Vapor CLI using Homebrew:
brew install vapor
-
Create a New Vapor Project:
vapor new TaskAPI
cd TaskAPI
vapor xcodeSelect
yes
to use Fluent and choose Postgres as the database.
Step 2: Define the Task Modelβ
Create a Task.swift
file under the Models
group:
import Vapor
final class Task: Content {
let id: UUID?
var description: String
var category: String
init(id: UUID? = nil, description: String, category: String) {
self.id = id
self.description = description
self.category = category
}
}
Step 3: Setup Routesβ
Define routes in Routes.swift
to handle task creation and retrieval:
import Vapor
var tasks = [Task]()
func routes(_ app: Application) throws {
app.post("task") { req -> EventLoopFuture<HTTPStatus> in
let task = try req.content.decode(Task.self)
tasks.append(task)
return req.eventLoop.makeSucceededFuture(.ok)
}
app.get("task") { req -> [Task] in
return tasks
}
}
Step 4: Run and Test the APIβ
-
Run the Vapor Project:
vapor build
vapor runVisit
http://127.0.0.1:8080/hello
to verify the setup. -
Test with cURL:
- Create a Task:
curl -H "Content-Type: application/json" -X POST -d '{"description":"Buy milk","category":"Shopping"}' http://127.0.0.1:8080/task
- Retrieve Tasks:
curl http://127.0.0.1:8080/task
- Create a Task:
Adding Fluent for Database Supportβ
Step 5: Integrate Fluent with Postgresβ
Modify configure.swift
to connect Vapor with a PostgreSQL database using Fluent:
import Fluent
import FluentPostgresDriver
import Vapor
public func configure(_ app: Application) throws {
app.databases.use(.postgres(
hostname: "localhost",
username: "vapor_username",
password: "vapor_password",
database: "vapor_database"
), as: .psql)
app.migrations.add(CreateTask())
try app.autoMigrate().wait()
}
Step 6: Define Database Migrationβ
Create a migration for the Task
model in Task.swift
:
import Fluent
struct CreateTask: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema("tasks")
.id()
.field("description", .string, .required)
.field("category", .string, .required)
.create()
}
func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema("tasks").delete()
}
}
Step 7: Update Routes to Use Fluentβ
Replace the in-memory storage with Fluentβs database operations:
import Vapor
import Fluent
func routes(_ app: Application) throws {
app.post("task") { req -> EventLoopFuture<Task> in
let task = try req.content.decode(Task.self)
return task.save(on: req.db).map { task }
}
app.get("task") { req -> EventLoopFuture<[Task]> in
Task.query(on: req.db).all()
}
}
Conclusionβ
This guide covers setting up a REST API using Vapor and Fluent, managing tasks with routes, and integrating with a PostgreSQL database. With this foundation, you can expand your API to include more complex operations.