JS SDK Guide
¶ Getting Started
The Uber JS SDK allows you to easily integrate Uber Direct APIs and Organization APIs into your applications. This guide will help you get started with setting up the JS SDK and making your first API calls. It assumes you are familiar with JavaScript and Node.js and that you’ve installed Node.js and npm.
¶ Requirements
¶ Uber Direct Account
To create an Uber Direct developer account, visit Get Started Section and follow the steps listed there. If you already have an account, log in and you can find your keys on the Developer tab in the dashboard.
Your app must have the following scopes to access each API:
- DaaS API:
eats.deliveries
- Organizations API:
direct.organizations
You should pass in these scopes when requesting an access token (see Authentication below).
¶ Node
This package supports Node versions 18 and above. You can install it with brew
on Mac:
brew install node@18
For Windows, check out nvm-windows.
For more Node downloads, check out this link.
¶ Installation
To install the Uber JS SDK, run the following command:
npm install uber-direct
¶ Set Environment Variables
To use the Uber Direct JS SDK, you must authenticate with a client ID and secret. To make API calls, you’ll also need a customer token. Set these as environment variables.
export UBER_DIRECT_CLIENT_ID=your_client_id
export UBER_DIRECT_CLIENT_SECRET=your_client_secret
export UBER_DIRECT_CUSTOMER_ID=your_customer_token
¶ Authentication
To authenticate with the Uber API, use your client ID and client secret that you have set in your environment variable file to obtain an access token. Here is an example of how to do this:
import { getAccessToken } from "uber-direct/auth";
getAccessToken()
.then((token) => console.log(`Your access token is: ${token}`))
.catch((error) =>
console.error(`Error fetching access token: ${error.message}`)
);
¶ Making API Calls
Here are examples of how to make API calls using the Uber JS SDK.
More examples in Github Samples Repository
¶ Getting a Delivery Quote
import { getAccessToken } from 'uber-direct/auth';
import { createDeliveriesClient } from "uber-direct/deliveries";
const token = await getAccessToken();
const deliveriesClient = createDeliveriesClient(token,customer_id);
const quoteReq = {
pickup_address: "{\"street_address\":[\"100 Maiden Ln\"],\"city\":\"New York\",\"state\":\"NY\",\"zip_code\":\"10023\",\"country\":\"US\"}",
dropoff_address: "{\"street_address\":[\"30 Lincoln Center Plaza\"],\"city\":\"New York\",\"state\":\"NY\",\"zip_code\":\"10023\",\"country\":\"US\"}",
};
const quote = await deliveriesClient.createQuote(quoteReq);
¶ Creating a Delivery
import { getAccessToken } from 'uber-direct/auth';
import { createDeliveriesClient } from "uber-direct/deliveries";
const token = await getAccessToken();
const deliveriesClient = createDeliveriesClient(token,customer_id);
const deliveryRequest = {
pickup_name: 'Store Name',
pickup_address: "{\"street_address\":[\"100 Maiden Ln\"],\"city\":\"New York\",\"state\":\"NY\",\"zip_code\":\"10023\",\"country\":\"US\"}",
pickup_phone_number: '+14155551212',
dropoff_name: 'Customer Name',
dropoff_address: "{\"street_address\":[\"30 Lincoln Center Plaza\"],\"city\":\"New York\",\"state\":\"NY\",\"zip_code\":\"10023\",\"country\":\"US\"}",
dropoff_phone_number: '+14155551212',
manifest_items: [
{
name: 'Thing 1',
quantity: 1,
size: 'small',
price: 1000
},
]
};
const delivery = await deliveriesClient.createDelivery(deliveryRequest);
¶ Support
If you find a bug in the code that needs to be fix or you want to do a feature request to be included in the SDK you can do it in this form: https://p.uber.com/sdk-bugs