Creating a basic API in Ruby on Rails
Today the information shared between applications is vital for the use of modern software. Some years ago we were hearing the term API (“Application Programming Interface”) and the thousands of benefits that they have when using them.
This post will show a basic example of how to create an API with Ruby on Rails and Postgresql. I hope it will help you.
Creating the Project.
Let’s start creating our project, currently, Rails allows creating projects with the function only for API’s, in this case, we will use that functionality.
At this point, I consider that you already have your directory where the project will be built.
We open the terminal and position ourselves in the directory where your project will be created, in my case it will be in “c:\rails_api_basic”.
1. We execute the following command to create our project.
rails new api_pets -api -database = postgresql
This command will create the structure of our project.
2. Now we navigate to the api_pets folder and voila, now we run a bundle to install our project dependencies.
bundle or bundle install
With our dependencies installed, now we can prepare our databases, for this, we execute the following command.
rake db: setup
Perfect! Now let’s test our server.
rails s
This will run the server on port 3000, leaving as follows. http: // localhost: 3000
Now that we have everything ready, let’s start programming, let’s get to work.
Creating folder structure
For this API, we will handle the following folder structure:
controllers → API → v1, having an output with the following structure.
http: // localhost: 3000 / api / v1 / pets.
Creating our model
Let’s create our model called Pet, executing the following command.
rails g model pet name: string age: string category: string
After this, we execute its respective rake db: migrate so that it adds our model to the database.
Perfect!, now we create a file inside controllers → API → v1 that we will call pets_controller.rb
Wait … we’ll come back to this file later.
Configuring our routes
In our config/routes.rb file, we must add the namespaces corresponding to our API, at the end, our file must have the following structure.
Wow, our little API is already taking shape, it’s time to add our logic for the insertion of pets.
We return to our pets_controller.rb file
Adding Pets
Now in our pets_controller.rb file, we will add the logic to add our pets leaving the file as follows.
Let’s try our method, for we will use Postman, in which we will make a POST request to add our pet.
We must configure our header, so that it accepts our JSON, being as follows.
Send the request… if everything has gone wonderfully so far, we will have the following result.
Great!, we already have our api accepting our JSON data, now how do I get the data of the pet that I just created?. Let’s move on to that.
Getting Pets
To obtain our pets, we can use the same endpoint in this case http: // localhost: 3000/api/v1/pets, or we could redo the task of creating files, configuring another path, etc. … in this case, I will use the same endpoint.
Now the pet_controller.rb file will look like this!
Let’s test our endpoint again, only now instead of doing a POST, we’ll do a GET.
Great! This is the result of the request we have made.
Our little API works! After this, you are in charge of putting into practice the following methods of our API.
I hope and this small example has served you and put the knowledge into practice and comment if you have any questions about it!
Thanks for reading!