We will cover the initial setup and how to make both POST and GET with controllers and models.
Now open your terminal and cd into the place you want your project.
Now run (also in the terminal, of course change the ProjectName to your actual project name)
You should now see an ASCII art of the Vapor logo. Like so:
Congratulations, you have succesfully created your first Vapor project. Let’s now try to run the project. First cd into your project (
cd ProjectName) and after that run:
Vapor will then fetch the dependencies and run the project. Vapor will now ask you if you want to open the project, and yes, yes we do! (So type y to open)
You should now have your Xcode project open. Let’s check if we have everything running correctly.
So in Xcode press the ‘Run’ button in the top left corner of the UI (or Cmd + R). You should now see this in your console in Xcode (Make sure that you switch target from the framework to App, just next to the stop button)
0.0.0.0:8080 into your browser. If you see the ‘It works.’ screen then you have successfully run your first local Vapor.
Note: Beacuse Vapor is constantly updating and new versions pushed out frequently then you might have this error in your Xcode project after running for the first time.
If you didn’t get that error then just go to section two of this article. If you did then open your terminal and first run
vapor build --clean, after that run a
vapor xcode that should do the trick. (This basically cleans the project and compiles it again)
And hopefully you should be ready to rock and roll.
Open the terminal again, and run:
After that run
vapor xcode this will make sure that you get the correct path for the model. If you open Xcode then you should be able to see your model in the Models folder.
Fill the model with the code below:
Vapor is using a class called
Droplet, the Droplet is the main entry point on the server (If you are familiar with iOS development then this would be our AppDelegate class or your entry point in Storyboard). The Droplet instance will contain all providers, routes and makes sure that everything has been linked correctly to run the different functionalities in the run time. The line
drop.run() in the
main.swift is where the server actually gets booted, so everything you need to add has to be before this line.
Now open the
main.swift file and add the following code right above the
Now Run your Xcode project. You should now see this in your console in Xcode (Make sure that you switch target from the framework to App, just next to the stop button)
Now open your browser and type in
should now have returned your first JSON feed!
Of course, Vapor is way smarter than this. So please go ahead an delete the code you added in step 3.
Now go back to your terminal and run
touch Sources/App/Controllers/CarController.swift and again
vapor xcode, the Controllers folder should now contain your newly created CarController.swift
Before we can make any good use of our new Controller then we have to update our
Car.swift model to actually get some of Vapors awesome magic.
So jump back into the
Car.swift file and update the file to have the following:
So now our model should be ready for some cool controller action. Please now open our
CarController.swift and add the following code:
This is how some of the simplest controllers will look in Vapor. Now let’s try to see how we will use them in our
Are you ready to see some magic? Ok, now open the
main.swift let’s start by doing some house cleaning, delete the following lines from the file:
and add the following lines of code:
Now try to open your browser and run
http://0.0.0.0:8080/api/cars you should now see:
The id will still be null because the object wasn’t instantiated from the db.
Ok cool, let’s make our API actually hold some data (remember this will only be held until you have run the project again. Open your
CarController.swift file and update the code to this:
Now Run your Xcode project again. If you start by opening your browser at our ‘get’ endpoint
http://0.0.0.0:8080/api/cars for the cars then you will of course be able to see that it’s returning an empty array.
So let’s now try to make a post request to our ‘post endpoint’ which is basically the same slug
http://0.0.0.0:8080/api/cars (you can use whatever API testing tool, I normally use Postman. If you don’t have any tools like that then just use this cURL:
This should return our error response (response code 412):
Let’s now add our name parameter to the cURL:
And voila! We have stored our first (only in session object (and should ofc see our stored car object as JSON response). Try running the curl (or postman action) a couple of times with different names, and then run the ‘get’ in the browser.
So as you could see it’s pretty easy to get up running with Vapor. In the next blog post, I will add a MySQL database and show how you CRUD objects. (If you have followed this tutorial then it’s only a small amount of lines of code that needs to be added). Sign up to the blog to make sure you get the latest tutorials and tech posts from our engineering team.