Yelp Fusion API tutorial

Yelp Fusion API is a REST API that gives access to information across 50 million businesses across countries.

In this tutorial, you will be learning how to use the Yelp API to search businesses by providing their names, locations or phone numbers. You will also learn to match businesses in a given location to check their availability. You will be using the requests library of Python, Requests and JSON library to parse the JSON response of data.

This tutorial covers the following features of the Yelp API: Business Search, Reviews Search, Phone Search, and Business Match.

Finally, there will be a practical project to help you use Yelp API in a real-life case.

Generate Yelp API Key

The Yelp Fusion API uses private key authentication to authenticate all endpoints. You have create an app on Yelp to generate your private API Key.

1. Sign up at: https://www.yelp.com/signup

2. Go to the “Manage App” tab and then click “Create App”. Fill in the form and click “Create New App”.

3. You can see the API Key you need has been automatically generated.

 

Import Libraries 

Import Requests and JSON libraries to accomplish the task. Use Python’s Requests library to make GET requests to the API, and the JSON library to parse the data received as a response from the API.

 

Replace the value of the variable api_key with your own API key. Then, create a headers dictionary to pass the API key into the request that you will make to the API.

 

Business Search

You can search for businesses using a search term (e.g. “seafood”) and you must specify the location of the businesses you are searching for. You need to pass the parameters in a dictionary.

 

When you make a GET request to the API, the status code of the request is searched. If it is 200, only then the JSON response will contain results from the API.

 

You will get a result similar to this.

In the practical project, you will see how to extract data from this JSON response.

 

Business Reviews

Among the endpoints offered by the Yelp API is “reviews”. You need to know the ID of the business to be able to extract its reviews. To clarify this, let’s use this id “FEVQpbOPOwAPNIgO7D3xxw” for example.

 

When we print the result, Yelp gives you 3 reviews.

 

Phone Search

You can also extract business details using the phone number of the business.

You should pass the phone number in your params dictionary as you can see here:

 

Business Match

Similarly, you can match businesses by giving the name of a business and its address as you can see in this example:

 

Project: Extract Bookstores in New York City, Details & Reviews

 

First of all, import the Python libraries, Requests and JSON.

 

Then, use your API Key.

 

Search Using Term and Location

Now, use the Business Search URL and specify the search “term” and the “location”. Here we are searching for bookstores in New York City.

 

Send a request.

 

Convert the data into a JSON object.

 

Now, you can print parsed to get an idea how it looks like.

 

Parse Businesses in JSON Response

As you can see in the output, “businesses” is a list, enclosed by a square brackets.

{
“businesses”: [
{
“id”: “ehUuSk5gPTCQmwS_ubgKRA”,

….

]

 

So you have to extract businesses first into a Python list and then loop over the businesses in the list.

Note that the “display_address” is inside another dictionary; so you have to access “location” first and then “display_address”. Note also that the “display_address” is a list; so you have to use the join() method to merge it into one string.

“location”: {

“address1”: “139 Chrystie St”,
“address2”: null,
“address3”: “”,
“city”: “New York”,
“zip_code”: “10002”,
“country”: “US”,
“state”: “NY”,
“display_address”: [

“139 Chrystie St”,
“New York, NY 10002

]

}

 

When you run the code, you will get an output similar to this:

Name: The Mysterious Bookshop
Rating: 4.5
Address: 58 Warren St New York, NY 10007
Phone: +12125871011

Name: Human Relations
Rating: 5.0
Address: 1067 Flushing Ave Brooklyn, NY 11237
Phone:

Name: Astoria Bookshop
Rating: 4.5
Address: 31-29 31st St Astoria, NY 11102
Phone: +17182782665

 

Extract Business Reviews

Now, lets also extract the ID of each business to search for its reviews using the Business Reviews endpoint. Note you are still inside the for loop.

 

Then, apply the same steps to connect to the API. For the reviews endpoint, you will have to concatenate the URL with the id you have just extracted.

 

You can print parsed to get an idea how it looks like.

 

Also, the reviews are inside a list; so you will have to extract the list first.

 

Now, loop over the reviews to extract the name of the user, the rating, and the review text. Note that the name is inside another dictionary.

‘user’: {‘id’: ‘8YkvYzOIT-8pKIQNS1j_rg’,
‘profile_url’: ‘https://www.yelp.com/user_details?userid=8YkvYzOIT-8pKIQNS1j_rg’,
‘image_url’: ‘https://s3-media4.fl.yelpcdn.com/photo/_qRYsM-o-5u3_TL2Q0lZnQ/o.jpg’,
‘name’: ‘Alvin W.’}

 

So you have to access “user” first and then “name”.

 

 

Project Complete Code

 

 

Rating: 5.0/5. From 13 votes.
Please wait...

8 Replies to “Yelp Fusion API tutorial”

  1. HI, Nice tutorial. The question I have is. How do you make a request for more than 1000 businesses from Yelp API

    Rating: 5.0/5. From 1 vote.
    Please wait...
    1. Hi Kelvin! Do you mean via the Business Search endpoint? If so, you can put them into a list and loop on it.

      No votes yet.
      Please wait...
  2. Hi,

    can you please let me know if i want to validate yelp id so how can i do this ?
    like i want to send in request yelp id and i want to get response if exists all information if not exists then it will return invalid yelp id or something else.

    No votes yet.
    Please wait...
    1. Hi Subhash! What about putting your code portion inside “try, except”. Under “except”, you can send the error message you prefer.

      No votes yet.
      Please wait...
  3. Thanks, this tutorial made things easy.

    Question: when I run the code you provided for, say Business Search, at the very bottom of the json.loads(req.text) output it says ‘total: 11800’. Do you know what this means? I figured it would be the number of businesses matching the parameters, but when I inspect the text manually, there are maybe only 20-50 businesses.

    Thanks again!

    No votes yet.
    Please wait...
    1. Nathan, the API does not allow more than 50 per request. You need to use the offset parameter to get the next page of results. For using offset, give it any number. If you have limit=50, that means you are getting results 1-50, so give it offset=51 and you will get 51-100. Check here for more details.

      Again, the API can only return up to 1,000 results at this time.

      Rating: 5.0/5. From 2 votes.
      Please wait...
  4. can i used this code to extract the information of users????

    Rating: 1.0/5. From 1 vote.
    Please wait...
    1. @aftab – You can find all the available endpoints here.

      No votes yet.
      Please wait...

Leave a Reply