Authentication
The authentication process for the Tesla API
Tesla uses a separate SSO service (auth.tesla.com) for authentication across their app and website. This service is designed around a browser-based flow using OAuth 2.0, but also appears to have support for Open ID Connect. This supports both obtaining an access token and refreshing it as it expires.
Tesla's SSO service has a WAF (web application firewall) that may temporarily block you if you make repeated, execessive requests. This is to prevent bots from attacking the service, either as a brute force or denial-of-service attack. This normally presents as a "challenge" page, which requires running some non-trivial JavaScript code to validate that you have a full browser engine available. While you can potentially fully evaluate this page to remove the block, the best practice for now is to reduce your calls to the SSO service to a minimum and avoid things like automatic request retries. The service also expects TLS 1.2 or lower connections, so avoid connecting with TLS 1.3.
Logging in
Step 1: Obtain the login page
Subsequent requests to the SSO service will require a "code verifier" and "code challenge". These are a random 86-character alphanumeric string and its SHA-256 hash encoded in URL-safe base64 (base64url). Here is an example of generating them in Ruby, but you can apply this same process to other languages.
You will also need a stable state
value for requests, which is a random string of any length.
Avoid setting a User-Agent
header that looks like a browser (such as Chrome or Safari). The SSO service has protections in place that will require executing JavaScript if a browser-like user agent is detected.
GET https://auth.tesla.com/oauth2/v3/authorize
https://auth.tesla.com/oauth2/v3/authorize
The first request returns HTML intended for display in the browser. You will need to parse this HTML for hidden input fields.
The request is made with a redirect_url
of "https://auth.tesla.com/void/callback", which is a non-existent page. The Tesla app intercepts the request to this page to capture the authorization code.
Request parameters
Response
This returns an HTML response body. There will be a <form>
with hidden <input>
elements that contain session-based information to prevent CSRF attacks. At the moment, they appear to be _csrf
, _phase
, _process
, transaction_id
, and cancel
, but they may change due to server-side changes by Tesla. These must be provided in the POST body to validate the following request.
The response will also include a set-cookie
header that includes a session ID cookie. This should be provided to the following request as a Cookie
header so that the SSO service can match up your request with private data it has in that session.
When the optional login_hint
parameter is supplied with the GET
request and the email is registered with a Tesla SSO service in another region this will respond with a 303 HTTP response code (See Other), which will redirect you to the Tesla SSO service in that region (e.g. auth.tesla.cn). Should this redirect happen you should continue using the region specific Tesla SSO host name in all subsequent steps. Easy way to test this is to use auth.tesla.cn
with login_hint
using an email registered under auth.tesla.com
.
Step 2: Obtain an authorization code
This will simulate a user submitting the form from the previous request in their browser. Ensure that the hidden <input>
s are provided as POST body parameters and the Cookie
header is set.
POST https://auth.tesla.com/oauth2/v3/authorize
https://auth.tesla.com/oauth2/v3/authorize
Request parameters
Note: These are query parameters, not part of the POST body
Note: This is the contents of the POST body. These should be form encoded (
application/x-www-form-urlencoded
).
Response
This will respond with a 302 HTTP response code, which will attempt to redirect to the redirect_uri with additional query parameters added. This new URL is located in the location
header. You should not follow it, as it is non-existent. Instead, you should parse this URL and extract the code
query parameter, which is your authorization code.
Step 3: Exchange authorization code for bearer token
POST https://auth.tesla.com/oauth2/v3/token
https://auth.tesla.com/oauth2/v3/token
This is a standard OAuth 2.0 Authorization Code exchange. This endpoint uses JSON for the request and response bodies.
Request parameters
Response
The response varies. If the user has no MFA enabled, the response will be:
However, if the user has MFA enabled the response will be an HTML document with a passcode
field inside it. This is for the TOTP (Time-based-One-time Password).
To authenticate, you first need to get the list of known factors on the account, by requesting the /authorize/mfa/factors
endpoint via GET. You also need to supply the transaction_id
from the /authorize
endpoint with this request.
After doing this you need to send a POST request to the /authorize/mfa/verify
endpoint with the transaction_id
, factor_id
and the current TOTP passcode
. Similar to step 1, you also need to pass an additional _csrf
field to the /authorize/mfa/verify
endpoint. However, this is a different one from the first request, and has to fetched and parsed from the response of the /authorize
endpoint that you hit earlier (the same response that had a passcode
field inside of it).
This will validate the current transaction_id
and allow for the previous request to POST successfully with the transaction_id
as a form body parameter.
Making requests
Requests are made using the access_token
provided with the response. It is treated as an OAuth 2.0 Bearer Token and expires every eight hours. This token is passed along in an Authorization header with all future requests:
Refreshing an access token
POST https://auth.tesla.com/oauth2/v3/token
https://auth.tesla.com/oauth2/v3/token
This uses the SSO refresh_token
from Step 3 above to do an OAuth 2.0 Refresh Token Grant. The refreshed access token is to be used directly with the Owner API as a bearer token as per the above Making requests section.
This endpoint uses JSON for the request and response bodies.
Request parameters
Response
Last updated