Building an API with Sailsjs

Today's tutorial, brought to you by yours truly is all about setting up a simple project using Sails.js, taglined as the webframework of your dreams.

We are going to be building a quick Coffee Shop app where you can create orders and drinks. So let's get started.

**Note, this is for a Mac and mostly run on the command line

About this tutorial:


  • Postman
  • Your favorite text editor (mine is Sublime Text 3)

Level: Know what a GET/POST request is? Then you're good. Beginner-Intermediate

Time: 30-45 Minutes

Installing Homebrew and NodeJS

  1. Open up your terminal and install Homebrew:
    • ruby -e "$(curl -fsSL"
    • Homebrew is a package manager that helps easily install packages you may need
  2. Install NodeJS:
    • brew install node
    • Check your installation with node -v

Installing SailsJS

  1. Now that we have Homebrew and NodeJS installed, installing Sails will be much easier.
  2. Packaged with the NodeJS install, comes another NodeJS packagemanager called npm
  3. Run: npm -g install sails
    • -g is a global flag to install it on your system so you can build applications

Setting Up Your Project

  1. Now if you're like me, you like having your own folder for code-related projects, so we are going to walk through that too
  2. Again in your terminal, mkdir ~/Documents/Development
    • I put all my code in a folder called Development in my main Documents directory, feel free to put it wherever you like
  3. cd ~/Documents/Development
  4. Now we are going to create a Sails project - sails new coffeeShop
  5. cd coffeeShop
  6. Now let's test out our installation with sails lift
  7. If everything went according to plan, you should see something that looks like a Sailboat!
  8. Now you can open your browser to http://localhost:1337/
  9. Hopefully you will see something that says "A brand new app" at the top. Congratulations, you've setup your first sails app!

Creating Your CoffeeShop API

  1. On to the fun stuff. Hit ctrl-c to stop the current running sails app
  2. sails generate api order to generate our orders
  3. Open the project folder in favorite text editor and you will now see the basic outline of a Sails app. I will not go into it this time, but check out that goes into great detail about your app
  4. Go to the api >> models folder and open up Order.js
  5. Here we are going to place all the attributes of an order! So let's brainstorm for a hot second, what is involved in an order:
    • Drink
    • Customer
    • Price
    • (this is a basic version)
  6. If we then think more about those, a Drink is going to be a reference to another Model that we will create, a Customer has a name, so most likely a string, and the Price is a number (not an integer though, a float). So let's add those attributes. Also note, every Order needs to have a unique id.
  7. For now, we are going to leave off the Drink until next tutorial so we can create a relation between the two!

       attributes: {
          id: {
             type: "integer",
              primaryKey: true,
              autoIncrement: true
           customer: {
              type: "string",
              required: true
           price: {
              type: "float",
              required: true
  8. Now that we have our basic attributes set up for a model, let's try it out.
  9. sails lift
  10. This time a message will come up about altering data. Since we have a new model, Sails wants to know what to do with the current models data (if there was some). If you are running in production, always chose safe. For now, since we are just testing stuff out, we can choose alter, so hit 2
  11. If we go back to our browser, we will notice that nothing has changed. Well that's cause we are building an API right now and haven't done anything with the frontend!
  12. Open up Postman at this point as we will test out our new API call.
  13. Run a quick GET request in Postman for localhost:1337/order
    • You will probably get back nothing, just two empty brackets: []
    • That's because we haven't created anything!
  14. Change the GET request in Postman to a POST and where it says "Body", click on that, click again on the "raw" option and add these things to the Body:

    "customer": "Nikola Tesla"
    "price": "1.04"
  15. Click on "Send" at the top and hopefully you get back something that looks like this!

      "customer": "Nikola Tesla",
      "price": "1.04",
      "createdAt": "2016-01-22T15:52:27.849Z",
      "updatedAt": "2016-01-22T15:52:27.849Z",
      "id": 1
  16. Congratulations! You've successfully made your first API POST! If we switch the POST back to a GET and hit send, you will now see your order right there!
  17. The nice thing about sails is that it comes with some of these base HTTP Requests right out of the box, so check out the documentation here:

Well that's all for today - Part 2 of the series will come soon where we will add our Drink model, create relations, and maybe add a frontend!

(Image from

Hey, guess what? This was my first tutorial! Have any comments/questions/requests? Let me know below! I may have glossed over some more important topics that I can talk about next time!

Kevin Guebert

Read more posts by this author.

Subscribe to Kevin Guebert

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!