Welcome to Tesla Motors Club
Discuss Tesla's Model S, Model 3, Model X, Model Y, Cybertruck, Roadster and More.
Register

Model S REST API

This site may earn commission on affiliate links.
That's a Unix timestamp. How you parse it depends on the language. The only thing that could trip you up when working with it is that this particular Unix timestamp is in seconds, and some date libraries expect milliseconds, so you would need to multiple it by 1,000.

If you pop open your JavaScript Console in your browser's developer tools and type...

Code:
new Date(1463347242 * 1000) // The created_at timestamp quoted above multiple by 1,000

...it will return a human-readable date string corresponding to that Unix timestamp.

The timestamp above gave me this: "Mon May 16 2016 06:20:42 GMT+0900 (KST)". Note that this is Korean Standard Time, so you will probably get a half-day earlier or so.

Well you learn something new every day, that worked like a charm thanks for the info.


On a side note had anyone noticed that when you set the temp for either driver or the passenger both zones are set? I take it this was another change by Tesla?
 
Have any of you guys ran into issues with Javascript turning the id into an exponential? I'm using tasker to automate some things and the id is always turned into an exponential so it becomes invalid.

I posted it to SO:
Scientific Notation in JavaScript

The id is too big for JavaScript's int data type. You need to either use the "id_s" string version of the id or use a big integer handling library like JSON-bigint. See node.js is there any proper way to parse JSON with large numbers? (long, bigint, int64)
 
  • Informative
Reactions: L-P-G
@Prefect - are you still stuck? I use PHP in my Tesla app and may be of some assistance.

Hello apacheguy! That would be great if you could give me a tip!

This is my test-code, and it responds with 404:

Code:
<?php

$token = "fdf3d02....553c798909c7e4";
$VEHICLE_ID = "719.....506";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://owner-api.teslamotors.com/api/1/vehicles/".$VEHICLE_ID."/command/honk_horn");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  "Authorization: Bearer $token"
));

$response = curl_exec($ch);

var_dump($response);

The same code, without the CURLOPT_POST option works fine for all the request, but fails for all commands. Do I have to set any CURLOPT_POSTFIELDS ?

Any hints? Thanks a lot!!
 
@Prefect - In your header, you MUST specify application/JSON.

OK, did that:

Code:
<?php

$token = "fdf.....e4";
$VEHICLE_ID = "7....6";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://owner-api.teslamotors.com/api/1/vehicles/".$VEHICLE_ID."/command/honk_horn");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  "Content-Type: application/json",
  "Authorization: Bearer $token"
   
));

$response = curl_exec($ch);

var_dump($response);

Nothing changed :)
 
One trick you can do, is use TCPdump or WireShark to sniff the packets of a working implementation. Before there was any documentation, this is how I reversed the protocol. Then, compare the output to your code. Change your code until it's identical to the output of the working implementation. In my case, I used Tesla's app, but now you can use PC based apps that are easier to monitor, such as VisibleTesla.
 
VisibleTesla is broken, it does not work correctly anymore. And isn't the iPhone-App encrypted? Even if it sends the data in plain text (which I really hope it does not) I can't sniff it from my wifi router to the wired PC I guess.

The PHP code I use is copied from this page: Tesla Model S JSON API · Apiary
I don't see why it should not work? Very strange..

You would need to use something like this in order to view the traffic from your iPhone. Note that Apple has really stepped up certificate checking so this may not even work anymore as of iOS 9. I believe apps are now allowed to have some kind of whitelist of acceptable certificates (certificate pinning), of which a self-signed certificate, even a non-malicious one, used to MITM, would fail.

You can read more about MITMProxy and AppTransportSecurity on this GitHub issue.

I don't own a Tesla (yet; my Model S order will be finished in September), or else I'd already be working on a replacement for VisibleTesla.
 
Last edited:
  • Like
Reactions: Prefect
@Prefect - how do you treat the vehicle id? In my code I had to set JSON store big int as string in order for it to work.

It is a normal variable in PHP, so it should not be length limited. It also works for all the other requests against the Tesla server. It just fails if I want to send a command. Hmm..

Could someone with a PHP server try the script I posted (which is the same as the example in the API)? That would help a lot.

Thank you!
Chris