Download Images using Mercedes Vehicle Images API
Mercedes has a slew of API for various applications, offered to businesses, predominantly for businesses in the EU, and most are available for purchase only.
These APIs are not the one @ReneNulschDE is using for his Home Assistant integration.
Luckily, some APIs are offered on a trial basis, without having a business in the EU.
One of these is the Vehicle Images API, which can used by car dealers to present rendered pictures of the new or used cars they are offering as a basis for a “3D view”. Mercedes has a database of the options of all the cars they manufactures, and they use it to generate images that reflect the individual cars for which the images are requested. Meaning, if your car has blue paint, the images will show a blue car, etc. Its kind of a digital twin of your car:
This tutorial has three part:
- Getting access to the API
- Using the API
- Programs that use the API to download the images
A prerequisit to play along with the manual download is curl, which should come preinstalled on both Windows and Linux. Use it on the terminal/command line of your computer, not in Home Assistant.
Lets start. Click the arrow to expand a section.
Getting Access to the API
Set-up account and API
All of Mercedes’ API are found in the Developers Portal, so you need to get a Mercedes Developer Access.
The address for the Developers site is https://developer.mercedes-benz.com/
On there click Login.
Accept the terms of use:
You will land in your console page, where you need to create your project.
Fill out the form (the details are not relevant or checked as far as I know).
Then inside your project, add a Product:
Select “Vehicle Images”:
As the purchase model, select “Get for Free”. No credit card required.
Choose the “360° Trial” for the Package.
Please note that the free version cames with some pretty hefty restrictions, limiting it to 5 calls. More on that later.
Subscribe, then go to your project page.
There you need to create the API key for your project. Copy it down for further reference.
Thats it for setting up your developers account and activate the Vehicle Images API.
Using the API
Using the API
Go to the page of the Vehicle Images API, either click on the link on your project page, or from the “Products” page.
On the Vehicle Images page, you find the documentation under the appropriately named “Docs” link.
The workflow for getting the images are:
- Getting the
ImageIds
, which are unique IDs tied to both your API key, and the VIN of the car.
- Use the
ImageIds
to download the actual images.
The endpoint for the API is https://api.mercedes-benz.com/vehicle_images/v2
To get the ImageIds
, GET the following URL, with the authentification (API key) in the header.
curl -X "GET" "https://api.mercedes-benz.com/vehicle_images/v2/vehicles/<vehicleId>" -H "x-api-key: <insert_your_api_key_here>" -H "accept: application/json"
The result is a JSON output with the ImageIds
of each individual image. There are several parameters availabe to influence how the pictures are generated (backgrounds, daytime/nighttime, image parameters, open/closed root for convertibles etc.). See the “Default Image Settings” page.
A sample request looks like this:
Not every car model does have the same number of images available.
With the ImageIds
, you can now download the images.
As with the ImageIds
, the curl command to GET the images (with the API key in the header) is:
curl -X "GET" "https://api.mercedes-benz.com/vehicle_images/v2/images/<image-id>" -H "x-api-key: <insert_your_api_key_here>" -H "accept: */*"
You may want to include a -o <filename>
to save the image.
A sample request looks like this:
Now do that with all the rest of the images that you got from the previous step.
A note about the limit of “5 calls” that we touched on earlier.
As stated by Mercedes in the documentation (under “Caching of Images” section):
Even though all image references are generated dynamically, we will give you a limited time period to get the images from our side. You have to request the images within 24 hours, starting from the time of requesting the image references.
After this time period you will not be able to get the images anymore and you have to request new imageId(s). If you want to use these images for a longer time period, we advice you to store the files on your own.
So as long as you don’t generate new ImageIds
, you have all day long to download the actual images.
Automating the Downloads
There is a python script out there that lets you download the images without going through each step indivdually:
As the python script generates the ImageIds
on each run, each run may count against the 5 calls from the trial. Haven’t tested that myself.
Thats it, have fun with the stock image of your individual car. I’m looking forward to what you all come up with these images for your Home Assistant instance.
Thomas