Here is the Fastest Way to Invoke (Uber) API From the Server Using JavaScript

Appery.io Server Code makes it super easy to create a custom API on the server. With Server Code you can add any app logic using JavaScript including invoke another API. Invoking another API literally takes a few minutes. Let me show you how do to it using Uber API.

Screen Shot 2015-10-09 at 4.33.43 PM
Invoking Uber API

When working with Uber API, one of the first request will be to find out what products are available in your area using latitude/longitude information.

This simple script will return all Uber products available in your location:

// script input
var lat = request.get("lat");
var lng = request.get("lng");
var url = "https://api.uber.com/v1/products";
var token = "4dPYezRBsWmqpV_XO..............";

// send Ajax request
var res = XHR2.send("GET", url + "?latitude="+lat+"&longitude="+lng, {
 "headers": {
 "Authorization": "Token " + token
 }
});
// response
response.success(res, "application/json");

The script:

  • lat and lng are inputs to the script
  • token can be found in your Uber developer account
  • XHR2 sends an Ajax request
  • The last line will return the response from Uber API

To test the script is fast, just enter sample latitude/longitude information:

Screen Shot 2015-10-09 at 4.35.12 PM
Testing Uber API

What’s super nice is that when you created the script, you also created an API to invoke it:

Screen Shot 2015-10-09 at 4.36.54 PM
REST API to invoke the script

In this post I have showed you how to expose any custom logic via a REST API. This example showed you how to invoke Uber API. You can invoke any other API as fast.

If you liked this and want to build apps fast, sign up for Appery.io plan.

Published by

10 responses to “Here is the Fastest Way to Invoke (Uber) API From the Server Using JavaScript”

  1. Thank you so much, very helpful
    Can you kindly clarifiy just the first line of code?
    var lat = request.get(“latar lng = request.get(“lngar url = “https://api.uber.com/v1/products”;
    I don’t understand the purpose
    Thank you

    1. Hi – that line reads the service request parameters (included after the ?).

  2. thank you, but I’m still confused!
    The variable ‘res’ shouldn’t be ‘result’? (lines 8 and 15 on the top image)

    1. res – is the response (JSON) from Uber REST API.

      1. but why do you use afterwards in the last line ‘response’ instead of ‘res’?
        It’s not a mistake?

  3. res – is the response from Uber API. response (it’s better to write Apperyio.response) is the response of Server Code script (which is also an API).

  4. Can you help me with this little code in Javascript + ajax for the Uber API estimates/price –

    var api_url = ‘https://api.uber.com/v1.2/estimates/price?start_latitude=’ + userLatitude + ‘&start_longitude=’ + userLongitude + ‘&end_latitude=’ + eLatitude + ‘&end_longitude=’ + eLongitude;
    // It’s working all fine till this point.
    $.ajax({
    type: ‘GET’,
    url: api_url,
    headers: {
    Authorization: “Token ” + sToken
    },
    Accept-Language: “en_US”,
    Content-Type: “application/json”,
    success: function(data){
    alert(data);
    }
    )};

    1. Hi Viplav – as this is not related to Appery.io Server Code, it’s best to contact the Uber developer support team directly.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.