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.
Could someone please give a simply layperson's explanation of this thread? I last programmed a computer using Fortran 4 and punch cards about 40 years ago. Have you folks invented a new app that non programmers can use or do you have to be able to write code to do these things? Not even sure what these things are. I looked up several of the terms, like REST API, in wikipedia, but I can't make heads or tails of what they are.

Thanks from a senior citizen Model S owner.

Honestly, you do need to be a programmer to use these things. I worry that people who are not familiar with REST, and CURL, and Ruby, and JSON, etc. will try and take some of what has been created and get themselves into some trouble.

Having said that, since you are interested to know, here is my elevator pitch on "what this is".

You know FORTRAN so you understand programming and subroutines and the general flow of most programming languages. There are three major differences between the FORTRAN you used to use and most Internet programming today.

1) Programs are much more distributed with "server" bits running on one place (like in Tesla's datacenter) and "client" bits running in the Model S or the app on your iPhone or Android device.

2) Clients and Servers can be written in any programming language and talk to each other via standard internet protocols like HTTP (aka REST) and so I no longer care what programming language was used in the car (or in Telsa's datacenter), so long and I know the HTTP URL (like in your browser) and the right parameters to send to it, I can control my car using a JavaScript programs while others here choose to use Python, or Ruby, or Java, or any other modern programming language that has built in support for HTTP (aka REST).

3) Most communications today is not a client application directly calling a server application. Instead, most applications communicate via intermediaries called a proxy, or gateway, or broker. What we are doing so far in this thread is investigating the communication between the phone applications from Tesla and the server/gateway application that runs in Tesla's datacenter. The last mile from Tesla's datacenter to the actual car is not what we have been looking at (yet). So when we write our apps, we are pretenting to be a Tesla iPhone app and sending similar commands into Tesla's datacenter (not directly to the car). Since Tesla's servers cannot distinguish between my app and their own iPhone app, they pass the commands on to my car just the same.


iPhone ------HTTP/REST------Tesla Datacenter Servers-----????------Model S (running Linux)

My JavaScript App----HTTP/REST----Tesla Datacenter Servers-----????----Model S (running Linux)

The documentation others have put together so far details the HTTP/REST interface so we can all write our own fun scripts to honk the horn, open the sun roof, or more importantly to start charging at 9pm when the electricity rates are lower.

Some day we will be able to snoop on the traffic from Tesla Servers to the Model S.
Some day we will be able to get a Tesla Software Developer Kit (SDK) and write apps that run on the car in Linux, but not yet.
 
Last edited:
Has anyone noticed a high frequency of server error responses?

I see server unavailable error (503) a lot for mobile_enabled requests, frequent enough that I had to change what I was doing -- I was requiring that it returned true before using the rest of the API -- but that query fails so much that instead I'm going to assume the car is mobile enabled. All the other APIs return error if you try to use them on a non-mobile enabled car anyway...
 
Has anyone noticed a high frequency of server error responses?

Not a high frequency but I do see 503 once in a while. I suspect that Tesla needs to scale out the head end of this system.

On a totally unrelated note I am ready to share my Node.js JavaScript wrappers of the "REST" and "Streaming" interfaces. I put them on GitHub at:

hjespers/teslams GitHub

Now I am not a hard core JavaScript programmer so take it easy on me with your feedback (which is welcome). I would like to make this a proper node module. I would like for the functions to return values rather than just print them out on the console. However, I find this useful in it's existing raw state so I think others might as well.
 
Last edited:
Sorry, I know we covered the auth stuff a long time ago, but I just started playing with this. I am able to use /login to get back a cookie file, cookies.txt, with _s_portal_session set. Everything seems successful. But when I try to use it with:

curl -b cookies.txt http:// portal.vn.teslamotors.com/vehicles

Then all I get back from the server is a redirection to /login. Anyone have any quesses as to what I'm doing wrong?
 
Last edited:
Sorry, I know we covered the auth stuff a long time ago, but I just started playing with this. I am able to use /login to get back a cookie file, cookies.txt, with _s_portal_session set. Everything seems successful. But when I try to use it with:

curl -b cookies.txt http://portal.vn.teslamotors.com/vehicles

Then all I get back from the server is a redirection to /login. Anyone have any quesses as to what I'm doing wrong?

Try this URI instead (you're missing the 's'):
Code:
curl -b https://portal.vn.teslamotors.com/vehicles
 
Sorry, I know we covered the auth stuff a long time ago, but I just started playing with this. I am able to use /login to get back a cookie file, cookies.txt, with _s_portal_session set. Everything seems successful. But when I try to use it with:



Then all I get back from the server is a redirection to /login. Anyone have any quesses as to what I'm doing wrong?

https?
 
It looks like only _s_portal_session is set. I don't see any reference to user_credentials.

That is a sign that your login (username and password) are not being accepted as valid. I won't have my code in front of me till tonight to compare it to yours, but that is the section to look at (jar container, jar file, post data, site, etc).

Peter
 
I'm trying the curl commands out and it works fine, but I can't make it start charging. After sitting a couple of nights in the cold, with only one short errand, it's down from from the standard 241ish charge to 216. But trying this...

curl -b cookies.txt https://portal.vn.teslamotors.com/vehicles/702/command/charge_start

...gives me "reason":"charging","result":false}. So I check the charging state:

{"charging_state":"Complete","charge_to_max_range":false,"max_range_charge_counter":0,"fast_charger_present":false,"battery_range":216.39,"est_battery_range":171.15,"ideal_battery_range":249.06,"battery_level":91,"battery_current":-0.7,"charge_starting_range":null,"charge_starting_soc":null,"charger_voltage":0,"charger_pilot_current":40,"charger_actual_current":0,"charger_power":0,"time_to_full_charge":0.0,"charge_rate":-1.0,"charge_port_door_open":true}

That 91% seems to mean 91% of a standard charge, not a max charge (216 is not 91% of 265, but it is 91% of 241, which is what I usually get for a standard charge). I'd like to get the battery (a) back up to 241 and (b) warmed up before use so I get regen breaking.

I've read that I could toggle it to range mode (I don't want a full range charge) and then toggle it back, to trick it into charging, but that makes me a bit nervous; also, it just seems like this is not working right. If I go out and unplug it and plug it in again, it'll charge--I believe I did that a few days ago--I was just hoping the API would let me do that without going out into the cold. ;-)

Any idea why telling it to charge doesn't work? Is this just me? (Side Note: I'm disappointed it doesn't do this on its own; how long must it sit before it finally starts charging on its own?) Amusingly, telling it to stop charging returns "true"--which IMHO should return "false" with a reason of "not charging," but whatever.
 
I'm trying the curl commands out and it works fine, but I can't make it start charging.
After sitting a couple of nights in the cold, with only one short errand, it's down from from the standard 241ish charge to 216.
But trying this...

curl -b cookies.txt Portal

...gives me "reason":"charging","result":false}.

So I check the charging state:

{"charging_state":"Complete", [...] "charge_rate":-1.0,"charge_port_door_open":true}

Once the car completes a charge, there is no way (that I know of) to get it to start charging again other than physically unplugging and plugging it in again. I tried to switch to max_range from standard and every combination of stop_charge and start_charge to get mine to top off after a complete standard charge but it wouldn't go.

I think the car is drawing power for maintaining the battery (charge_rate is -1.0) so perhaps it cannot distinguish between charging and charge_complete.

The only work around I can think of is to monitor SOC and stop charging before it completes. Then you can start charging again later if needed.
 
Once the car completes a charge, there is no way (that I know of) to get it to start charging again other than physically unplugging and plugging it in again. I tried to switch to max_range from standard and every combination of stop_charge and start_charge to get mine to top off after a complete standard charge but it wouldn't go.

I think the car is drawing power for maintaining the battery (charge_rate is -1.0) so perhaps it cannot distinguish between charging and charge_complete.

The only work around I can think of is to monitor SOC and stop charging before it completes. Then you can start charging again later if needed.

That's a drag. Thanks, hans!
 
Once the car completes a charge, there is no way (that I know of) to get it to start charging again other than physically unplugging and plugging it in again.
Try toggling some seemingly unrelated things like climate control, range mode, units (miles <-> km), and active driver profile. Or whichever of those you can change.

Maybe something will trigger what you want.

Sidenote: I don't recommend you toggle the 3G off. ;)
 
Try toggling some seemingly unrelated things like climate control, range mode, units (miles <-> km), and active driver profile. Or whichever of those you can change.

Maybe something will trigger what you want.

Sidenote: I don't recommend you toggle the 3G off. ;)

Are you guessing or do you know something?

I tried toggling range mode, no dice. There is no way to turn off 3G in the API specs we know of so far.
 
I know it's a stretch but does anyone have traces of an app session with an account that has multiple vehicles?

I'm working on some ruby code that calls the API and the account I'm working with has multiple vehicles on it. I am able to authenticate OK and get the _s_portal_session and user_credentials cookies.

When I use the session and credential cookie I'm able to make a subsequent call to /vehicles just fine and get the JSON vehicle roster back.

However, if instead of calling /vehicles I change the url to make a command call for a specific vehicle such as /vehicles/my_vehicle_id/commands/charge_state passing the cookies just as I did for /vehicles I get redirected to the sign in.

Same results when using curl. I'm wondering if I need to pass user_id in somehow when there are multiple vehicles.

Thanks