Tesla Location API - PHP
This is a simple PHP implementation to query your Tesla's location using Tesla's API.
Requirements
- PHP 7.4 or higher
- cURL extension enabled
- A Tesla account with at least one vehicle
Files
TeslaAPI.php- Main API classexample_usage.php- Example script showing how to use the APIget_token.php- Helper script to get your access token
How to Get Your Access Token
Tesla uses OAuth authentication. Here are the main ways to get your access token:
Option 1: Use Tesla Auth (Recommended)
You can use a third-party tool to generate tokens. One popular option is:
- Visit: https://github.com/timdorr/tesla-api (check their auth section)
- Or use this web tool: https://tesla-info.com/tesla-token.php
- Follow the OAuth flow to get your access token
ACCESS_TOKEN="eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZ..." # your access token here
VEHICLE_ID="1492930654251209" # waso lili
VEHICLE_ID="1492932249535092" # TheLandShark
curl -s \
-H "Authorization: Bearer $ACCESS_TOKEN" \
"https://owner-api.teslamotors.com/api/1/vehicles/$VEHICLE_ID/vehicle_data"
Option 2: Use the Included PHP Auth Script
I've created a helper script that can guide you through the process:
php get_token.php
Option 3: Manual Token Extraction (Advanced)
You can extract the token from the Tesla mobile app using a proxy tool like Charles Proxy or mitmproxy, but this is more technical.
Usage
-
Get your access token (see above)
-
Edit
example_usage.phpand add your token:$accessToken = 'YOUR_ACCESS_TOKEN_HERE'; -
Run the script:
php example_usage.php
Example Output
Found 1 vehicle(s):
Vehicle: My Tesla
VIN: 5YJSA1E26HF123456
State: online
ID: 1234567890
Getting location for My Tesla...
Location Information:
-------------------
Latitude: 37.7749
Longitude: -122.4194
Heading: 180°
Speed: 0
Timestamp: 2026-01-28 10:30:45
Google Maps: https://www.google.com/maps?q=37.7749,-122.4194
API Methods
getVehicles()
Returns a list of all vehicles on your account.
getLocation($vehicleId, $wakeIfAsleep = true)
Gets the current location of a specific vehicle. Will automatically wake the vehicle if it's asleep.
Returns:
latitude- GPS latitudelongitude- GPS longitudeheading- Direction the car is facing (0-360°)speed- Current speed (may be null if stationary)timestamp- When the data was recordedgoogle_maps_url- Direct link to Google Maps
wakeVehicle($vehicleId)
Wakes up a sleeping vehicle. Note: This will drain a small amount of battery.
getVehicleData($vehicleId)
Gets all vehicle data including charge state, climate, drive state, etc.
Important Notes
-
Battery Drain: Frequently waking your vehicle or polling for data can drain the battery. Be mindful of how often you query the API.
-
Unofficial API: Tesla doesn't officially support third-party API access, so endpoints may change without notice.
-
Rate Limiting: Don't poll too frequently. Tesla may rate limit your requests.
-
Security: Keep your access token secure! Don't commit it to version control or share it publicly.
-
Token Expiration: Access tokens expire after some time. You may need to refresh them periodically.
Troubleshooting
"Vehicle is asleep": The script will automatically try to wake the vehicle. This is normal if the car hasn't been used recently.
"API request failed with code 401": Your access token is invalid or expired. Get a new one.
"API request failed with code 408": The vehicle is taking too long to wake up. Try again in a minute.
Advanced Usage
You can also get other vehicle data:
// Get full vehicle data
$data = $tesla->getVehicleData($vehicleId);
// Access charge state
echo "Battery level: " . $data['charge_state']['battery_level'] . "%\n";
// Access climate state
echo "Interior temp: " . $data['climate_state']['inside_temp'] . "°C\n";
License
This code is provided as-is for educational purposes. Use at your own risk.
