Decoding A Vehicle License Plate With An API
Tags: api, vehicle, tutorial, javascript
Published: 2019-12-28 17:12:27

If you're building an application that involves vehicles whether it's an AI solution or mobile parking application a VIN is one of the most powerful pieces of information you can attain to learn more about the vehicle and its owner.

Every vehicle on the road has a unique vehicle identification number (VIN) but that number is usually small and hidden under the windshield. Furthermore, most car owners don't know their vehicle's VIN let alone that they have one. 

One thing that is visible and every vehicle owner knows about is a vehicle's license plate. The license plate, like the VIN, is unique but it changes from owner to owner. 

In this tutorial we're going to use the license plate number of a vehicle to retrieve the VIN, make, model, year and a sample image of the vehicle.

## Retrieve Data Using License Plate Number

We'll use the [CarsXE Plate Decoder API](https://api.carsxe.com/vehicle-plate-decoder) and it's [documentation](https://api.carsxe.com/docs#vehicle-plate-decoder) to retrieve vehicle information using the license plate number.

Let's say I'm building a mobile parking application. In the app, I require the user to give me the license plate number and state of the vehicle.

The user provides the following information:
* `36619HT` - Plate number
* `MD` - Two-character state code for Maryland

So I need to make an HTTP GET request to the API requesting this data. I love JavaScript so I'll give an example using the request package in my NodeJS server application. Here's what I need to make the request:

1. Get a unique CarsXE API Key (by creating an [account](https://api.carsxe.com/login) and adding a payment method)
2. Make the request to the endpoint `http://api.carsxe.com/platedecoder?`
3. Set our `plate` query to our value: `plate=36619HT`
4. Set our `state` query to our value: `state=MD`
5. Set our `key` query to our CarsXE API Key: `key=<CarsXE_API_Key>`
6. Set our `format` query to either `json` or `xml` depending on how we'd like the format of the response to be.

Ok, let's make the request!

```
var request = require('request');

const r = "http://api.carsxe.com/platedecoder?plate=36619HT&state=MD&format=json&key=<CarsXE_API_Key>";

request(r, function (error, response, body) {
  console.log('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', body); // Print the HTML for the Google homepage.
});
```

Ok, let's see what the response of the API looks like!

```
{
    "success": true,
    "vin": "4T1BF22K5WU057633",
    "imageUrl": "https://api.carsxe.com/pic?image=@VG95b3RhIENhbXJ5IENFIC8gTEUgLyBYTEU=",
    "assembly": "United States",
    "Description": "Toyota Camry CE / LE / XLE",
    "RegistrationYear": "1998",
    "CarMake": "Toyota",
    "CarModel": "Camry CE / LE / XLE",
    "BodyStyle": "Sedan 4D",
    "EngineSize": "3.0L V6 EFI"
}
```

Isn't that amazing! Just from knowing the plate number and state which we required the user to enter we were capable of retrieving that the vehicle the user just parked is a 1998 Toyota Camry with a 3.0L V6 EFI engine. 

Even more impressive is that we were able to pull the vehicle VIN for that specific vehicle which we can use to pull the vehicle's [history](https://api.carsxe.com/vehicle-history), [specs](https://api.carsxe.com/vehicle-specifications), and [market value](https://api.carsxe.com/vehicle-market-value). We can know the owner of the vehicle, whether it's stolen, whether it's been in any accidents, or there are any liens on the vehicle and more.

I hope you can benefit from this tutorial and API as much as I have!