Creating Your First Swift Backend Using Vapor

Petr Pavlík
The Swift Web Developer
3 min readNov 27, 2016

This tutorial will walk you through what it takes to create a basic server-side web app you can build on, and deploy it to Heroku. I’ve tried to make this article easy to understand for both people with iOS/macOS background and backend developers currently using other platforms, like node.js.

Install Xcode

The easiest way is to download it from the App Store for free.

Install Vapor Toolbox

Open terminal and type in following command to install Vapor toolbox, we will use it to generate a skeleton Vapor project and deploy it to Heroku.

$ curl -sL toolbox.vapor.sh | bash

Create New Project

Open terminal, navigate to where you want to have your source code and execute following command. replace SwiftPackageCatalogVapor with whatever you want your project to be called.

$ vapor new SwiftPackageCatalogVapor
$ cd SwiftPackageCatalogVapor

This creates the folder structure of a basic Vapor project. If you’re coming from node.js background, you can think of this as express-generator.

Generate Xcode Project

Type following command into terminal after navigating to your app’s directory.

$ vapor xcode

You should see something like this:

No Packages folder, fetch may take a while…
Fetching Dependencies [Done]
Generating Xcode Project [Done]
Select the `App` scheme to run.
Open Xcode project? y/n>y
Opening Xcode project…

You should have ended up with Xcode running and containing your project. Let’s try to launch the app locally.

  • Switch the target scheme in the upper right corner to App in case it isn’t.
  • Launch the app by pressing the run button or cmd+r.
  • You should end up with the app running and connected to the debugger.
  • Open a browser and navigate to http://0.0.0.0:8080.
  • Profit

You should be looking at the hello world Vapor template.

Deploy to Heroku

This is cool and everything but I’d bet that you want your web app accessible on the Internet. The easiest way I could find to do so Is to deploy the app to Heroku.

You’ll need to sign up for Heroku first, and install Heroku toolbelt, which can be found here https://devcenter.heroku.com/articles/heroku-command-line. Don’t worry, it’s free and no credit card is needed.

After installing Heroku toolbelt, you’ll need to connect to your account in the terminal.

$ heroku login
Enter your Heroku credentials.
Email: adam@example.com
Password (typing will be hidden):
Authentication successful.

The most basic way to deploy something to Heroku is to add Heroku as a git remote and push to it like you’d push to your origin repository. Let’s commit the source code first.

$ git init 
$ git add .
$ git commit -m “initial commit”

Now let’s make use of Vapor toolbelt again to set everything up for us.

$ vapor heroku init

The toolbox will guide you through few basic steps.

Would you like to provide a custom Heroku app name?
y/n>y
Custom app name:
>packagecatalog
https://packagecatalog.herokuapp.com/ | https://git.heroku.com/packagecatalog.git

Would you like to provide a custom Heroku buildpack?
y/n>n
Setting buildpack...
Are you using a custom Executable name?
y/n>n
Setting procfile...
Committing procfile...
Would you like to push to Heroku now?
y/n>y
This may take a while...
Building on Heroku ... ~5-10 minutes [ • ]

Deploying to Heroku currently takes few minutes because it always builds the whole codebase, including all the underlying c libraries from scratch. When it’s done, you should see something like this.

Building on Heroku ... ~5-10 minutes [Done]
Spinning up dynos [Done]
Visit https://dashboard.heroku.com/apps/
App is live on Heroku, visit
https://packagecatalog.herokuapp.com/ | https://git.heroku.com/packagecatalog.git

An indeed, the app is up and running on Heroku.

Vapor app deployed to Heroku
Vapor app deployed to Heroku

Make Use of 3rd Party Swift Packages

You can search github, or use a package catalog like http://swiftpack.co or https://swiftpkgs.ng.bluemix.net to search for useful packages. Dependencies are added in Package.swift file. You will need to fetch the dependencies and re-generate the Xcode project after updating your dependencies.

$ swift package update
$ vapor xcode

--

--