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.
@HankLloydRight

This is my most simplified implementation in PHP to use the Tesla API

.... tried it with "quote", it works, but without indentation:

<?php


class SimpleTeslaAPI {


// Quick&Dirty Tesla API
// 9.1.2016 (V1.1)
// von [email protected]


var $token;
var $vehicleID;


// Implementierung
function SimpleTeslaAPI() {
// !!!!!!!!! set in token and vehicle_id (example out of mobile app "Remote S" !!!!!!!!!
$this->token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$this->vehicleID = "xxxxxxxxxx";
}


private function curlexec($command,$mode="GET",$params=array()) {
$url = 'https://owner-api.teslamotors.com/';


$ch = curl_init();
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);


$params['vehicle_id']=$this->vehicleID;


if ( $mode == "POST" ) {
if ( $command == "oauth/token" ) {
curl_setopt($ch,CURLOPT_URL, $url.$command);
} else {
if ( $command == "wake_up" ) {
curl_setopt($ch,CURLOPT_URL, $url."api/1/vehicles/".$this->vehicleID."/wake_up");
} else {
curl_setopt($ch,CURLOPT_URL, $url."api/1/vehicles/".$this->vehicleID."/command/".$command);
}
curl_setopt($ch,CURLOPT_HTTPHEADER, array("Authorization:Bearer ".$this->token) );
}
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $params);
} else {
if ( $command == "vehicles" ) {
curl_setopt($ch,CURLOPT_URL, $url."api/1/vehicles");
} else {
curl_setopt($ch,CURLOPT_URL, $url."api/1/vehicles/".$this->vehicleID."/data_request/".$command."?".join("&",$params));
}
curl_setopt($ch,CURLOPT_HTTPHEADER, array("Authorization:Bearer ".$this->token) );
}
$result = curl_exec($ch);
$rc=curl_getinfo($ch,CURLINFO_HTTP_CODE);


curl_close($ch);


return json_decode($result);
}


//base functions
function vehicles() {
return $this->curlexec("vehicles","GET");
}
function wake_up() {
return $this->curlexec("wake_up","POST");
}


//data-requests
function charge_state() {
return $this->curlexec("charge_state","GET");
}
function climate_state() {
return $this->curlexec("climate_state","GET");
}
function drive_state() {
return $this->curlexec("drive_state","GET");
}
function gui_settings() {
return $this->curlexec("gui_settings","GET");
}
function vehicle_state() {
return $this->curlexec("vehicle_state","GET");
}




// commands
function charge_port_door_open() {
return $this->curlexec("charge_port_door_open","POST");
}
function charge_standard() {
return $this->curlexec("charge_standard","POST");
}
function charge_max_range() {
return $this->curlexec("charge_max_range","POST");
}
function set_charge_limit($percent) {
return $this->curlexec("set_charge_limit","POST",array("percent" => $percent ));
}
function charge_start() {
return $this->curlexec("charge_start","POST");
}
function charge_stop() {
return $this->curlexec("charge_stop","POST");
}
function flash_lights() {
return $this->curlexec("flash_lights","POST");
}
function honk_horn() {
return $this->curlexec("honk_horn","POST");
}
function door_unlock() {
return $this->curlexec("door_unlock","POST");
}
function door_lock() {
return $this->curlexec("door_lock","POST");
}
function set_temps($tempDriver, $tempPassenger) {
return $this->curlexec("set_temps","POST",array("driver_temp" => $tempDriver,"passenger_temp" => $tempPassenger ));
}
function auto_conditioning_start() {
return $this->curlexec("auto_conditioning_start","POST");
}
function auto_conditioning_stop() {
return $this->curlexec("auto_conditioning_stop","POST");
}
function sun_roof_control_state($state) {
return $this->curlexec("sun_roof_control","POST",array("state" => $state ));
}
function sun_roof_control_percent($percent) {
return $this->curlexec("sun_roof_control","POST",array("state" => "move", "percent" => $percent ));
}
function remote_start_drive($password) {
return $this->curlexec("remote_start_drive","POST",array("password" => $password ));
}


// deprecated
function trunk_open() {
return $this->curlexec("trunk_open","POST",array("which_trunk" => "rear" ));
}
}




// Main


$tesla = new SimpleTeslaAPI();


print_r($tesla->honk_horn());
 
Last edited:
That's awesome, thanks! I'm going to try to integrate this with my LogMySc web app so it automatically pulls the information needed at each stop/start event instead of requiring the user to key it in each time.

Since the PHP code needs the token and vehicleID and doesn't use the MyTesla login, I'm also thinking writing a small Javascript app to do that part so the login credentials are never sent to my server, only the token is.

Now if Tesla would replace the crappy browser in the Model S, I'd be all set.
 
Single button press to turn on climate control

Has anyone developed or is willing to develop a simple web site that just turns on the climate control when one goes to that site. That allows one to put a bookmark icon on the home screen and turn on the climate control with a single button press. Someone did this for the LEAF and it avoids having to wait for the Nissan app to start up and log n. While the Tesla app is faster than Nissan's, it would still be nice to have this most used feature available with a single button press and no waiting. For the LEAF, the person who did it was willing to put the script on his own server if you were willing to trust him with your login credentials or he gave clear directions about how to install it on your own server. If this already exists, I'd appreciate a pointer to it. If someone is willing to write it, I can find amd give a pointer to what was done for the LEAF, if that would help.
 
Has anyone developed or is willing to develop a simple web site that just turns on the climate control when one goes to that site. That allows one to put a bookmark icon on the home screen and turn on the climate control with a single button press. Someone did this for the LEAF and it avoids having to wait for the Nissan app to start up and log n. While the Tesla app is faster than Nissan's, it would still be nice to have this most used feature available with a single button press and no waiting. For the LEAF, the person who did it was willing to put the script on his own server if you were willing to trust him with your login credentials or he gave clear directions about how to install it on your own server. If this already exists, I'd appreciate a pointer to it. If someone is willing to write it, I can find amd give a pointer to what was done for the LEAF, if that would help.

Hmm.. with the Tesla native app, it's just three taps to turn on the climate control.

Plenty of us could write an app to do that with one tap, but is it worth it? Not sure I have the free time to write a simple app like that, manage it, distribute it, just so people can save two taps. And who would pay for such an app? Also, doesn't Remote S already do something like this with its custom buttons?
 
Has anyone developed or is willing to develop a simple web site that just turns on the climate control when one goes to that site. That allows one to put a bookmark icon on the home screen and turn on the climate control with a single button press. Someone did this for the LEAF and it avoids having to wait for the Nissan app to start up and log n. While the Tesla app is faster than Nissan's, it would still be nice to have this most used feature available with a single button press and no waiting. For the LEAF, the person who did it was willing to put the script on his own server if you were willing to trust him with your login credentials or he gave clear directions about how to install it on your own server. If this already exists, I'd appreciate a pointer to it. If someone is willing to write it, I can find amd give a pointer to what was done for the LEAF, if that would help.

If you're willing to press two buttons, the Remote S app for iOS will turn on the Climate control faster than the Tesla app, because as soon as the app opens, you can press the climate control button and then exit out the app. The app will send you a notification if the command fails, but if it doesn't, it will stay silent.

In a future version, I plan on allowing specialized URLs to control the app. So a URL like remotes://startclimate would turn on the climate control. This is also safer security-wise, because your password is never passed onto any non-Tesla servers, and the app is self-contained. It would also allow you to Bookmark the specialized URL like you requested and add a homescreen button for climate control. If you have an iPhone 6S, I plan on adding 3D Force Touch options onto the app to allow turn on Climate Control and other options using the Force Touch menu on the app icon.
 
I'm an Android user or I would be using remoteS right now.
It is less the extra button presses than the time spent waiting for the Tesla app to connect to the server.
Allen, if you keep improving remoteS, especially if you add voice control to summon my car, I will be FORCED to get an IPhone. I'd rather not get the iWatch though, so hope you make that work with just the phone. It would be way cool.
 
Has anyone developed or is willing to develop a simple web site that just turns on the climate control when one goes to that site. That allows one to put a bookmark icon on the home screen and turn on the climate control with a single button press. Someone did this for the LEAF and it avoids having to wait for the Nissan app to start up and log n. While the Tesla app is faster than Nissan's, it would still be nice to have this most used feature available with a single button press and no waiting. For the LEAF, the person who did it was willing to put the script on his own server if you were willing to trust him with your login credentials or he gave clear directions about how to install it on your own server. If this already exists, I'd appreciate a pointer to it. If someone is willing to write it, I can find amd give a pointer to what was done for the LEAF, if that would help.

I wrote a simple app called "restla" that creates a local webpage (only for your car) and creates a link for every one of the REST commands. These links can be bookmarked for quick access without having to go to the full menu page which is shown below. You would have to install and configure the app on your laptop and keep it continually running as I have no interest in running this as a service for others. I also wrote a command line "teslacmd" that you can run in a terminal window to turn climate control on and off and another called "climatemon" that monitors the temperature for you in real time. The apps are all written in Javascript so they work on Mac, Windows, or Linux. PM me if you are interested in trying it out. Takes a little bit of computer skills to install and configure (command prompt and text editing skills) but you don't have to be a programmer or system administrator. I could modify the app to make it look like an actual button to press rather than a simple html link to click but that sounds like what you want anyway.

Screen Shot 2016-01-11 at 12.30.00 PM.png
 
I wrote a simple app called "restla" that creates a local webpage (only for your car) and creates a link for every one of the REST commands. These links can be bookmarked for quick access without having to go to the full menu page which is shown below. You would have to install and configure the app on your laptop and keep it continually running as I have no interest in running this as a service for others. I also wrote a command line "teslacmd" that you can run in a terminal window to turn climate control on and off and another called "climatemon" that monitors the temperature for you in real time. The apps are all written in Javascript so they work on Mac, Windows, or Linux. PM me if you are interested in trying it out. Takes a little bit of computer skills to install and configure (command prompt and text editing skills) but you don't have to be a programmer or system administrator. I could modify the app to make it look like an actual button to press rather than a simple html link to click but that sounds like what you want anyway.

View attachment 107512
This sounds like exactly what I am looking for. I'm fairly computer literate. Please post instructions. Probably best to do it here in this thread as others may be interested.
Thanks.
 
This sounds like exactly what I am looking for. I'm fairly computer literate. Please post instructions. Probably best to do it here in this thread as others may be interested.
Thanks.

It's all part of the package of open source software called "TeslaMS". I put it up on github at hjespers/teslams · GitHub
There is another thread on the forum for this software at TeslaMS tools for telemetry data visualization so post questions there.

You will need to install Node.js first from https://nodejs.org (I recommend the LTS "Long Term Support" version).
Once NodeJS is installed start a terminal window and run "npm install -g teslams" or on a Mac or Linux run "sudo npm install -g teslams". Sudo will prompt you for your root/admin password. This is only ever used once for the install.

Once installed you will be able to run "restla -u <username> -p <password>" with your TeslaMotors username (your email) and password.

This should start a web server which you can access at http://localhost:8766

After that the only OS-dependent trick is to make sure the restla app always starts when you reboot or when you login. Also you can setup a config file so that you don't have to specify username and password every time as command line options.
 
It's all part of the package of open source software called "TeslaMS". I put it up on github at hjespers/teslams · GitHub
There is another thread on the forum for this software at TeslaMS tools for telemetry data visualization so post questions there.

You will need to install Node.js first from https://nodejs.org (I recommend the LTS "Long Term Support" version).
Once NodeJS is installed start a terminal window and run "npm install -g teslams" or on a Mac or Linux run "sudo npm install -g teslams". Sudo will prompt you for your root/admin password. This is only ever used once for the install.

Once installed you will be able to run "restla -u <username> -p <password>" with your TeslaMotors username (your email) and password.

This should start a web server which you can access at http://localhost:8766

After that the only OS-dependent trick is to make sure the restla app always starts when you reboot or when you login. Also you can setup a config file so that you don't have to specify username and password every time as command line options.
Thanks. I will give it a try.
 
Has anyone developed or is willing to develop a simple web site that just turns on the climate control when one goes to that site.
If you have a computer that could run the Apache2 web server, and that includes a $5 Rapsberry Pi Zero, then this is pretty easy to set up and I will give you some code to do this in Python. You just create a Python script put it in your /usr/lib/cgi-bin folder and hit the URL http://myserver/cgi-bin/climeon.py . And you can learn a bit about how to easily write scripts that you can execute from pretty much any web browser.
 
If you have a computer that could run the Apache2 web server, and that includes a $5 Rapsberry Pi Zero, then this is pretty easy to set up and I will give you some code to do this in Python. You just create a Python script put it in your /usr/lib/cgi-bin folder and hit the URL http://myserver/cgi-bin/climeon.py . And you can learn a bit about how to easily write scripts that you can execute from pretty much any web browser.
I think I can manage that if you will give me the code. Is there a security issue in that the script will presumably have to have my account and password in it? I'm not worried about someone turning on my climate control, but if they can unlock my car via this website, I would be worried.
 
I think I can manage that if you will give me the code. Is there a security issue in that the script will presumably have to have my account and password in it? I'm not worried about someone turning on my climate control, but if they can unlock my car via this website, I would be worried.

The ability to do anything with the API is predicated upon having a security token. You get a security token by sending username and password. Once you have that security token, you can do anything the mobile app can - so yes, someone with access to that site could indeed unlock your car.

That said, if you follow good security practices with configuring and maintaining the web site, you're unlikely to be exposed too much. Certainly not any more than having the mobile app in the first place. If you have that concern, then you may want to just disable mobile access altogether.
 
The ability to do anything with the API is predicated upon having a security token. You get a security token by sending username and password. Once you have that security token, you can do anything the mobile app can - so yes, someone with access to that site could indeed unlock your car.

That said, if you follow good security practices with configuring and maintaining the web site, you're unlikely to be exposed too much. Certainly not any more than having the mobile app in the first place. If you have that concern, then you may want to just disable mobile access altogether.
By access to the site, do you mean just knowing the URL or do you mean ability to log on to the server. Only the former worries me.