Skip to content

mchesler613/smartcar

Repository files navigation

Django with Smartcar API Integration

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.

GOALS

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.

Dev Environment Part 1

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

Set up credentials at Smartcar

  • 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 to http://localhost:8000/exchange/

Dev Environment Part 2

  • export the 3 smartcar credentials to your local environment
  • don't forget to setup your DJANGO_SETTINGS_MODULE as well to django_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.

login

  • 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) authorize
  • Click on the Toyota to proceed to this page allow
  • 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 vehicles list
  • 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>/ one vehicle

Summary

There are 4 endpoints created for this Django app that integrates with the Smartcar API.

  • /authorize/, which is redirected from http://localhost:8000/ to retrieve an authorization url
  • /exchange/, is the redirect_url that is waiting to receive a code from the authorization url. This code is exchanged for an access 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 the access code
  • /vehicles/<str:id>, is a single retrieve endpoint for vehicle data based on its id and using the access 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!