Using Yelp's API can be a bit complicated, because you are given a Consumer Key, Consumer Secret, Token, and Token Secret. Yelp uses Oauth 1.0a, which requires for signing of requests, before you can make calls to their API.

Yelp gives you a page that looks like this:

But how do you translate that into an API call? Your first intuition might be to just put those things in a query string like so:

http://api.yelp.com/v2/search?location=San+Francisco&oauth_consumer_key=123&oauth_consumer_secret=456&oauth_token=789&oauth_token_secret=012  

But this isn't quite right as Yelp's API, and standard Oauth 1.0a states, you need the following to make your API request.

OAuth Parameter & Values

  • oauth_consumer_key
    • Your OAuth consumer key (from Yelp's Manage API Access).
  • oauth_token
    • The access token obtained (from Yelp's Manage API Access).
  • oauth_signature_method
    • HMAC-SHA1
  • oauth_signature
    • The generated request signature, signed with the oauthtokensecret obtained (from Manage API Access).
  • oauth_timestamp
    • Timestamp for the request in seconds since the Unix epoch.
  • oauth_nonce
    • A unique string randomly generated per request.

Notice how oauth_consumer_secret and oauth_token_secret are not a part of the list. This is because you need them for generating the oauth_signature. So to generate the oauth_signature you need the entire list above, minus oauth_signature, and any other search paramters like location.

So here is an example of how to make a function that calls the Yelp API.

First we need to set up the environment, you are going to want to install these npm middlewares.

npm install --save oauth-signature  
npm install --save nonce  
npm install --save request  

The following is a basic, bare-bones search.

/* require the modules needed */
var oauthSignature = require('oauth-signature');  
var n = require('nonce')();  
var request = require('request');  
var qs = require('querystring');  
var _ = require('lodash');

/* Function for yelp call
 * ------------------------
 * set_parameters: object with params to search
 * callback: callback(error, response, body)
 */
var request_yelp = function(set_parameters, callback) {

  /* The type of request */
  var httpMethod = 'GET';

  /* The url we are using for the request */
  var url = 'http://api.yelp.com/v2/search';

  /* We can setup default parameters here */
  var default_parameters = {
    location: 'San+Francisco',
    sort: '2'
  };

  /* We set the require parameters here */
  var required_parameters = {
    oauth_consumer_key : process.env.oauth_consumer_key,
    oauth_token : process.env.oauth_token,
    oauth_nonce : n(),
    oauth_timestamp : n().toString().substr(0,10),
    oauth_signature_method : 'HMAC-SHA1',
    oauth_version : '1.0'
  };

  /* We combine all the parameters in order of importance */ 
  var parameters = _.assign(default_parameters, set_parameters, required_parameters);

  /* We set our secrets here */
  var consumerSecret = process.env.consumerSecret;
  var tokenSecret = process.env.tokenSecret;

  /* Then we call Yelp's Oauth 1.0a server, and it returns a signature */
  /* Note: This signature is only good for 300 seconds after the oauth_timestamp */
  var signature = oauthSignature.generate(httpMethod, url, parameters, consumerSecret, tokenSecret, { encodeSignature: false});

  /* We add the signature to the list of paramters */
  parameters.oauth_signature = signature;

  /* Then we turn the paramters object, to a query string */
  var paramURL = qs.stringify(parameters);

  /* Add the query string to the url */
  var apiURL = url+'?'+paramURL;

  /* Then we use request to send make the API Request */
  request(apiURL, function(error, response, body){
    return callback(error, response, body);
  });

};

Now if you call the function request_yelp(params, callback) it will call the callback with these arguments, callback(error, response, body).

That's it, now you're ready to take full advantage Yelp's API.