In this post, you will build a NodeJS API to find the best restaurants around you using Yelp API. In this tutorial, you will be using the yelp-fusion API to do a business search. In this post you will see code samples as well as setup instructions on how to setup your NodeJS API to do a search via Yelp.

Getting started with Yelp

To start working with Yelp, you would need to create an account on the Yelp site or use social login (e.g. using your Google account) and then register an app. Post registration you will get a Yelp API key which you can use to initialise the Yelp client from the Yelp fusion API. You can also invoke Yelp urls without the yelp-fusion library by calling the url directly and passing the API key bearer token in the authorization header. (more on this in the next tutorial)

The best pizza restaurants around you

This is a NodeJS API and Express based API. Written below is the function to get the best pizza restaurants around you. For the purpose of this tutorial, the city has been hard-coded into the index.js file.

/search/best/pizza

const yourCity = "sydney, nsw";
app.get('/search/best/pizza', (req, res) => {
  const searchRequest = {
    term: "pizza",
    location: yourCity,
    attributes: [
      'liked_by_vegetarians'
    ],
    sort_by: "rating"
  };

  client.search(searchRequest).then(response => {
    res.send(response.jsonBody);
  }).catch(e => {
    console.log(e);
  });
});

As you can see….

The best restaurants around you

In this tutorial you will write the function to return the data for the following url

/search/best/

const yourCity = "sydney, nsw";
app.get('/search/best/', (req, res) => {
  const searchRequest = {
    term: "restaurants",
    location: yourCity,
    attributes: [
      'liked_by_vegetarians'
    ],
    sort_by: "rating"
  };

  client.search(searchRequest).then(response => {
    res.send(response.jsonBody);
  }).catch(e => {
    console.log(e);
  });

});

The best restaurants in any location

Now let’s combine the concepts in the above functions and make it parametrised, so the client calling the url can pass some values for the data that it wants to search for.


app.get('/search/:food/:city/', (req, res) => {
  const food = req.params.food;
  const city = req.params.city;
  const searchRequest = {
    term: food,
    location: city,
    attributes: [
      'liked_by_vegetarians'
    ]
  };

  client.search(searchRequest).then(response => {
    res.send(response.jsonBody);
  }).catch(e => {
    console.log(e);
  });

});

Food and the city are values that are passed into the controller method to call this form, hence the location could be any region of the world and the food could be any value passed by the client.

Conclusion

Hope you found this tutorial useful and for the full source code of this Github repository, you can go the following Github url. The repository in that location contains a full source for a working nodejs API that uses the yelp-fusion API to find the best restaurants.

To know more about Yelp create your app in Yelp developer dashboard and have a look some of the Yelp documentation to know how to work with the API.

If you find any of my posts useful and want to support me, you can buy me a coffee 🙂

https://www.buymeacoffee.com/bhumansoni

While you are here, maybe try one of my apps for the iPhone.

Products – My Day To-Do (mydaytodo.com)

Have a read of some of my other posts on AWS

Deploy NodeJS, Typescript app on AWS Elastic beanstalk – (mydaytodo.com)

How to deploy spring boot app to AWS & serve via https – My Day To-Do (mydaytodo.com)

Some of my other posts on Javascript …

What is Javascript event loop? – My Day To-Do (mydaytodo.com)

How to build a game using Vanilla Javascript – My Day To-Do (mydaytodo.com)

Vanilla Javascript: Create Radio Buttons (How-To) – Bhuman Soni (mydaytodo.com)

Java Spring Boot & Vanilla Javascript solution – My Day To-Do (mydaytodo.com)

Vanilla Javascript: Create Radio Buttons (How-To) – Bhuman Soni (mydaytodo.com)


0 Comments

Leave a Reply

Avatar placeholder
Verified by MonsterInsights