Connect to your Tesla through an API!

Daniel Vega
Future Vision
Published in
4 min readJun 15, 2019

Get started connecting and even controlling your Tesla today!

I’m going to show you how to connect to your Tesla. There are multiple ways to achieve this but today we will focus on getting this done using python. With this python Tesla API example you can:

  • Get all the details about your car; hardware, options, etc.
  • Get the current drive_state i.e the location of the vehicle, speed, direction, etc.
  • Get the current charge_state i.e battery range, estimated range, charge limit, etc.
  • Get the current climate_state i.e temperature settings, seat heater status, outside temp, etc.
  • Get the service records

All the source code for this tutorial can be found here. The original API information can be found here. I used rauth to make the connection, you can find that here.

Want to find out what hardware your Tesla came with? Then this tutorial is for you! It tends to be difficult to track the changes in all Tesla Vehicles since the company adds and removes features so often. Sometimes even on a monthly basis as opposed to a yearly basis (what most auto manufacturers do). Therefore, when buying or selling a used Tesla, it can be difficult to know what features or updates will be compatible with your particular model. We can use this API to find out exactly what options, hardware, and firmware the vehicle is equipped with.

This article will be geared towards someone with basic experience with the following:

  • HTTP (GET & POST) requests
  • Programming experience (preferably python)

We will go through the entire process for using the Tesla API using OAuth2.

Getting Started: Get the client_id and client_secret

You can find that here. Once you have this information you are ready to start using the Tesla API with the python editor of your choice. I like to use jupyter notebook but anything will work.

Creating the Authentication headers

  • Begin by importing the libraries
  • Define your id and key
  • Format the information
  • Use OAuth2Service to begin the connection
from rauth import OAuth2Service
import json
service = OAuth2Service(
client_id = '<insert client key here>',
client_secret = '<insert client secret here>',
access_token_url = "https://owner-api.teslamotors.com/oauth/token",
authorize_url = "https://owner-api.teslamotors.com/oauth/token",
base_url = "https://owner-api.teslamotors.com/",
)

Next, let’s build the request that we will use for the authentication for Tesla. To do this, we will need to complete the following steps:

  • Create the authentication data
  • Create variables for your Tesla credentials (If you plan to share your code, use environment variables)
  • Using OAuth2Service send the request
email = '<insert Tesla email here>'
password = '<insert Tesla password here>'
data = {"grant_type": "password",
"client_id": '<insert client key here>',
"client_secret": '<insert client secret here>',
"email": email,
"password": password}
session = service.get_auth_session(data=data, decoder=json.loads)

To check to make sure that your request worked, you can take a look at the authentication response status code(session.response). Status code 200 means that the request worked!

Next, we will take the response and get the access key for our future requests. This is called the bearer token and it gives your app authenticated access to the Tesla API!

access_token = session.access_token
my_session = service.get_session(token=access_token)

You are now ready to explore the Tesla API with your authenticated session!

Get a list of Vehicles

In order to GET the vehicle information we need to know the vehicle “id,” thankfully we can get a list of vehicles by sending a GET request to the following url:

url = 'https://owner-api.teslamotors.com/api/1/vehicles/'

So our GET request would look like this:

vehicles = my_session.get(url).json()

If you want the information directly you can get to the first Tesla(if you have more than one, hats off to you!) on the list by doing the following instead:

vehicles = my_session.get(url).json()['response'][0]

Set the vehicle id to a variable so we can access it’s specific data

Car_1 = vehicles['id_s'] #id_s is the id as a string

Get Vehicle Information

Now that we have what we need, go ahead and send GET requests for anything you want to see, in order to get a summary use the following endpoint:

'/api/1/vehicles/{}/vehicle_data'.format(Car_1)

This will return (I’ve also added the endpoints in case you want them individually):

  • Vehicle Information
  • Drive State: ‘/api/1/vehicles/{id}/data_request/drive_state’
  • Climate State: ‘/api/1/vehicles/{id}/data_request/climate_state’
  • Charge State: ‘ /api/1/vehicles/{id}/data_request/charge_state’
  • Gui Settings: ‘/api/1/vehicles/{id}/data_request/gui_settings’
  • Vehicle State: ‘/api/1/vehicles/{id}/data_request/vehicle_state’
  • Vehicle Config: ‘/api/1/vehicles/{id}/data_request/vehicle_config’

Also, you can get nearby charging sites here:

  • ‘/api/1/vehicles/{id}/nearby_charging_sites’

From, this tutorial you should be able to get more information than what you signed up for!

If you’re interested in learning how to code in python, click here for a great place to start!

--

--