Building API in Rails - Part 1

Getting it ready

API for ruby on rails

Hi there, welcome to the first part of the series of building API in rails. Let’s just start with what this series will cover

  1. Setting up our Ruby on Rails API only application with all necessary gems and stuff.
  2. Adding Rspec for the TDD approach.
  3. Building a CRUD RESTful APIs
  4. Serializing the API resources
  5. Securing our API with JWT
  6. Integration SWAGGER for api documentation
  7. and many more….

Setting up our Ruby on Rails API only application with all necessary gems and stuff

It’s quite easy to set up a ROR application and since ROR is a battery included framework we don’t have to worry about any configuration stuff.

Make sure that you have

  1. Ruby installed
  2. For this, we can install first RVM (Ruby Version Manager) then install the required ruby version. We will be using the latest ruby version which is 3.0.2 as of now.
  3. Node js, npm, and Yarn installed Although we will not need this since it’s API only application but it might come in handy later so it’s better to install it. Refer to this for installing Yarn.

    For node, we can install it with nvm . I love using n as node version manager.

    In Rails 7 Import Map is going to be the new and default way of importing javascript assets into the rails application so we might not even need node ​:man_shrugging

  4. Rails installed. Open the terminal and enter gem install rails
  5. Check your rails version with the rails version command. For this series, we will be using the Rails version 6.1.4

Now since we have everything needed let us go ahead and set up our application. Go to terminal and enter

rails new api_app --api -d postgresql --skip-system-test --skip-test

So, you might have noticed the database we will be using which is Postgresql, and also I have skipped the test since we will be using Rspec for testing.

Before moving forward to creating an API lets first do the following

  1. Setup Database
  2. Setup Rubocop Gem
  3. Setup Rspec

Great 🎉 🎉 we are done with the setup. Let’s run rails s in the terminal and visit here .

Please feel free to give your feedback on the comment section below or ping me at or . Have a great time :smiley_cat:

Building API in Rails - Part 1