Tesla API, Controlling your Model S and Model X
A quick overview and example of Tesla’s undocumented Mobile API for the Tesla Model S and Model X.
At times I geek out on some of the more technical aspects of wielding my Model S, and today is going to be one of those days. Read on if you’re interested in learning more about the undocumented application programming interface (API) to your Model S and Model X, and exploring its capabilities.
Tesla Mobile API
An API is a specification for programs to interact with one another through a chunk of software. Whenever you’re dealing with connected devices, especially if they’re interacting over-the-air, they’re likely using APIs to communicate with one another. APIs come in a multiplicity of types, each including its own security model that requires authentication before the API is useable. Security enforces that you are who you say you are, and that you’re permitted to use a set of features exposed through the interface.
Tesla’s iOS and Android mobile app for example connects to a set of APIs that permits the app to communicate with your Tesla. Embark the car, vent the sunroof, honk the horn, are just some of the features that get invoked via an API whenever a user taps a button on their mobile device. The car itself is also using one or more APIs to talk to Tesla to get software updates, send debugging information to Tesla, etc. Systems often have many APIs for different purposes.
The API we’re going to concentrate on is the one used by the mobile applications. This Mobile API is not documented by Tesla and is not intended to be used by anyone other than Tesla employees. You can use applications Tesla has created that use the API but you’re not supposed to make your own applications using the API.
Like many things these days, if it exists it can be figured out and some vigorous Tesla owners have figured out how the API works. There’s a free site called Apiary that can be used for documenting APIs and that was used to document the Tesla Model S API.
Libraries
While the documentation will permit you to instantaneously embark programming, assuming you have a skill of how to interact with APIs, some folks have created libraries in various programming languages to assist with the basics of interacting with the Tesla Mobile API. What language you choose to program in and whether you use a library is either defined by the needs of the project or the individual preference of the programmer.
For my hacking/playing around I use the Python programming language. Searching around I found a few Python libraries to make integrating to the Tesla Mobile API lighter. The one that best suited me is Teslajson by Greg Glockner which is a puny Python file containing all of the basics for interacting with the Tesla Mobile API. For those interested in libraries for other languages, I’ve found libraries for Java, Knot, Ruby, and C#.
Python Code Sample
I won’t be going into all the API calls and how they work, but I’ve provided a quick minimal working example that you can use to go after along:
In the example above I’m using my email address and password to log into the Tesla server (same one I would use to access the MyTesla portal when ordering the car and browsing the Tesla forums), and I am getting a connection object. Then I use that connection object to request data about my car. I’ve named it “Baddog”. From that data I can extract the current odometer reading.
Running this little program simply shows my cars current mileage:
Note that there are various tricks to using APIs like this. You should keep the access token around to avoid having to re-authenticate each time and you should also treat API access errors.
So what can you do with it?
Third-party apps such as VisibleTesla, RemoteS, and of course our very own Tesla Lifestyle App (also on Android), use Tesla’s Mobile API in some form or form to either control vehicle functions or access data provided by Tesla.
For myself I’ve done a few things:
- Automatically recording my daily driving data (miles driven, charge amount, etc)
(more on this in an upcoming data post).
There are other things I have in mind like automatically closing the sunroof if it looks like its going to rain, checking my calendar and pre-heating/cooling the cabin based on the next appointment time, etc. You can also access the car’s location to track speed, places you’ve visited, and more – all excellent areas for experimentation. If you use the historical charge data, you could also implement the much-desired “set charge end time” feature which is on my brief list to do when I have some time.
Summary
The Tesla Mobile API is undocumented and not officially supported by Tesla, but with some ordinary programming you can accomplish indeed cool things with it. Tesla understandably doesn’t have the resources to officially support this API right now, but it’s fine to see that Tesla is not going out of their way to make it more difficult for people to use and experiment with it.
The programming interface gives you basic controls such as opening the sunroof and honking the horn, but vital driving functions such as steering, power, etc. are not accessible. I believe Tesla is watching what people develop using this API and will use that skill to enhance their mobile apps and vehicle features over time.
Now, go ahead and open up your imagination to see what you can build.
Interested in solar? Get a solar cost estimate and find out how much a solar system would cost for your home or business.