- Tesla 2024 API implementation in Python.
- This is a library.
- See the example implementation in tesla_api_example
- Implements the 2024 interface including authentication and token handling
- It is a part of the ElectronFluxBalancer project
- Supports developer account registration
- Supports customer registration and key install on car
- Gets vehicle data as python structures
- Uses Tesla Vehicle Commands for signed commands.
- include into your project as a library / git submodule in
lib/tesla_api/
- use
bash git submodule update --remote
to update - For best usage, please read the comments in the main file
tesla_api_2024.py
at the beginning and at the end of the file. - Example implementation in tesla_api_example
- Maybe you have a look at the implementation at ElectronFluxBalancer
- For sending commands, you need to get and build tesla-control, see below and the readme in the subdirectory
tesla-control
-
Set up a third-party account at Tesla Developer
- Visit Tesla Developer Portal to set up an account.
- Read the registration process there!
-
Complete registration of your account
- Generate a public/private key pair for Tesla Vehicle Commands using the
secp256r1
curve:openssl ecparam -name prime256v1 -genkey -noout -out privatekey.pem
- Extract the corresponding public key and save it in PEM format:
openssl ec -in privatekey.pem -pubout -out publickey.pem
- Host your public key in the
/.well-known
section of your website. - Store the public key at
https://your.domain/.well-known/appspecific/com.tesla.3p.public-key.pem
- Store both keys in the
TeslaKeys
directory
- Generate a public/private key pair for Tesla Vehicle Commands using the
-
Now within your python implementation
- Generate a partner authentication token:
partner_token = tesla_get_partner_auth_token(config.tesla_client_id, config.tesla_client_secret, AUDIENCE)
- Make a POST call to
/api/1/partner_accounts
with your partner authentication token:import config _r = tesla_register_partner_account(partner_token, config.tesla_audience) # call once per audience you want to register for. print("account registration", _r)
Now your partner registration should be completed. Check with:
python _r = tesla_partner_check_public_key(partner_token, audience)
- Generate a partner authentication token:
-
Request authorization permissions from a customer
- Generate a third-party token on their behalf:
tesla_register_customer(myTesla) # register new customer (customer must log in and enter code here!)
- Generate a third-party token on their behalf:
-
Customer must allow key on vehicle(s)
tesla_register_customer_key()
- Install Go environment on your PC
- Download Tesla Vehicle Commands source
- Read the build instructions there
- In the downloaded directory go to
cd vehicle-command/cmd/tesla-control/
- Build for PC (testing)
This generates an elf file
go build .
tesla_control
, which can be directly executed on the PC. - Cross-compile on the PC for Raspberry Pi 2/3 zero2W
This generates an elf file
env GOOS=linux GOARCH=arm GOARM=7 go build . # one line!
tesla_control
, which can be directly executed on the Pi. - Put this file into the lib/tesla_api/vehicle_command/ directory of your project on the Pi.
- Grant BLE permission:
sudo setcap 'cap_net_admin=eip' "$(which ./tesla-control)"
Setup for BLE commands not influenced by the Tesla API rate limit:
-
You have to generate a new key pair - the fleet keys will NOT work.
Maybe you have to compile tesla-keygen first - do as described for the tesla-command.
./tesla-keygen -key-file ./relay_priv.pem create > ./relay.pem
will generate two files - public and private
relay.pem
relay_priv.pem
for local operation put the private key to: ./lib/tesla_api_ble/TeslaKeys/BLEprivatekey.pem
-
optional: copy both keys to the remote machine Setup of password-less SSH should be done at this point.
scp relay*.pem [email protected]:~
-
add the public! key to the vehicle
Sends add-key request to your car.
Confirm by tapping NFC card on center console.
./tesla-control -ble -vin LRWYGCEK9NC999999 add-key-request ./relay.pem owner cloud_key
-
Send commands Now you cen test the setup:
./tesla-control -ble -debug -vin LRWYGCEK9NC999999 -key-file ./relay_priv.pem honk
You will hear, when it works.
-
optional: Execute remotely:
ssh -p 2222 [email protected] 'da command'
-
edit config.py
tesla_ble = True # True if commands to be sent via BLE tesla_remote = "[email protected]" # user:host if BLE command should be done on another device via SSH
Be aware, that in a git submodule in default, any commit will not be to the main branch, if you do not check out main.
- Inside the library=submodule directory:
- to check out before commit:
git checkout main
- to update:
git pull origin main