API stands for Application Interface Program, which provides one application to interact with ‘n’ number of applications which is of same/different language, to access the data/functionality.
Creating API application provides more scalability to the web applications. It will also helps for the easy integration with cross domain applications/languages.
• iOS apps
• Android apps
• Node js framework
• Angular js framework
There are 2 ways to achieve this in rails.
1. We can easily create a new API application using gem called rails-api, which inherit the application from ActionControllerAPI instead of ActionControllerBase and it will skip view generation. This will also helps to configure the middlewares.
2. In case the application is already created we have to inherit ActionControllerAPI manually.
Once the application is set-up we can create the controller under controller/v1 folder, that will helps for the easy maintenance of versions and releasing new version of API’s. In this controller we can write code for crud or some functionality that can be called by curl or as API request from front end application for the GET, POST, DELETE, PATCH requests gives responses in JSON/xml format, which is in human readable form. This json data can be read and shown from front-end application.
By passing the token which is generated for each user and email of the user through an api header to secure an api. It can be ensured that there only authenticated user can access and modify data using api. Using these we can authenticate the user and secure the application. According to the data sent and the data matches in the applications we can send the proper responses back to the front-end application.