Using External APIs on Rails


I am pretty new to programming and this would be my second project on Rails. I have never used an external API on rails before. Could anyone give me some pointers on what to do?

I want to create a simple recipe app where users enter a word on a form and a list of recipes/pictures would show up.

I have searched about using APIs on rails and seen some tutorials on HTTParty, but I still didn't understand how everything works and connects. Creating the search form and making the button work with the API call confuses me the most. Most of the examples out there just show me how to use HTTParty by itself, but could someone explain to me how this would work altogether? I am using the food2fork API. Any help would be appreciated.

2 thoughts on “Using External APIs on Rails”

  1. The API is used to connect two systems, yours and food2fork’s.
    Here’s how it works:
    1. Users enter something into your form
    2. Your systems get the data from you form (still everything basic, no api needed)
    3. Your system uses HTTParty (or something else) to create a request to the API
    3.1 This request submits the data to the food2fork via their API
    3.2 f2f gets the data and does something (whatever they do, no idea…)
    3.3 f2f generates a response (data from their database you searched for or whatever)
    4. The f2f API presents their response-data to your request
    5. HTTParty tells you/your app that you received this and that as a response
    6. You do something with the response (display it on the page where the user searched for it, for example)

    Does that make sense to you?

  2. Hey there!

    The best way I’ve found to think about APIs is as a collection “black box” classes that your app uses. Imagine that you have a `Recipe` class that contains all the info in food2fork. You can ask that class for recipes and pictures, and use its data like you would any other class in your app.

    So, “calling the method” in this class is making an API call. You’d get the data and use it in your app.

    I’ll threw together a quick sample here to make things a little more understandable. It’s pseudocode, but hopefully it will help! I’ve done a lot of work with APIs, both writing them and using them. I’m the main developer for [Freckle Time Tracking’s API](, and I **love** helping people use APIs, so please let me know how I can help! 😀

    require ‘json’
    # I find it’s useful to wrap API calls in a class. You can reuse logic for
    # multiple API calls and it abstracts away the details of calling the API,
    # making the rest of your code much cleaner
    class Food2Fork
    include HTTPParty
    # Constants for things like API keys and base URLs make your code a little
    # faster and make it clear they don’t change
    API_KEY = “YourAPIKey”

    def search(search_term, options = {})
    # I’m using reverse_merge! here to setup default values for searching.
    # By default, I want the first page of trending results
    :page => 1,
    :sort => “t”

    # Now I’m going to merge my options hash to set the search term and
    # API key. writing API code tends to involve a lot of merges and reverse
    # merges, so I’m putting both here so you get comfortable with them
    options.merge!({:key => API_KEY, :q => search_term })

    # Now we’re actually going to make the request. This is where HTTP Party is
    # used. Calling this method will return the response from the API.
    # We want to parse the response’s body (the main content of the request) as
    # JSON and return that to be used in our code:

    # {
    # “count”: 1,
    # “recipes”: [
    # {
    # “publisher”: “”,
    # “social_rank”: 99.81007979198002,
    # “f2f_url”: “”,
    # “publisher_url”: “”,
    # “title”: “Slow-Cooker Chicken Tortilla Soup”,
    # “source_url”: “”,
    # “page”: 1
    # }
    # ]
    # }

    # In this case, let’s only return the array of recipes we found:
    response = self.class.get(BASE_SEARCH_URL, options)
    return JSON.parse(response.body)[“recipes”]

    # Meanwhile, somewhere else in our app…
    # Create an instance of our API wrapper so we can start searching
    cabinet_rummager =

    # Get our recipes.
    recipes =“pot pie”)

    # It should look something like this:
    # [
    # {
    # “publisher” => “”,
    # “social_rank” => 99.81007979198002,
    # “f2f_url” => “”,
    # “publisher_url” => “”,
    # “title” => “Slow-Cooker Chicken Tortilla Soup”,
    # “source_url” => “”,
    # “page” => 1
    # }
    # ]


Leave a Comment