UNPKG

8.56 kBMarkdownView Raw
1# @shaggytools/nhtsa-api-wrapper
2
3<p align="center">
4 <a href="https://www.npmjs.com/package/@shaggytools/nhtsa-api-wrapper" target="_blank">
5 <img src="https://img.shields.io/npm/v/@shaggytools/nhtsa-api-wrapper?style=for-the-badge">
6 </a>
7 <a href="https://github.com/ShaggyTech/nhtsa-api-wrapper/actions/workflows/ci.yml" target="_blank">
8 <img src="https://img.shields.io/github/actions/workflow/status/shaggytech/nhtsa-api-wrapper/ci.yml?style=for-the-badge">
9 </a>
10 <a href="https://bundlephobia.com/result?p=@shaggytools/nhtsa-api-wrapper" target="_blank">
11 <img src="https://img.shields.io/bundlephobia/min/@shaggytools/nhtsa-api-wrapper?style=for-the-badge">
12 </a>
13 <a href="https://bundlephobia.com/result?p=@shaggytools/nhtsa-api-wrapper" target="_blank">
14 <img src="https://img.shields.io/bundlephobia/minzip/@shaggytools/nhtsa-api-wrapper?style=for-the-badge">
15 </a>
16 <a href="https://codecov.io/gh/ShaggyTech/nhtsa-api-wrapper" target="_blank">
17 <img src="https://img.shields.io/codecov/c/github/shaggytech/nhtsa-api-wrapper/main?style=for-the-badge">
18 </a>
19</p>
20
21---
22
23## Javascript Wrapper and Helper Functions for the [NHTSA VPIC API](https://vpic.nhtsa.dot.gov/api/Home)
24
25A universal (browser/server) javascript wrapper for the National Highway Traffic
26Safety Administration (NHTSA) Vehicle Information API (VPIC).
27
28The VPIC API is primarily used for decoding useful information from a Vehicle Identification Number
29(VIN) in the United States and Canada. It can also be used to get all models of a make, to decode
30WMIs, get all makes for a certain year, and more.
31
32---
33
34<div align="center" style="font-size: 1.5em; font-weight: bold;">Built With:</div>
35<br>
36<p align="center">
37 <a href="https://github.com/pnpm/pnpm" target="_blank">
38 <img src="https://img.shields.io/static/v1?style=for-the-badge&message=pnpm&color=222222&logo=pnpm&logoColor=F69220&label=">
39 </a>
40 <a href="https://vitejs.dev" target="_blank">
41 <img src="https://img.shields.io/static/v1?style=for-the-badge&message=Vite&color=646CFF&logo=Vite&logoColor=FFFFFF&label=">
42 </a>
43 <a href="https://vitepress.dev/" target="_blank">
44 <img src="https://img.shields.io/static/v1?style=for-the-badge&message=Vitepress&color=000000&logo=Vite&logoColor=FFFFFF&label=">
45 </a>
46 <a href="https://vitest.dev/" target="_blank">
47 <img src="https://img.shields.io/static/v1?style=for-the-badge&message=Vitest&color=222222&logo=Vitest&logoColor=4FC08D&label=">
48 </a>
49 <a href="https://github.com/vercel/turborepo" target="_blank">
50 <img src="https://img.shields.io/static/v1?style=for-the-badge&message=Turborepo%20by%20Vercel&color=000000&logo=Vercel&logoColor=FFFFFF&label=">
51 </a>
52</p>
53
54---
55
56## Full Documentation
57
58### [https://vpic.shaggytech.com/](https://vpic.shaggytech.com/)
59
60- [Introduction](https://vpic.shaggytech.com/guide)
61- [Install](https://vpic.shaggytech.com/guide/install)
62- [Node Quick Start](https://vpic.shaggytech.com//guide/getting-started#node-quick-start)
63- [Browser Quick Start](https://vpic.shaggytech.com/guide/getting-started#browser-quick-start)
64- [API Reference](https://vpic.shaggytech.com/api/)
65
66## Node Install
67
68### [NPM](https://www.npmjs.com/package/@shaggytools/nhtsa-api-wrapper)
69
70```sh [npm]
71$ npm install @shaggytools/nhtsa-api-wrapper
72```
73
74### Yarn
75
76```sh [yarn]
77$ yarn add @shaggytools/nhtsa-api-wrapper
78```
79
80### Pnpm
81
82```sh [pnpm]
83$ pnpm add @shaggytools/nhtsa-api-wrapper
84```
85
86## Node Quick Start
87
88Decoding a VIN is as easy as importing the `DecodeVinValues` function and calling it
89with a VIN.
90
91Make sure to first install via your favorite package manager or use a CDN.
92
93```javascript
94import { DecodeVinValues } from '@shaggytools/nhtsa-api-wrapper'
95
96const results = await DecodeVinValues('WA1A4AFY2J2008189')
97
98/*
99results = {
100 Count: 136, - number of Results objects returned
101 Message: 'Results returned successfully ...',
102 SearchCriteria: 'VIN:WA1A4AFY2J2008189',
103 Results: [ {...} ] - an array with single object of type DecodeVinValuesResults
104}
105*/
106
107/* You can also use destructuring to get the Results object */
108const { Results } = await DecodeVinValues('WA1A4AFY2J2008189')
109
110/* This endpoint only returns a single object in the Results array
111 The first object in the array is the decoded VIN data */
112const decodedVehicle = Results[0] // equals an object of type DecodeVinValuesResults
113```
114
115For a full example response see: [DecodeVinValues](https://vpic.shaggytech.com/api/endpoints/decode-vin-values#returns)
116
117All available endpoints can be found here: [VPIC API Endpoints](https://vpic.shaggytech.com/api/#vpic-api-endpoints)
118
119## Browser Install
120
121You can use the package directly in html script tags using a CDN. There are several options for CDN
122providers.
123
124For targeting modern browsers, you can use the ESM versions with `<script type="module">` and
125import statements.
126
127For older browsers, you can use the IIFE versions with `<script src="https://...">` to import the
128package. Then use the package in a separate html script via the browser global `NHTSA`. This global
129variable is only available when using the IIFE or UMD versions.
130
131### UNPKG CDN
132
133All package files and types are hosted on the UNPKG CDN found here:
134
135[https://www.unpkg.com/@shaggytools/nhtsa-api-wrapper/](https://www.unpkg.com/@shaggytools/nhtsa-api-wrapper/)
136
137### jsDelivr CDN
138
139Homepage: [https://www.jsdelivr.com/package/npm/@shaggytools/nhtsa-api-wrapper](https://www.jsdelivr.com/package/npm/@shaggytools/nhtsa-api-wrapper)
140
141## Browser Quick Start
142
143The following examples use the jsDelivr CDN:
144
145#### ESM:
146
147_~ 4kB (auto minified)_
148
149```html
150<!DOCTYPE html>
151<html lang="en">
152 <head>
153 <meta charset="UTF-8" />
154 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
155 </head>
156
157 <body>
158 <h1>ESM Example</h1>
159 </body>
160
161 <script type="module">
162 // import the entire package as a single object called NHTSA
163 import NHTSA from 'https://cdn.jsdelivr.net/npm/@shaggytools/nhtsa-api-wrapper/+esm'
164 // log to see all exported functions
165 console.log(NHTSA)
166
167 // OR import individual functions as needed
168 import { DecodeVinValues } from 'https://cdn.jsdelivr.net/npm/@shaggytools/nhtsa-api-wrapper/+esm'
169 // Decode a VIN and log the results
170 const { Results } = await DecodeVinValues('11111111111111111')
171 console.log('Results', Results[0])
172 </script>
173</html>
174```
175
176#### IIFE:
177
178_~ 4kB (auto minified)_
179
180IIFE browser global variable: `NHTSA`
181
182```html
183<!DOCTYPE html>
184<html lang="en">
185 <head>
186 <meta charset="UTF-8" />
187 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
188
189 <!-- Import via jsDelivr CDN -->
190 <script
191 src="https://cdn.jsdelivr.net/npm/@shaggytools/nhtsa-api-wrapper"
192 ></script>
193 </head>
194
195 <body>
196 <btn id="DecodeVinValues"
197 >Click to use DecodeVinValues()</btn
198 >
199 </br>
200 Results:
201 <div id="DecodeVinValuesResults"></div>
202 </body>
203
204 <!-- Use the package in a separate script -->
205 <script>
206 // log the browser global NHTSA to see all exported functions
207 console.log(NHTSA)
208
209 // add click handler to a button that uses the DecodeVinValues() function
210 document
211 .getElementById("DecodeVinValues")
212 .addEventListener("click", async function () {
213 const response = await NHTSA.DecodeVinValues("3VWD07AJ5EM388202").catch(
214 (err) => err
215 );
216
217 // log the VPIC response
218 console.log('VPIC Response: ', response);
219
220 // add the decoded VIN results to the DOM
221 document.getElementById("DecodeVinValuesResults").innerText =
222 JSON.stringify(response.Results[0]);
223 });
224 </script>
225</html>
226```
227
228## List of Exported Functions
229
230```javascript
231import {
232 // NHTSA API Endpoints
233 DecodeVin,
234 DecodeVinExtended,
235 DecodeVinValues,
236 DecodeVinValuesBatch,
237 DecodeVinValuesExtended,
238 DecodeWMI,
239 GetAllMakes,
240 GetAllManufacturers,
241 GetCanadianVehicleSpecifications,
242 GetEquipmentPlantCodes,
243 GetMakeForManufacturer,
244 GetMakesForManufacturerAndYear,
245 GetMakesForVehicleType,
246 GetManufacturerDetails,
247 GetModelsForMake,
248 GetModelsForMakeId,
249 GetModelsForMakeIdYear,
250 GetModelsForMakeYear,
251 GetParts,
252 GetVehicleTypesForMake,
253 GetVehicleTypesForMakeId,
254 GetVehicleVariableList,
255 GetVehicleVariableValuesList,
256 GetWMIsForManufacturer,
257 // composable function returning helper functions for NHTSA API
258 useNHTSA,
259 // function for offline VIN validation
260 isValidVin,
261} from '@shaggytools/nhtsa-api-wrapper'
262```