Vapor for SwiftIf you love using Swift to build your iOS apps, but another language for your backend, you’ll be super excited to hear about Vapor. Vapor is a web framework written in Swift that you can use to build a website or API. While still in beta, it’s already garnered a huge amount of interest: over 4,000 people have starred it on GitHub, and it’s growing quickly.

Last week, I wrote a post with an overview of various server-side Swift frameworks. Of them, Vapor has the best documentation by far, and even a command line tool to help you bootstrap your new project! In this tutorial, I’ll show you how to get started with Vapor, and build your first web app. So, let’s get started!

Install Swift Version Manager

Like most of the other server-side Swift frameworks, Vapor is built on Swift 3. This is because Swift 3 is the first version of Swift to include Swift Package Manager. Like Cocoapods or Carthage, Swift Package Manager allows you to easily manage your code’s dependencies, and automate the build and installation of external code.

Unfortunately, Swift 3 is still in development, so there are multiple versions of Swift 3 out there. To manage this complexity, we’ll install Swift Version Manager, or Swiftenv. Enter your shell and run these commands to install:

Install Swift 5-31

Next, we’ll use Swiftenv to install the 5-31 development snapshot of Swift. Run this command in the shell:

Initialize Your Swift Project

Once you have the correct version of Swift installed, we’ll create our project! To do so, we’ll make a HelloWorld directory, set the local version of Swift to the 5-31 snapshot, and tell Swift Package manager to initialize a project:

By running this, Swift Package Manager creates two files for you:

  • Sources/main.swift – this is the file where your code starts and runs.
  • Package.swift – this file stores information about your code, and its dependencies.

Add Vapor as a Dependency

To install Vapor, we’ll add it as a dependency to our Package.swift. Replace your Package.swift file with the following:

Generate an Xcode Project File

Next, we want Swift Package Manager to download Vapor. SPM can also generate an Xcode file for you to use. If you prefer, you can skip this step and use another text editor in the next step.

Write the Vapor Code!

With Xcode (or your favorite text editor), open up main.swift. In Xcode, you’ll need to make sure that you’re running the correct version of Swift. You can do that through the Xcode menu: Xcode > Toolchains > Swift Development Snapshot 2016-05-31 (a)

Xcode Toolchains

Now that you’ve fixed Xcode, replace the existing code in main.swift with:

In this code, we import the Vapor library, and initialize its Application object. We then register a handler for /, which returns “Hello, World!”. Finally, we start the app.

Run the app

To run the app, run the HelloWorld target in Xcode, or via the command line using the following:

And viola! Your first server-side Swift application is now running at http://localhost:8080/. Check it out!

Screen Shot 2016-06-06 at 11.29.17 PM

Want to keep building with Vapor? Check out Vapor’s website for more information.

More Reading

There’s more than just Vapor on the server! Read our overview of various server-side Swift frameworks. This blog post is based on a talk I gave at the Swift Language Meetup in San Francisco. Watch the recording!

If you’re building a backend API for your app, consider using Stormpath to help you implement a secure REST API. Stormpath is an Identity API for developers that provides complete user management, including authentication and authorization, out of the box. Read our tutorial on how to build a REST API for your mobile apps using Node.js to learn more.

  • Pingback: Swift on the Server - Where Are We Today? - Stormpath User Identity API()

  • Christopher Kong

    Getting error of “no such module Vapor” in Xcode even though I cloned it – I assume this tutorial is only for Swift 3?

    • “Like most of the other server-side Swift frameworks, Vapor is built on Swift 3”

      • You’re actually able to switch Swift versions even in Xcode 7. I’ll update the post with details.

    • Oh shoot! I should mention that you can switch to the correct version of Swift 3 using Xcode. Click on Xcode > Toolchains in the mac menu to switch it. Thanks for reporting this bug with my post!

  • Pingback: พัฒนาระบบ Swift Web Application ด้วย Vapor กัน()

  • Rusty Magnet

    Hi Edward, awesome article. I would just add – for those readers who struggled to get the the HelloWorld app to compile – stick to the Toolchain version that Edward used. I used a newer version & this caused nasty compile errors. The version that worked was the following:

    swiftenv install DEVELOPMENT-SNAPSHOT-2016-05-31-a

    The version that died was:

    swiftenv install DEVELOPMENT-SNAPSHOT-2016-06-06-a

    • Thanks Rusty! Yeah, different versions of Vapor will work under different versions of Swift, and the version of Vapor in this tutorial works with the 5-31. I’ll probably update this tutorial sometime in the future, but it’s too much work to try to update every two weeks on a new Swift release.

  • Pingback: Watch: Mobile Authentication for iOS Applications - Stormpath User Identity API()

  • León Yannik López Rojas

    Hello!, Any idea of why with Application( ) i get the Use of unresolved identifier ‘Application’ error?

  • Thomas Minshull

    Hmm got stuck on the “swift package generate-xcodeproj” command, Keep getting Failed to clone to —my path— vapor seems to have moved and the path isn’t Failed to clone

  • Pingback: ankara escort()