# Tesla Owner API and Car Data



## twm01 (Jul 30, 2018)

Has anyone created or know of a simple web interface that queries your car and displays the results? I.E. the phone app and 3rd party sites like TeslaFi do some API call to the car and does something with the returned values (stores in a database, displays the values, etc.). Is there a simple web interface that one could enter their credentials or token and get/view the returned values? I am sure this is a simple thing for any web programmer, just looking for a tool the rest of us can use.

TIA.


----------



## NOGA$4ME (Sep 30, 2016)

This is a good starting point: https://tesla-api.timdorr.com/

Many people have written various apps and web pages that work off of this base.


----------



## twm01 (Jul 30, 2018)

NOGA$4ME said:


> This is a good starting point: https://tesla-api.timdorr.com/
> 
> Many people have written various apps and web pages that work off of this base.


Thanks - that looks like a start to what I am looking for - I have heard the terms JSON and RESTful API, etc. but have no idea how to set up something that authenticates, calls the api, then displays the results... I am sure it would be trivial for someone to slap up a simple web page that takes care of all that - just thought someone might have done it already


----------



## theloneranger08 (Sep 29, 2017)

Personally, I wouldn't trust a fan-made web page to control my car unless there was a guarantee that my data wouldn't be stored anywhere.


----------



## NOGA$4ME (Sep 30, 2016)

I think the OP wants to make their own web page. They would be using the 3rd party LIBRARY, but it would be running on their own machine with their own code.


----------



## twm01 (Jul 30, 2018)

NOGA$4ME said:


> I think the OP wants to make their own web page. They would be using the 3rd party LIBRARY, but it would be running on their own machine with their own code.


My thought was a web page anyone could use, but I guess there would be security concerns... so that said, can someone walk me through how to make the web call on my own??


----------



## Bokonon (Apr 13, 2017)

twm01 said:


> My thought was a web page anyone could use, but I guess there would be security concerns...


The closest solution I can think of would be a simple web page, ideally with no external source files, that called the Tesla API directly from client-side Javascript to generate your token. The page's source code would be concise and readable, so that anyone knowledgeable could easily confirm that it didn't do anything with your credentials other than package them up and send them to Tesla.



> so that said, can someone walk me through how to make the web call on my own??


Do you have a Mac? If so, open a Terminal window, modify the command below to use your Tesla account's email address and password, and run it:


```
curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "grant_type=password" -F "client_id=81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384" -F "client_secret=c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3" -F "[email protected]" -F "password=********" "https://owner-api.teslamotors.com/oauth/token"
```
If you have Windows, you can download Curl from here and run the above command from a command-prompt window. I can provide more detailed instructions if needed.

EDIT: I should note that the above command only generates a Tesla API access token, it does not actually return any information about your car. Once you have a token, you will need to make additional API calls (with your access token) to query your car's status. The Apiary documentation linked above can help you generate curl commands for these API calls.


----------



## twm01 (Jul 30, 2018)

That's getting me close! I downloaded curl, then executed the command substituting my acct email and pw. It seems that all it did was give me an access token?
{"access_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","token_type":"bearer","expires_in":3888000,"refresh_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXX","created_at":1539298492}

Is there a second command I need to enter to get the state values indicated in the api documentation linked above?
(I am on Win10)

Thanks so much for the info - it is greatly appreciated!


----------



## twm01 (Jul 30, 2018)

Just saw your edit; I will see if I can figure out how to make the state call using the token... wish me luck


----------



## Bokonon (Apr 13, 2017)

twm01 said:


> That's getting me close! I downloaded curl, then executed the command substituting my acct email and pw. It seems that all it did was give me an access token?


Perfect! That's exactly what you want!

Next up is to query your Tesla account to get your vehicle ID. You can do this by using the "vehicles" endpoint, like so:


```
curl --include --header "Authorization: Bearer {access_token}" "https://owner-api.teslamotors.com/api/1/vehicles"
```
That should give you a JSON response with an array of vehicles under your account. For each vehicle, you can see its VIN, configuration codes, current state (asleep / online / etc), and a few other things. Note the value of the "id" property (*not* the "vehicle_id" property), because you will use it with the following commands.

If you want to get more specific information about your vehicle, you'll need to wake it up if it's currently asleep. You can do this by opening the Tesla app, or you can issue a "wake" command through the API. (Note: replace {vehicle_id} with the value of the "id" property from the previous command.)


```
curl --include --request POST --header "Authorization: Bearer {access_token}" "https://owner-api.teslamotors.com/api/1/vehicles/{vehicle_id}/wake_up"
```
The response to this command may indicate that your vehicle is still asleep, but if you're within earshot of your driveway/garage, you may hear your car make the usual clunking sounds that signal that it's waking up. After a short wait, your car should be awake (and you can confirm this by issuing the same "wake" command and checking the car's status).

Okay, so now your car's awake, how can you find out everything there is to know about its current state? Well, there's a handy endpoint for that:


```
curl --include --header "Authorization: Bearer {access_token}" "https://owner-api.teslamotors.com/api/1/vehicles/{vehicle_id}/data"
```
That will basically give you everything the API knows about the car's current state and configuration. Copy and paste the JSON response into your favorite JSON formatting tool (example) to make it legible, and enjoy!

(Side note: looks like some new fields specific to firmware V9 capabilities have been added recently. Cool!)

For other API endpoints and further reading, Tim Dorr has a more complete, updated version of the API documentation here. The old Apiary site is still useful for quickly generating curl command with the correct syntax, but the documentation there is no longer being maintained.


----------



## webdriverguy (May 25, 2017)

Bokonon said:


> The closest solution I can think of would be a simple web page, ideally with no external source files, that called the Tesla API directly from client-side Javascript to generate your token. The page's source code would be concise and readable, so that anyone knowledgeable could easily confirm that it didn't do anything with your credentials other than package them up and send them to Tesla.
> 
> Do you have a Mac? If so, open a Terminal window, modify the command below to use your Tesla account's email address and password, and run it:
> 
> ...


Just an observation I would never post client_id and secret on a public forum.


----------



## twm01 (Jul 30, 2018)

Bokonon said:


> Perfect! That's exactly what you want!
> 
> Next up is to query your Tesla account to get your vehicle ID. You can do this by using the "vehicles" endpoint, like so:
> 
> ...


Thank you @Bokonon !! That gives me exactly what I was looking for. I appreciate you taking the time to give me the exact syntax needed - I knew what I wanted to do, just not how to do it! I hope that others find this as useful as I did.


----------



## Bokonon (Apr 13, 2017)

webdriverguy00 said:


> Just an observation I would never post client_id and secret on a public forum.


Normally I'd agree, but they already appear in the Apiary documentation, as well as just about every forum post, how-to article, or StackOverflow topic that discusses the Tesla API, so they're a pretty poorly-kept secret. I figured that including them in yet another copy-and-paste example wasn't going to do any additional harm.


----------



## NOGA$4ME (Sep 30, 2016)

webdriverguy00 said:


> Just an observation I would never post client_id and secret on a public forum.


Yeah, this is technically not a problem at all. This client_id and secret is for the Tesla app itself, and the values are readily available to anyone with a modest amount of technical knowledge, and have been widely distributed. Just about every non-Tesla app/service makes use of them. It's actually one of worst kept secrets around!

This was discussed at length in a thread that @twm01 would probably find very useful (but which I've lost for the moment). Basically the Tesla app should never have exposed it's secret key (so that it kept ownership of its client-specific privileges) and second, Tesla should make available the mechanism for 3rd party clients to request their own client id, issue secret keys, and allow the user to set a reduced set of permissions for each separate client (and revoke access if necessary). But, alas, there is no such mechanism so everyone just uses the Tesla app's client/secret. The funny thing is that everyone generates access tokens from that key thinking that somehow it's more secure than providing their username/password, when in reality that's not really the case (other than the case where you are re-using your Tesla password for other services).


----------



## Skelly (Aug 15, 2018)

This might be a little off-topic, but has anyone use the python library teslajson to get info about their car? 

Overall, it works pretty well, but I can't figure out how to 'ping' the car to see if it is sleeping or not. If I do a data request, it wakes up to give me the data. I am having trouble figuring out the command to just get the online state....


----------



## webdriverguy (May 25, 2017)

Skelly said:


> This might be a little off-topic, but has anyone use the python library teslajson to get info about their car?
> 
> Overall, it works pretty well, but I can't figure out how to 'ping' the car to see if it is sleeping or not. If I do a data request, it wakes up to give me the data. I am having trouble figuring out the command to just get the online state....


Can you please post the link? I am interested in checking it out


----------



## Skelly (Aug 15, 2018)

webdriverguy00 said:


> Can you please post the link? I am interested in checking it out


Here is the link to the python library:

https://github.com/gglockner/teslajson


----------



## webdriverguy (May 25, 2017)

Skelly said:


> Here is the link to the python library:
> 
> https://github.com/gglockner/teslajson


Thank you. Going to check it out.


----------



## HeavyPedal (Oct 28, 2018)

As a bit of a Smart Home geek, I'm accessing the Tesla API via SmartThings/webCoRE to generate & maintain a current token, log vehicle information, send charge reminders, and change settings based on time or other triggers, such as routines. My wife and I can even ask Alexa to perform such tasks as warming up and unlocking the car. Still tinkering to figure out the best use of all the capability.

For those not familiar with the webCoRE smartapp, this may look a bit intimidating at first, but in reality any SmartThings user with modest computer skills and a modicum of programming experience can build on the initial work presented here.


----------



## john5634 (Jan 31, 2019)

Hi Everyone! I keep getting a WWW-Authenticate: Bearer realm="Doorkeeper", error="invalid_token", error_description="The access token is invalid"

Any ideas?

This is the command i'm using

curl --include --header "Authorization: Bearer {f4067336754656b96345345ggsdgd45345gsd4565f076cce3c965ddfc22e}" "https://owner-api.teslamotors.com/api/1/vehicles"

Token is scrambled, but I wanted to show the exact format.

Thank you all!!!


----------



## Bokonon (Apr 13, 2017)

john5634 said:


> Hi Everyone! I keep getting a WWW-Authenticate: Bearer realm="Doorkeeper", error="invalid_token", error_description="The access token is invalid"
> 
> curl --include --header "Authorization: Bearer {f4067336754656b96345345ggsdgd45345gsd4565f076cce3c965ddfc22e}" "https://owner-api.teslamotors.com/api/1/vehicles"


The error message and realm suggest that the token itself is malformed. Are you surrounding the token in curly braces ({})? If so, that's the issue... remove the curly braces, and it should work. e.g.:


```
curl --include --header "Authorization: Bearer f4067336754656b96345345ggsdgd45345gsd4565f076cce3c965ddfc22e" "https://owner-api.teslamotors.com/api/1/vehicles"
```


----------



## zephyrus (Jun 22, 2020)

Bokonon said:


> Perfect! That's exactly what you want!
> 
> Next up is to query your Tesla account to get your vehicle ID. You can do this by using the "vehicles" endpoint, like so:
> 
> ...


I would like to make the process of getting data automatic (I want to have an app that sends notification if the car is at home and not plugged in). I'm able to manually do this by copying/pasting results from cURL queries, but I want to put this into a BASH shell script and I don't know how to transfer the results from one cURL command to the next. Here's an example:

```
result="$(curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"\

        -F "grant_type=password" -F "client_id=$CLIENT_ID" -F "client_secret=$CLIENT_SECRET" -F "email=$EMAIL" -F "password=$PASSWORD"\

        "https://owner-api.teslamotors.com/oauth/token")"
```
The output from this is:

result:'{"access_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","token_type":"bearer","expires_in":3888000,"refresh_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","created_at":1592850440}'

where XXXXXXXXXXXXXX is a hidden entry. I'm a novice programmer and am unable to take part of the result (e.g. "access_token") and put it in the next cURL:

```
curl --include --header "Authorization: Bearer {access_token}" "https://owner-api.teslamotors.com/api/1/vehicles"
```
Is there an easy way to do this with BASH or is it better to use JavaScript or Python? (I'm not very good at either)


----------



## CleanEV (May 17, 2018)

@twm01- Not sure if below 2 links can guide you in your project

@JWardell has written a lot about this at this link
Additionally there is teslamate that I have Been using - this one can be web exposed if you follow instruction.


----------



## NOGA$4ME (Sep 30, 2016)

BASH is probably not the best tool for this job, but if that's what you are best at, then you will have to make do with using other commands (e.g. cut) to pick apart the results of the return string.

But honestly, if I were you, I would invest a bit of time in learning a language that is more suited to the task. You don't have to become an expert it in, and thankfully there is already plenty of sample code out there that does the "tough" part of authenticating and sending requests, and usually those languages (like Python, and my preference, PHP) that will more or less natively pick apart that result string for you.

OR, another option is to use a tool that already does exactly what you want. Maybe not as fun as coding your own solution, and you may end up paying for it, but something like TeslaFi already does exactly what you are doing. You can set it to send you a text if the car is at home, past a certain time, below a certain SOC, and not plugged in.

It all boils down to how much you are willing to learn, versus just use a pre-packaged solution.


----------

