Skip to content

Instantly share code, notes, and snippets.

@egavard
Created July 3, 2024 15:12
Show Gist options
  • Save egavard/3f0d8decb600dcd79c7ddc57d8392d8c to your computer and use it in GitHub Desktop.
Save egavard/3f0d8decb600dcd79c7ddc57d8392d8c to your computer and use it in GitHub Desktop.
Retrieve invoices from Tesla API for the month instead of using the APP
#!/bin/bash
CLIENT_ID=
CLIENT_SECRET=
AUDIENCE=https://fleet-api.prd.eu.vn.cloud.tesla.com
#LOGIN
TESLA_API_TOKEN=$(curl --request POST --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'grant_type=client_credentials' --data-urlencode "client_id=$CLIENT_ID" \
--data-urlencode "client_secret=$CLIENT_SECRET" --data-urlencode 'scope=openid vehicle_device_data vehicle_cmds vehicle_charging_cmds' \
--data-urlencode "audience=$AUDIENCE" 'https://auth.tesla.com/oauth2/v3/token' | jq -r '.access_token')
invoices=$(curl --header 'Content-Type: application/json' --header "Authorization: Bearer $TESLA_API_TOKEN" \
"https://fleet-api.prd.na.vn.cloud.tesla.com/api/1/dx/charging/history?startTime=2024-06-01T00:00:00Z&endTime=2024-06-30T00:00:00Z" | jq -r ".data[].invoices[].contentId")
for INVOICE_ID in $invoices
do
curl --header 'Content-Type: application/json' \
--header "Authorization: Bearer $TESLA_API_TOKEN" \
"https://fleet-api.prd.na.vn.cloud.tesla.com/api/1/dx/charging/invoice/${INVOICE_ID}" >> $INVOICE_ID.pdf
done
@killtux
Copy link

killtux commented Jul 9, 2024

is there a way to extend the script to sent all fetched invoices to an email account?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment