NHTSA Vehicles
VehicleExploring the NHTSA API for Vehicle Information
The National Highway Traffic Safety Administration (NHTSA) provides a public API that allows anyone to retrieve information about vehicles, including their make, model, year, and other relevant data.
Getting Started
Before we dive into the API, we need to get an API key. Visit the NHTSA API website and sign up for an API key. Once you have your key, you can start using the API by making HTTP requests with your key as a query parameter.
Example 1: Retrieve General Vehicle Information
Let's start by retrieving general information about a specific vehicle. In this example, we'll retrieve the make, model, year, and origin of a vehicle using its VIN.
const vin = '1HGCM82633A004352';
const apiKey = 'YOUR_API_KEY';
const url = `https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/${vin}?format=json&modelyear=${year}&catalog=true&brandName=true&key=${apiKey}`;
fetch(url)
.then(response => response.json())
.then(data => {
const make = data.Results[6].Value;
const model = data.Results[8].Value;
const year = data.Results[9].Value;
const origin = data.Results[10].Value;
console.log(`Make: ${make}, Model: ${model}, Year: ${year}, Origin: ${origin}`);
})
.catch(error => console.log(error));
Example 2: Retrieve Vehicle Manufacturer Details
In this example, we'll retrieve details about a vehicle manufacturer using its ID.
const manufacturerId = 987;
const apiKey = 'YOUR_API_KEY';
const url = `https://vpic.nhtsa.dot.gov/api/vehicles/getmanufacturerdetails/${manufacturerId}?format=json&key=${apiKey}`;
fetch(url)
.then(response => response.json())
.then(data => {
const name = data.Results[0].Mfr_Name;
const country = data.Results[0].Country;
const address = `${data.Results[0].City}, ${data.Results[0].State}, ${data.Results[0].Zip}`;
console.log(`Name: ${name}, Country: ${country}, Address: ${address}`);
})
.catch(error => console.log(error));
Example 3: Retrieve Vehicle Equipment Details
In this example, we'll retrieve details about a vehicle's equipment using its ID.
const equipmentId = 10002345;
const apiKey = 'YOUR_API_KEY';
const url = `https://vpic.nhtsa.dot.gov/api/vehicles/getequipmentdetails/${equipmentId}?format=json&key=${apiKey}`;
fetch(url)
.then(response => response.json())
.then(data => {
const name = data.Results[0].EquipmentName;
const description = data.Results[0].EquipmentDescription;
console.log(`Name: ${name}, Description: ${description}`);
})
.catch(error => console.log(error));
Conclusion
The NHTSA API provides a wealth of information about vehicles that can be utilized in various applications. In this short guide, we covered just a few examples of how to use the API in your JavaScript code. By exploring the API documentation and experimenting with the examples outlined here, you can discover even more ways to leverage this powerful resource.