Tesla Inventory Stock

The following scripts are provided as-is, I made some updates that allow you to set some variables at the top of the script to direct your search. Currently it is configured to allow a single color, you would need to run multiple instances if you are flexible in color choices. If you are familiar with scripting, you can add multiple colors in the PAINT array in the filter JSON. The condition is set to “used”, I would run the script at least one time with that to make sure the behavior is as expected. Once you confirm the alerting and color choice is found, update the value to new and save the script.

The MACOS version will use the “say” command to make an announcement when starting as well as if any are found in inventory. The script will also create a notification with a link to the filtered list based on the variables set at the beginning of the script.

The Windows version will issue a system beep “[console]::beep(500,300)”, which I cant test as I only have a Windows VM and the sounds are not passed in my case. When it finds a match, it will launch a URL which should open your default browser to the link with the filtered listings. That behavior is initiated by the line “start “https://www.tesla.com/inventory/${condition}/${model}”…. which can be deleted if you don’t wish that to occur.

Check https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell?view=powershell-7.1 for downloads for Powershell.

***Noticed that the API query will sometime returns cars that have an invalid VIN, which do not show up in the web interface. If you look at the output file, you can search for “VIN” (include the quotes), you might see a VIN that looks like this “VIN”: “5YJ3204_d96db18febd06a645100eb3b80a5526e” – which won’t appear in the inventory web interface while a VIN “VIN”: “5YJ3E1EA1LF745917” that is valid should be available to purchase.

mac os version (Powershell 7.x installed):

$api_url = 'https://www.tesla.com/inventory/api/v1/inventory-results'
$color = "WHITE"
# Valid color values: WHITE,BLACK,BLUE,SILVER,RED,GRAY
$zip = "92694"
$model = "m3"
# Valid Model numbers: m3,my,ms,mx
$condition = "used"
# Valid conditions: used, new

function Get-TimeStamp {
    return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
}

say "Starting API query for Tesla $model Inventory"
echo "$(Get-TimeStamp) Start time"

$filters = @"
{
    "query": {
        "model": "$model",
        "condition": "$condition",
        "options": {
            "PAINT": [
                "$color"
            ]
        },
        "arrangeby": "Price",
        "order": "asc",
        "market": "US",
        "language": "en",
        "super_region": "north america",
        "zip": "$zip",
        "range": 200
    },
    "offset": 0,
    "count": 50,
    "outsideOffset": 0,
    "outsideSearch": false
}
"@

while ($true) {
    $CompressedFilters = $filters | ConvertFrom-Json | ConvertTo-Json -Depth 10 -Compress
    $encodedUri = "{0}?query={1}" -f $api_url, [System.Web.HttpUtility]::UrlEncode($CompressedFilters)
    $inventory = Invoke-WebRequest -Method 'GET' -Uri $encodedUri -ContentType 'application/json' -SkipCertificateCheck
    $inventoryjson = echo $inventory."Content" | ConvertFrom-Json

    $totalMatches = $inventoryjson."total_matches_found"
    echo "Checking Tesla $model Inventory"
    if ($totalMatches -gt 0) {
        say "$totalMatches Tesla $model cars found"
        echo "$(Get-TimeStamp) $totalMatches $model Cars In Inventory"
        echo $inventoryjson | ConvertTO-Json -Depth 9 | Add-Content -Path .\inventory.txt
        terminal-notifier -message “Tesla $model found, go buy now” -title “Tesla $model Found” -open "https://www.tesla.com/inventory/${condition}/${model}?PAINT=${color}&arrangeby=plh&zip=${zip}&range=200&referral=sky96454"
    }
    Start-sleep -s 30
}

Windows Version (Powershell 7.x)

$api_url = 'https://www.tesla.com/inventory/api/v1/inventory-results'
$color = "WHITE"
# Valid color values: WHITE,BLACK,BLUE,SILVER,RED,GRAY
$zip = "92694"
$model = "m3"
# Valid Model numbers: m3,my,ms,mx
$condition = "used"
# Valid conditions: used, new

function Get-TimeStamp {
    return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
}

echo "$(Get-TimeStamp) Start time"

$filters = @"
{
    "query": {
        "model": "$model",
        "condition": "$condition",
        "options": {
            "PAINT": [
                "$color"
            ]
        },
        "arrangeby": "Price",
        "order": "asc",
        "market": "US",
        "language": "en",
        "super_region": "north america",
        "zip": "$zip",
        "range": 200
    },
    "offset": 0,
    "count": 50,
    "outsideOffset": 0,
    "outsideSearch": false
}
"@

while ($true) {
    $CompressedFilters = $filters | ConvertFrom-Json | ConvertTo-Json -Depth 10 -Compress
    $encodedUri = "{0}?query={1}" -f $api_url, [System.Web.HttpUtility]::UrlEncode($CompressedFilters)
    $inventory = Invoke-WebRequest -Method 'GET' -Uri $encodedUri -ContentType 'application/json' -SkipCertificateCheck
    $inventoryjson = echo $inventory."Content" | ConvertFrom-Json

    $totalMatches = $inventoryjson."total_matches_found"
    echo "Checking Tesla $model Inventory"
    if ($totalMatches -gt 0) {
        [console]::beep(500,300)
        echo "$(Get-TimeStamp) $totalMatches $model Cars In Inventory"
        echo $inventoryjson | ConvertTO-Json -Depth 9 | Add-Content -Path .\inventory.txt
        start "https://www.tesla.com/inventory/${condition}/${model}?PAINT=${color}&arrangeby=plh&zip=${zip}&range=200&referral=sky96454"
    }
    Start-sleep -s 30
}