DEV Community

Kim Nguyen
Kim Nguyen

Posted on • Updated on

Yelp Fusion API in a nutshell.

I finished my first project at Flatiron School two weeks ago, which ended our Phase 1. A phase is a three-week time frame of lectures that cover a big chunk of coding-related materials. In our first phase, we covered Ruby, SQL, ActiveRecord, and developing a CLI (command line interface) application.

For the project, me and my partner decided to build an app called SalonFindr assistance app that helps users to find hair salons based on their personal reference such as high-end, budget-friendly, currently open salons, view salon's reviews, sort salons by popularity, etc, based on a specific location. We decided to use Yelp Fusion API for our database.

Alt Text

What is an API?

"An application programming interface (API) is a computing interface which defines interactions between multiple software intermediaries. It defines the kinds of calls or requests that can be made, how to make them, the data formats that should be used, the conventions to follow, etc. It can also provide extension mechanisms so that users can extend existing functionality in various ways and to varying degrees." - Wikipedia

In essence, API is a set of functions and procedures that allow SE to develop an application using data and features on other apps.

Yelp Fusion API Usage:

Yelp API is a powerful tool to get the best local content from businesses and users reviews across 32 countries, which is awesome because our app can retrieve data outside of the U.S. It is extremely easy to use and also contains a large amount of data to work with, that is why we chose it for our first project. Here is a list of Yelp Fusion API usages:

  • Business Search: look for businesses by keyword, category, location, price level, etc.
  • Phone Search: look for businesses by phone number.
  • Transaction Search: look for businesses that support food delivery transactions.
  • Business Details: get overall business information (operation hours, yelp reviews, address, contact info, etc)
  • Reviews: get up to three reviews from a business.

During the process of figuring out how to use Yelp Fusion API, it was stressful for both me and my partner because we were lost, like very lost. But guess what? We suffered so y'all don't have to! I hope this blog is helpful for newbies like me, cheers <3.

Set up Yelp Fusion API for my first project:

I was initially struggling with finding a way to implement my API into my project. After some researches, I found this link to Yelp's Github sample code, I'll sample some code from this repo below.

1. Obtain you API Key:

In order to access Yelp Fusion API, you have to create your app to obtain a unique API key.

Alt Text

2. Store your API Key :

There are 2 ways to store your unique API Key in order to make your app running without actually exposing it to the public.
Keep in mind that API key should not be shared anywhere since it is the credential for your call to Yelp's API.
So let's dive right into it.

  • Store API key by using environment variable:

In your terminal:

$ export YELP_API_KEY=your_api_key_is_a_long_complex_string_sfharf
Enter fullscreen mode Exit fullscreen mode

And you can check if you API is being stored by enter:

$ env
Enter fullscreen mode Exit fullscreen mode

You should see the last line as the API key you just stored!

  • Store API key using .gitignore:

Create a ruby file in your config folder, naming is optional but I'll name mine as key.rb. In this file, we can store our API key as:

YELP_API_KEY=your_api_key_is_a_long_complex_string_sfharf
Enter fullscreen mode Exit fullscreen mode

Look for your .gitignore file in your directory. In this file, you should add the file that you want to keep as a secret file, meaning specifies intentionally untracked files that Git should ignore. Our .gitignore file should look something like this:

Alt Text

Now when you git push you project, your key.rb file should be hidden, that way, your app can still function without exposing your API key to public!

3. Accessing your API Key:

  • Through environment variables:
API_HOST = "https://api.yelp.com"
SEARCH_PATH = "/v3/businesses/search"
BUSINESS_PATH = "/v3/businesses/" 
DEFAULT_BUSINESS_ID = "yelp-san-francisco"
DEFAULT_TERM = "dinner"
DEFAULT_LOCATION = "San Francis6co, CA"
SEARCH_LIMIT = 20

API_KEY = ENV["YELP_API_KEY"]
Enter fullscreen mode Exit fullscreen mode

Environment variables are stored in hashes, so you can easily access your API key by using ENV["YELP_API_KEY"].

  • Through .gitignore file:
require_relative '../config/key.rb'

API_HOST = "https://api.yelp.com"
SEARCH_PATH = "/v3/businesses/search"
BUSINESS_PATH = "/v3/businesses/" 
DEFAULT_BUSINESS_ID = "yelp-san-francisco"
DEFAULT_TERM = "dinner"
DEFAULT_LOCATION = "San Francis6co, CA"
SEARCH_LIMIT = 20

API_KEY = YELP_API_KEY
Enter fullscreen mode Exit fullscreen mode

Make sure to set require_relative to the route to the file containing your API key.

4. Send Requests to Fusion Search endpoint:

# Original Source: https://github.com/Yelp/yelp-fusion/tree/master/fusion/ruby
#Returns a parsed json object of the request
class YelpApiAdapter

    def self.search(term, location)
        url = "#{API_HOST}#{SEARCH_PATH}"
        params = {
        term: term,
        location: location,
        limit: SEARCH_LIMIT
        }

        response = HTTP.auth("Bearer #{API_KEY}").get(url, params: params)
        response.parse
    end

    def self.business(business_id)
        url = "#{API_HOST}#{BUSINESS_PATH}#{business_id}"
        response = HTTP.auth("Bearer #{API_KEY}").get(url)
        response.parse
    end

    def business_reviews(business_id)
        url = "#{API_HOST}#{BUSINESS_PATH}#{business_id}/reviews"
        response = HTTP.auth("Bearer #{API_KEY}").get(url)
        response.parse["reviews"]
    end

end
Enter fullscreen mode Exit fullscreen mode

The sample code uses HTTP to authenticate and parse the code. So you will need to add HTTP gem in your Gemfile and bundle install or run in your terminal:

$ gem install http
Enter fullscreen mode Exit fullscreen mode

Our data should look something like this:

[1] pry(YelpApiAdapter)> YelpApiAdapter.search("pizza","san jose")
=> {"businesses"=>
  [{"id"=>"QNzqZtNb_I1mcHAviE5zeQ",
    "alias"=>"bibos-ny-pizza-san-jose",
    "name"=>"Bibo's Ny Pizza",
    "image_url"=>"https://s3-media4.fl.yelpcdn.com/bphoto/J7G2VYAA55MOuqPAd9LLTw/o.jpg",
    "is_closed"=>false,
    "url"=>"https://www.yelp.com/biz/bibos-ny-pizza-san-jose?adjust_creative=NUPSBDMz-cRd0wAMM-m6mw&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=NUPSBDMz-cRd0wAMM-m6mw",
    "review_count"=>1069,
    "categories"=>[{"alias"=>"pizza", "title"=>"Pizza"}],
    "rating"=>4.5,
    "coordinates"=>{"latitude"=>37.306477955952, "longitude"=>-121.89160958914},
    "transactions"=>["pickup"],
    "price"=>"$",
    "location"=>
     {"address1"=>"1431 Bird Ave",
:...skipping...
Enter fullscreen mode Exit fullscreen mode

...And that is it. You've successfully set up your Yelp Fusion API! Thank you for reading my blog. See ya later <3.

Top comments (1)

Collapse
 
raunaqss profile image
Raunaq Singh

Great article! The Yelp Fusion API is good, but the 3 review limit for scraping reviews is a bummer for many.

If you want to get all the reviews for a business and are looking for a neat way to overcome this limit you might want to check out my Yelp Reviews API which does exactly this.