Requesting data from an API with Rails
Published: 2022-03-29 14:37:47

#I'll be using polygon.io API in the below examples.

# Gem to Install

Gemfile:
```
gem 'faraday'
gem 'json', '~> 2.5', '>= 2.5.1'
gem 'dotenv-rails', groups: [:development, :test]
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0252v336n8mhnknf9qg.png)
Execute:
```
bundle
```
#Generate Controller & View for making API requests
```
rails g resource stock
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zokd7ldn5pc47ua6e7jr.png)

#Now open /config/routes.rb and add this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z590vuyr2a3ei9hr0c6r.png)
We'll make API requests using this route and display responses on a browser on the same route.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4ofqmjlqqftz10p2rwt.png)
#Open /app/views/stocks/search and fill it with:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0j4w493tidq1ti6lzz6.png)
Which gives us a form to enter a ticker whose stock data we can pull from Polygon API. And in the if statement, we have a response variable set to response from API if we made a call in the controller.
```
if params['stock']
  @response = Searchstock::Search.by_stock(params['stock'])
end
```
#Create API module
Now let's actually get into the process of making an API request to Searchstock.
Although we have Faraday installed, it can't be directly called from within the controller.
Add it inside /controller/concerns/searchstock.rb. Make sure not to change the name of the folder.

I'll create searchstock.rb inside /concerns/searchstock.rb

The class inside the module and the method inside the class can be named anything.

From the code above, we can make out that - by calling Searchstock::Search.by_stock method with a ticker parameter would make an API call to Searchstock, but what about ENV['API_KEY']?
We sure can replace ENV['API_KEY'] with Polygon's actual API key, but leaving an API key within the code is not right. So, we are going to place our API key as an Environment variable.
First, let's place the API key as an Environment variable in our local machine.
To do this, we'll be using dotenv gem.

#Install Dotenv-rails
Add this line to the top of your application's Gemfile:
gem 'dotenv-rails', groups: [:development, :test]
And then execute:
bundle
Now add a file named .env to the root of your application. Make sure not to change the name of the file. Add your API key in the first line like so:
API_KEY=your_api_key
Replace your_api_key with your actual key provided by OpenWeather.
If you're going to commit your work, add the .env file to .gitignore so that git does not track it.

#Call API from the controller
Now for the final part, add this to the search action of your controller /app/controllers/stock_controller.rb
```
if params['stock']
  @response = Searchstock::Search.by_stock(params['stock'])
end
```
Make sure to match the names of the module, class, and function that you named earlier.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fw4kldgia3yd3qepnitp.png)
#Check the result

Now, open the server
rails server
And visit http://localhost:3000/stocks/search from your browser.
Search for a ticker to see the response.