For my most recent coding project, I created an image search-based application that uses react for the frontend and rails for the backend API.
In thinking about the functionality of my project, I knew that I wanted to create more than just a static image search feature on the frontend, and instead I wanted to give the user the ability to make image searches that would access a third-party API via my rails backend.
Two major benefits to the application were gained by choosing this route: 1.) Users could search for a wider array of images then if I had just populated my database with dummy data or one external API call upon initialization and 2.) By setting up a backend database, the application would not only have the possibility of making third-party API calls but could also allow me to persist data to the database.
In this blog post, I will walk you through how to quickly set up your rails backend API so that you can make a call to a third-party API. We will be building a rails backend that will make a call to the third-party API, Shibe.online, an API which generates random images of dogs, cats, and birds.
Let’s get started!
Step 1: Create your rails backend repo
First thing we’ll want to do is create our rails backend API. The quickest way to do so is by utilizing the rails API generator. You’ll need to CD via your terminal into the folder that will house your backend application, and then execute the following command:
rails new my_api --api
You can name your application whatever you like, in this example we named the application “my_api”. Once your application has been successfully created, CD into the application folder and enter
code . to pull it up.
Next enter the highlighted commands in the terminal of your code editor to set up the files we need to build our backend application.
- Create your Resource:
rails g resource Pet image
2. Execute Migration:
rails g db:migration
3. Verify your server is up and running:
Since we’ll be grabbing animal images from our external API, I set up our resource name as Pet with an attribute of image.
Step 2: RestClient Gem
The restClient gem is what will allow us to make our calls to the third-party API. In order to use this gem, we must first add it to our gemfile.
Inside your gemfile add the following:
Then, run bundle install inside your terminal to successfully install the new gem.
Now that your gem is install proceed to the controller where you’d like to make the fetch request to your third-party AP.
Step 3: Create Your Action & Route
For this example, let’s assume we want to grab an array of random dog pictures from the Shibe.online API. Our get request will be made to the following url: http://shibe.online/api/shibes?count=10
Firstly, let’s create the action in our Pets controller to fetch the API data from the Shibe.online url which stores the dog images. We’ll call it “get_dogs”.
Next, in order for our application to execute the controller action, we’ll need to specify the route.
Proceed to your routes file in your config folder and create a route for get “/dogs” which invokes our get_dogs action. Later, when we’ve finish successfully setting up our API call, we should be able to go to our local server “/dogs” and see all of our JSON API data.
Step 4: Utilize the RestClient Gem in your Action
Inside our “get_dogs” action in the Pet Controller is where we will make the call to our third-party API using the RestClient gem.
Firstly, make sure that you require the gem at the top of the controller.
It is helpful to save the url to a local variable as a best practice to help us organize our data.
Next pass the url local variable into the RestClient.get method. We will also want to save the return value of the method into another local variable(we’ll call the variable “response”). Finally, execute the render json by passing in the response variable.
Once this step is complete you should now have successfully created the json data for your API!
To check out your newly fetched data, start up your rails server and head over to “localhost:3000/dogs”. The following API data should be displayed:
As a quick tip, it’s helpful to have a JSON formatter extension added to your web browser, otherwise your JSON data might look a bit wonky and difficult to read. Here’s the one I use for chrome.
The restClient API is easy to use and opens up many possibilities in terms of expanding your application’s ability to make third-party API calls via a rails backend.
You should now have enough knowledge to get started on making calls to all sorts of cool third-party APIs through your rails backend!
To understand the full features of the RestClient gem, check out the GitHub documentation here: https://github.com/rest-client/rest-client.