Skip to main content

Smartcar Python Backend SDK Build Status PyPI version

Python package to quickly integrate Smartcar API

Resources

Installation

# Inside your virtual environment:
pip install smartcar

Usage

Authentication

Before integrating with Python SDK, you'll need to register an application in the Smartcar Developer portal. Once you have registered an application, you will have a Client ID and Client Secret, which will allow you to authorize users.

Now that you have your id, secret and redirect URI, here's a simple overall idea of how to use the SDK to authenticate and make requests with the Smartcar API.

  • In your terminal, export your client id, client secret, and redirect uri as environment variables.
export SMARTCAR_CLIENT_ID='<your client id>'
export SMARTCAR_CLIENT_SECRET='<your client secret>'
export SMARTCAR_REDIRECT_URI='<your redirect uri>'
  • Import the sdk import smartcar
  • Create a new smartcar client with smartcar.AuthClient()
import smartcar

client = smartcar.AuthClient()
  • Redirect the user to an OEM login page using the URL from client.get_auth_url(scope)
# Alter this list to specify the scope of permissions your application is requesting access to
scopes = ['read_vehicle_info', 'read_odometer', <scope3>...]

# Generate auth url for User OAuth flow
auth_url = client.get_auth_url(scopes)
  • The user will login, and then accept or deny the permissions in your scopes

    • If the user is already connected to your application, they will not be shown the accept or deny dialog. However the application can force this dialog to be shown with client.get_auth_url(options={"force_prompt":True})
  • If the user accepts, they will be redirected to your redirect_uri. The query field code will contain an authorization code. This is very important, so save it for later.

    https://redirect-url.example.com/?code=<AUTHORIZATION_CODE>

  • With your authorization code in hand, use client.exchange_code(authorization_code) to exchange your authorization code for an access object.

access_object = client.exchange_code(<authorization_code>)

This access object will look like this:

{
  "access_token": "...",
  "token_type": "Bearer",
  "expiration": "2018-05-02T18:04:25+00:00",
  "refresh_token": "...",
  "refresh_expiration": "2018-06-02T18:03:25+00:00",
  "expires_in": "..."
}
  • To make any vehicle data request to the Smartcar API, you'll need to give the SDK a valid access token. Access tokens will expire every 2 hours, so you'll need to constantly refresh them.

  • It was pretty hard getting that first access token, but from now on it's easy! Calling client.exchange_refresh_token(refresh_token) will return a new access object using a previous access object's refresh token. This means you can always have a fresh access token, by doing something like this:

def get_fresh_access():
    access = load_access_from_database()
    new_access = client.exchange_refresh_token(access['refresh_token'])
    put_access_into_database(new_access)
    return new_access


fresh_access_token = get_fresh_access()['access_token']

Vehicle Data and Commands

With your fresh access token in hand, use smartcar.get_vehicles(access_token) to get a list of the user's vehicles.

vehicles = smartcar.get_vehicles(<access_token>)

print(vehicles.vehicles)
# [ uuid-of-first-vehicle, "...", uuid-of-nth-vehicle ]

# Vehicle ID of first vehicle
vehicle_id = vehicle.vehicles[0]

# Get attributes of the first vehicle
vehicle_response = smartcar.get_vehicle(access_token, vehicle_id)
print(vehicle_response.get("body"))
  • Now with a vehicle id in hand, use smartcar.Vehicle(vehicle_id, access_token) to get a Vehicle object representing the user's vehicle.

  • Now you can ask the car to do things, or ask it for some data! For example:

vehicle = smartcar.Vehicle(vehicle_id, access_token)

# Calling GET signals endpoint
signals_response = vehicle.get_signals()
print(signals_response.body)

# Calling GET signals/{signal_code} endpoint
odometer_response = vehicle.get_signal("odometer-traveleddistance")
print(odometer_response.body)

Handling Exceptions

Any time you make a request to the Smartcar API, something can go wrong. This means that you really should wrap each call to client.exchange_code, client.exchange_refresh_token, client.get_vehicles, and any vehicle method with some exception handling code.

All exceptions will be of type smartcar.SmartcarException with the... exception of missing client credentials. Navigate below to AuthClient for more details.

Upon a vehicle rate limit error, see SmartcarException.retry_after (seconds) for when to retry the request.

Check out our API Reference and v2.0 Error Guides to learn more.

Supported Python Branches

Smartcar aims to support the SDK on all Python branches that have a status of "bugfix" or "security" as defined in the Python Developer's Guide.

In accordance with the Semantic Versioning specification, the addition of support for new Python branches would result in a MINOR version bump and the removal of support for Python branches would result in a MAJOR version bump.

Metadata

Release files for smartcar 6.19.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for smartcar 6.19.1
File Size Uploaded
smartcar-6.19.1.tar.gz 24.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for smartcar 6.19.1
File Interpreter ABI Platform
smartcar-6.19.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 49.2 kB

Release files / smartcar-6.19.1.tar.gz

Download URL smartcar-6.19.1.tar.gz
Size 24.2 kB
Tags Source
SHA-256 checksum
How to use checksums
cbce4eaaac5713fad6cdeb99ccf6c13d06974259cde11f26ca3515f20598584c
BLAKE2b-256 checksum
How to use checksums
58ff761a6327985b02c948a94259c8d547da6a3e54b89e3a2083b80273a25922
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / smartcar-6.19.1-py2.py3-none-any.whl

Download URL smartcar-6.19.1-py2.py3-none-any.whl
Size 25.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
5595ff552e3dbeb2327cdda77d2f828fc8f4033fded2ec48b9d43aae66c1a5db
BLAKE2b-256 checksum
How to use checksums
a3004141cbb7102bef69582f52692970c7a690e1ddbb2f658a7975f751d6a059
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release history Release notifications | RSS feed

This release

6.19.1 This release

2 release files

6.19.0

2 release files

6.18.3

1 release file

6.17.0

1 release file

6.16.0

1 release file

6.15.0

1 release file

6.14.0

1 release file

6.13.1

1 release file

6.13.0

1 release file

6.12.0

1 release file

6.11.0

1 release file

6.10.1

1 release file

6.10.0

1 release file

6.9.0

1 release file

6.8.0

1 release file

6.5.1

1 release file

6.5.0

1 release file

6.4.0

1 release file

6.3.0

1 release file

6.2.0

1 release file

6.1.0

1 release file

6.0.3

1 release file

6.0.2

1 release file

6.0.1

1 release file

6.0.0

1 release file

5.2.1

1 release file

5.2.0

1 release file

5.1.1

1 release file

5.1.0

1 release file

5.0.0

1 release file

4.3.5

1 release file

4.3.3

1 release file

4.3.2

1 release file

4.3.1

1 release file

4.3.0

1 release file

4.2.4

1 release file

4.2.3

1 release file

4.2.2

1 release file

4.2.1

1 release file

4.2.0

1 release file

4.1.5

1 release file

4.1.4

1 release file

4.1.3

1 release file

4.1.1

1 release file

4.1.0

1 release file

4.0.0

1 release file

3.0.4

1 release file

3.0.3

1 release file

3.0.1

1 release file

3.0.0

1 release file

2.0.1

1 release file

2.0.0

1 release file

1.2.1

1 release file

1.2.0

1 release file

1.1.0

1 release file

1.0.13

1 release file

1.0.12

1 release file

1.0.11

1 release file

1.0.10

1 release file

1.0.9

1 release file

1.0.8

1 release file

1.0.7

1 release file

1.0.6

1 release file

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page