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.
This is what I'm using in VB:
Pano Roof:
https://owner-api.teslamotors.com/api/1/vehicles/" & CarID & "/command/sun_roof_control?state=open?vehicle_id=" & CarID

AC Temps:
https://owner-api.teslamotors.com/api/1/vehicles/" & CarID & "/command/set_temps?passenger_temp=27&driver_temp=25?vehicle_id=" & CarID

Custom Charge Limit:
https://owner-api.teslamotors.com/api/1/vehicles/" & CarID & "/command/set_charge_limit?percent=80?vehicle_id=" & CarID

All other commands work so my syntax must be wrong.
 
If the app can't log in, the API won't let you in. They use the same mechanism.
I see that I wasn't specific. The app that won't let me login is the Visiible Tesla app--it says to check user id and password. I verified that my code and the Visible Tesla app both go to the same IP address. The Official iPhone Tesla app lets me login, but says "no vehicles attached." I don't have a packet sniffer for the iPhone.
 
This is what I'm using in VB:
Pano Roof:
https://owner-api.teslamotors.com/api/1/vehicles/" & CarID & "/command/sun_roof_control?state=open?vehicle_id=" & CarID

AC Temps:
https://owner-api.teslamotors.com/api/1/vehicles/" & CarID & "/command/set_temps?passenger_temp=27&driver_temp=25?vehicle_id=" & CarID

Custom Charge Limit:
https://owner-api.teslamotors.com/api/1/vehicles/" & CarID & "/command/set_charge_limit?percent=80?vehicle_id=" & CarID

All other commands work so my syntax must be wrong.

Commands sun roof control, set temps, and set charge limit don't use query parameters, they use a JSON body for each parameter.
 
So I've added in state and percent as headers in my call with no luck. If someone could provide an example that would be great. (No cURL please only VB)
JSON-formatted parameters in the body, not the header. We can't provide a VB sample without knowing what web library you're using. If you're struggling despite knowing this information, we won't be of much help without writing it all for you. Look for a 'setBody' method and assign the JSON serialized string of your parameters.
 
JSON-formatted parameters in the body, not the header. We can't provide a VB sample without knowing what web library you're using. If you're struggling despite knowing this information, we won't be of much help without writing it all for you. Look for a 'setBody' method and assign the JSON serialized string of your parameters.
Ok, I'm using system.net as my library with http.webrequest.create to make my call

Dim request = TryCast(System.Net.WebRequest.Create("https://owner-api.teslamotors.com/api/1/vehicles/" & CarID & "/command/sun_roof_control"), System.Net.HttpWebRequest)

request.Method = "POST"

request.Headers.Add("authorization", "Bearer " + AccessToken)

request.ContentLength = 0
Dim responseContent As String
Using response = TryCast(request.GetResponse(), System.Net.HttpWebResponse)
Using reader = New System.IO.StreamReader(response.GetResponseStream())
responseContent = reader.ReadToEnd()
MessageBox.Show(responseContent)
End Using
End Using

request.SetBody is not a member of httpwebrequest so I'm unsure as to where to include my body.
I know my way around VB but never until now worked with APIs.
 
I'm by no means an expert in coding web interfaces in .net, but I think you need the HttpWebRequest.GetRequestStream method.
  • put the content of your request body in a string variable
  • set the content type with HttpWebRequest.ContentType = "application/x-www-form-urlencoded"
  • set the content length with HttpWebRequest.ContentLength
  • define a stream with HttpWebRequest.GetRequestStream()
  • write your body variable to the stream with Stream.Write()
  • close the stream
see: HttpWebRequest.GetRequestStream Method (System.Net)

however, if you're handling a lot of JSON, then you may find it easier to use a JSON framework, such as GitHub - JamesNK/Newtonsoft.Json: Json.NET is a popular high-performance JSON framework for .NET

But this is me just googling a bit ... I'm not a .net coder.
 
I'm by no means an expert in coding web interfaces in .net, but I think you need the HttpWebRequest.GetRequestStream method.
  • put the content of your request body in a string variable
  • set the content type with HttpWebRequest.ContentType = "application/x-www-form-urlencoded"
  • set the content length with HttpWebRequest.ContentLength
  • define a stream with HttpWebRequest.GetRequestStream()
  • write your body variable to the stream with Stream.Write()
  • close the stream
see: HttpWebRequest.GetRequestStream Method (System.Net)

however, if you're handling a lot of JSON, then you may find it easier to use a JSON framework, such as GitHub - JamesNK/Newtonsoft.Json: Json.NET is a popular high-performance JSON framework for .NET

But this is me just googling a bit ... I'm not a .net coder.
THANK YOU!

Your help is invaluable. Using your brief description of how to do it with some trial and error I had success.

For anybody who wants to know here is the code I used:
Dim encoding As New ASCIIEncoding()
Dim Bytes As Byte() = encoding.GetBytes(BodyText)


request.ContentType = "application/x-www-form-urlencoded"

request.ContentLength = Bytes.Length

Dim Stream = request.GetRequestStream()
Stream.Write(Bytes, 0, Bytes.Length)
Stream.Close()