Today, a web-portal that does not support APIs is not even considered a web portal! This post explains how we can implement Rails APIs in our application. Rails APIs require the following minimum implementation:
- API keys.
- API Versioning.
- API Request Authentication.
- Request throttling (optional).
Let’s see how this can be done.
API key generation
Lets generate the model.
or if the model already exists,
Now lets edit this model and add the logic to generate the API key when a record is created using the method
In this code we use
SecureRandom.base64.tr('+/=', 'Qat') to generate a token. Since, it may contain the special characters like
+/=, we shall replace them with some random alphabets using
tr, so that the API key looks good. Now lets add the
API controller and routes versioning
Now, lets generate the controller called
Api in the
api namespace. This…
View original post 575 more words