Three lines

Uber

Developers

Python

Access to the Driver API is currently limited access. If you are interested in using this API, you can apply for access on the Drivers Product Page.

This tutorial walks you through the steps to implement the Driver API in your Python application. The Python SDK is an easy way to add Uber functionality into your app. You’ll set up and configure the SDK, and then work through an example Driver integration.

Install the SDK

Once your app is registered on the developer dashboard, you’re ready to install the SDK. To install, run:

pip install uber-rides

The Python SDK is open source, so you can find it on Github.

Create an Uber session

Once you’ve created your app, you’ll be given a client_id and client_secret. These are used to authenticate your application and the driver when calling the API.

The following endpoints require a driver access token:

Privileged Scope This endpoint requires a privileged scope to be used in production by all Uber riders. You can use this endpoint immediately when authenticated as yourself or any of your 5 registered developers. When you are ready to distribute your application broadly for use by all Uber riders, you may request FULL ACCESS. For more information read about scopes.

Authorize a driver for your application

To fetch a driver’s profile, trips, or payments a driver must grant access to your application through the OAuth 2.0 Authorization Code flow. See the Uber API docs on authentication for more information.

from uber_rides.auth import AuthorizationCodeGrant
auth_flow = AuthorizationCodeGrant(
    <CLIENT_ID>,
    <SCOPES>,
    <CLIENT_SECRET>,
    <REDIRECT_URI>
)
auth_url = auth_flow.get_authorization_url()

Replace the values of each placeholder above (<CLIENT_ID>, <CLIENT_SECRET>,<REDIRECT_URI> and <SCOPES>). You can find these values in your developer dashboard under the your application’s Auth tab. The <REDIRECT_URI> must match the one of the values listed in your dashboard.

Have drivers sign in and use the auth_url to grant access to your application. After authorization is complete, drivers are redirected to the redirect_url. The redirect_url uses this format: <REDIRECT_URI>?code=UNIQUE_AUTH_CODE. The redirect_url creates a session and starts the UberRidesClient.

from uber_rides.session import Session
from uber_rides.client import UberRidesClient

session = auth_flow.get_session(redirect_url)
client = UberRidesClient(session, sandbox_mode=True)
credentials = session.oauth2credential

Make sure to store credentials information in a secure data store so drivers don’t need to go through the authorization processes repeatedly. The SDK handles token refreshing automatically when it makes API requests with an UberRidesClient.

Fetch a drivers profile

response = client.get_driver_profile()
profile = response.json

first_name = profile.get('first_name')
last_name = profile.get('last_name')
email = profile.get('email')

Fetch a drivers trips

response = client.get_driver_trips()
trips = response.json

Fetch a drivers payments

response = client.get_driver_payments()
payments = response.json

Congratulations! You finished the Driver API tutorial and can access trips and payments!

Uber

Developers
© 2023 Uber Technologies Inc.