This project contains all endpoints you need to integrate Uber's API in your application
Before begin, you need to register your app in the Uber developer dashboard. Notice that the app gets a client ID, Client Secret, and Server Token required for authenticating with the API. After registering your application, you need to install this module in your Node.js project:
npm install node-uber-rider
So as to use this module, you have to import it in your application first:
var Uber = require('node-uber-rider');
Next, initialize the Uber object with the keys you obtained from the Uber developer dashboard:
var uber = new Uber({
clientID: 'CLIENT_ID',
clientSecret: 'CLIENT_SECRET',
redirectURI: 'REDIRECT_URI',
//If you already have the authentication infos (they are all optional)
access_token: 'SERVER_TOKEN',
refresh_token: 'REFRESH_TOKEN',
selfScopes:[scopes]
});
To make calls in the API, you need to create an authenticated session with the API. User-specific operations require you to use a OAuth 2 bearer token with specific scopes. General operations can use a simple server-token authentication.
You do it in two steps. Step One, request the User's Code uber.getAuthorizeUrl([scopes])
with the scopes you need, step two, request the User's Bearer Token uber.getUserToken(UserCode,[scopes],callback)
using the User's Code.
To obtain a bearer token, you have to authorize your application with the required scope. Available scopes are: history
, history_lite
, profile
, request
, all_trips
, and places
.
To do so, you are initially required to redirect your user to an authorization URL. You can generate the authorization URL using uber.getAuthorizeUrl([scopes])
. In case you are using Express, your route definition could look as follows:
app.get('/getAuth', function(req,res){
var url = uber.getAuthorizeUrl(['history','profile','all_trips']);
res.redirect(url);
});
The URL will lead to a page where your user will be required to login and approve access to his/her Uber account. In case that step was successful, Uber will issue an HTTP 302 redirect to the redirect_uri defined in the Uber developer dashboard. On that redirect, you will receive an authorization code, which is single use and expires in 10 minutes.
To complete the authorization you now need to receive the callback and convert the given authorization code into an OAuth access token. You can accomplish that using uber.getUserToken(UserCode,[scopes],callback)
. This method will retrieve and store the access_token, refresh_token and authorized scopes with the uber object for consecutive requests.
Using Express, you could achieve that as follows:
app.get('/getCode', function(req, res){
code = req.query.code;
var token = uber.getUserToken(code,['history','profile','all_trips'],function(err,data){
if(err)
{
console.log(err);
}
res.send("token: "+ data);
});
});
After getting the authorize url, the user will be redirected to the redirect url with authorization code used in the next function.
uber.getAuthorizeUrl([scopes]);
uber.getAuthorizeUrl(['history','profile', 'request', 'places']);
uber.getUserToken(UserCode,[scopes],callback);
uber.getUserToken(code,['history','profile','all_trips'],function(err,token){
if(err){
console.log(err);
}
console.log("token: "+ token);
});
uber.getUserToken(code,['history','profile','all_trips'],function(err,token){
if(err){
console.log(err);
}
var refresh_token = uber.refresh_token;
console.log("refresh_token: "+ refresh_token);
});
uber.me(callback);
uber.me(function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
uber.history(limit,offset,callback);
uber.history(5,0,function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
uber.paymentMethods(callback);
uber.paymentMethods(function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
uber.getPlaceByPlaceId(palceId,callback);
uber.getPlaceByPlaceId('home',function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
uber.applyPromotion(promotionCode,callback);
uber.applyPromotion('FREE_RIDEZ',function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
uber.updateHomeOrWork(placeId,newAddress,callback);
uber.updateHomeOrWork('home','New Street St',function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
uber.products([coordinates],callback);
uber.products(['lat','long'],function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
uber.getProductByProductId(productID,callback);
uber.getProductByProductId('a1111c8c-c720-46c3-8534-2fcdd730040d',function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
uber.estimatePrice([startAndEndCoordinates],callback);
uber.estimatePrice(['startLat','startLon','endLat','endLon'],function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
uber.estimateTime([startAndEndCoordinates],callback);
uber.estimateTime(['startLat','startLon','endLat','endLon'],function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
var infos = {
'start_latitude':'start_latitude',
'start_longitude':'start_longitude',
'end_latitude':'end_latitude',
'end_longitude':'end_longitude',
'product_id':'product_id'
};
uber.requestEstimate(infos,callback);
var infos = {
'start_latitude':'start_latitude',
'start_longitude':'start_longitude',
'end_latitude':'end_latitude',
'end_longitude':'end_longitude',
'product_id':'product_id'
};
uber.requestEstimate(infos,function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
var infos = {
'start_latitude':'start_latitude',
'start_longitude':'start_longitude',
'end_latitude':'end_latitude',
'end_longitude':'end_longitude',
'product_id':'product_id',
'fare_id':'fare_id'
};
uber.createRequest(infos,callback);
var infos = {
'start_latitude':'start_latitude',
'start_longitude':'start_longitude',
'end_latitude':'end_latitude',
'end_longitude':'end_longitude',
'product_id':'product_id',
'fare_id':'fare_id'
};
uber.createRequest(infos,function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
uber.currentRequest(callback);
uber.currentRequest(function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
The Ride Request endpoint allows retrieving the status of an ongoing or completed trip that was created by your app.
uber.getRequestDetails(requestId,callback);
uber.getRequestDetails(requestId,function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
uber.getRequestMap(requestId,callback);
uber.getRequestMap(requestId,function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
uber.getRequestReceipt(requestId,callback);
uber.getRequestReceipt(requestId,function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
uber.updateOngoingDestination([endCoordinates],callback);
uber.updateOngoingDestination(['endLat','endLon'],function(err,data){
if(err){
console.log(err);
}
console.log(data);
});
uber.cancelCurrentRequest(callback);
uber.updateOngoingDestination(function(err,data){
if(err){
console.log(err);
}
console.log(data);
});