If you have a modern vehicle with telemetry installed and remote telemetry access via a mobile app, such as Toyota Connected Services or other similar apps, you can use the Smartcar API as a single entry point to retrieve data for all your remotely-connected vehicle(s).
I was asked to play around with the Smartcar API and I obliged. After a brief exploration, I found that the Smartcar tutorial comes with Python SDK samples that work with the Flask Web Framework. Having been a Djangonaut for the past several years, I can't help but create a miniature Django WIP (Work-in-progress) version for this.
At the end of this article, you will be able to:
- run a local Django service to query a small sampling of the Smartcar APIs to return vehicle data with the OAuth2 web protocol.
For this tutorial, I set up my development environment with Poetry and Pyenv. To begin:
- git clone this repo
- activate pyenv for python 3.12
pyenv shell 3.12
- configure poetry to use pyenv
poetry env use 3.12
- activate poetry's virtual environment
source $(poetry env info --path)/bin/activate
- run
poetry install
- Create a developer account at Smartcar
- Login to your developer account and copy the values for these 2 important credentials
SMARTCAR_CLIENT_ID
SMARTCAR_CLIENT_SECRET
to be exported later to your development environemnt.
- Set the value of this credential
SMARTCAR_REDIRECT_URI
to point tohttp://localhost:8000/exchange/
- export the 3 smartcar credentials to your local environment
- don't forget to setup your
DJANGO_SETTINGS_MODULE
as well todjango_smartcar.settings
- migrate your Django migration files
./manage.py migrate
- run your Django server locally
./manage.py runserver
- open up a browser and access your Django service at
http://localhost:8000
. This url will redirect you to this page to connect to your vehicle's connected service app.
- Click
Continue
and login to your vehicle's remote connect app - You will be presented with a view like this (if you have a Toyota)
- Click on the
Toyota
to proceed to this page - Since the data is fake, you might as well click
Allow
to proceed - You are redirected to this Django endpoint that is created for this app,
/localhost:8000/vehicles/
which list the vehicles that you alllowed for access - If you are interested in only one vechile at a time, you can pass the
id
to this endpoint that I created for this app,/localhost:8000/vehicles/<str:id>/
There are 4 endpoints created for this Django app that integrates with the Smartcar API.
/authorize/
, which is redirected fromhttp://localhost:8000/
to retrieve an authorization url/exchange/
, is the redirect_url that is waiting to receive acode
from the authorization url. This code is exchanged for anaccess code
to be passed to subsequent queries for vehicle data/vehicles/
, is the list endpoint that returns a list of allowed vehicles for viewing using theaccess code
/vehicles/<str:id>
, is a single retrieve endpoint for vehicle data based on its id and using theaccess code
Last but not least, you can extend this app to include a more comprehensive listing of Smartcar API endpoints and other supported brands and have fun. Happy Django!
An online demo for this app is available here. Enjoy!