Step 1
USE RESTCLIENT TO FETCH FROM API
The first thing we want to do is fetch from the API.
def from_api
resp = RestClient.get("https://exampleapi.com/?api_key=1241241515151")
render json: resp.body, status: :ok
end
Step 2
HIDE YOUR API KEY
If you need to hide your key you can do so by using a gem called Figaro.
gem "figaro"
Add figaro to to your gemfile, bundle install figaro and install figaro.
$ bundle exec figaro install
This creates a config/application.yml file and adds it to your .gitignore. You can use this to create Environment variables such as your key.
api_key: 1241241515151 (example)
And you can call it like this.
ENV['api_key']
> 1241241515151
Now use that key in your fetch request.
def from_api
resp = RestClient.get("https://exampleapi.com/
?api_key=#{ENV['api_key']}")
render json: resp.body, status: :ok
end
Top comments (0)