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.
@hans Yes, sorry and thank you for clarifying that point. What I've found however is that if you have a scheduled task, rather than a long-lived app/process, for data-logging you must login each time because the token is not exposed via the API. For my purposes that is less desirable than re-using the OAuth token. I see from recent discussions on teslams that similar changes are being considered.
 
Is this documentation still up to date? Tesla Model S JSON API · Apiary

Reading all parameters works just fine, but I can't get any of the commands to work. I get just a "404" back.

If I omit the "curl_setopt($ch, CURLOPT_POST, TRUE);" (which is the only difference to reading data) I seem to get an error back right from the firewall (graphical html 404 error message).

The api documentation also lists not all the parameters I get back. Is there an updated api documentation somewhere?
 
Good news, everyone!

I finally updated the API docs for the new oAuth API and some of the new endpoints: Tesla Model S JSON API · Apiary

If anyone has any ideas about the upcoming_calendar_entries, notification_preferences, and vehicle_subscriptions endpoints (or any that I've missed), I can get those added too.

Bumping this up as it would be nice to see these documented. To that list I would add trigger_homelink and autopark_request
 
The response parameters are nowhere near complete. I get about twice as many fields back from charge-state than are in the documentation. But that is not a problem. My problem is, that I cannot issue any commands. I always get "404" as an answer. Where should I start looking? I copied the php exampe 1:1. Reading is not a problem and works fine.
 
jsgoecke:

This code works and I get the json array back:

Code:
<?php

include_once("settings.php");
include_once("db_connect.php");
include_once("functions.php");


$ch = curl_init();

$token = getAccessToken();


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://owner-api.teslamotors.com/api/1/vehicles/".$VEHICLE_ID."/data_request/vehicle_state");
//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);
curl_close($ch);

var_dump($response);

This one gives me a "404" back: (just switched the two curl URL lines and uncommented the CURLOPT_POST line)

Code:
<?php

include_once("settings.php");
include_once("db_connect.php");
include_once("functions.php");


$ch = curl_init();

$token = getAccessToken();


$ch = curl_init();

//curl_setopt($ch, CURLOPT_URL, "https://owner-api.teslamotors.com/api/1/vehicles/".$VEHICLE_ID."/data_request/vehicle_state");
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);
curl_close($ch);

var_dump($response);


And if I omit the
Code:
curl_setopt($ch, CURLOPT_POST, TRUE);
the firewall gives me a html 404 page.

Thanks for helping :)
 
I've been playing with PowerShell on my PC to test out the capabilities of the API and I've been able to do most functions, lock/unlock, get data/state/etc, activate homelink, etc. However, trying to open the sunroof seems to not like any states, Here's my code (minus the code to get the token)

Code:
$headers = @{'Authorization' = "Bearer $($token.access_token)" }
$restResponse = Invoke-RestMethod -Uri "https://owner-api.teslamotors.com/api/1/vehicles/$($vehicle.id)/command/sun_roof_control?state=move&percent=50" -Method Post -Headers $headers

I've seen mixed replies as to what the command is, the APIary says to use:

($state being, vent, open, close, comfort)

Code:
state=$state=$state&percent=$percent
Or
Code:
state=$state

Then I've seen other people use
Code:
state=$state=move&percent=$percent

But to no avail they all return with
Code:
unknown state requested
has this functionality been removed from the API?
 
Sorry to ask again, but I am stuck.

Why does that not work? It is copied from the API docs php sample:

Code:
$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);
curl_close($ch);

var_dump($response);

I just get a 404 back. All the request-type functions work, but all the command ones don't.