fmcsa-sdk
v0.1.0
Published
Typed, ESM-friendly client for the FMCSA QCMobile REST API
Maintainers
Readme
FMCSA REST API
Typed, ESM-first client for the FMCSA QCMobile API. The official FMCSA documentation only includes prose tables, so this SDK wraps the public endpoints with a strongly typed TypeScript interface, normalizes the JSON and adds ergonomic helpers for Node.js and modern browsers.
Installation
npm install fmcsa-sdkThe package targets native ESM and requires Node.js 18+ (or any runtime that provides a fetch implementation). For Node.js 16 you can pass a custom fetch (for example node-fetch).
Authentication
FMCSA uses a WebKey query parameter for all requests. Store yours in an environment variable so you never accidentally commit it:
# .env
FMCSA_WEBKEY=replace-with-your-keyFmcsaClient automatically reads FMCSA_WEBKEY, FMCSA_WEB_KEY or WEBKEY. You can also pass the value explicitly through the constructor options.
Usage
import { FmcsaClient } from 'fmcsa-sdk';
const client = new FmcsaClient({
// Optional when FMCSA_WEBKEY is present in process.env
// webKey: process.env.FMCSA_WEBKEY,
userAgent: 'my-company/fmcsa-internal-tool',
});
const carriers = await client.searchCarriersByName('greyhound', { size: 10 });
carriers.data.forEach((carrier) => {
console.log(carrier.dotNumber, carrier.legalName, carrier.allowToOperate);
});
const profile = await client.getCarrierByDot(44110);
console.log(profile.data?.phyCity, profile.data?.telephone);
const basics = await client.getCarrierBasics(44110);
basics.data.forEach((basic) => console.log(basic.basicShortDesc, basic.percentile));Every public method returns a NormalizedResponse<T> with:
data– typed array/object for the requested endpoint.raw– the unmodified payload from FMCSA in case you need fields that are not modeled yet.- Metadata such as
retrievalDate,content(status text) and_linksif FMCSA sends them.
Available helpers
| Method | Description |
| --- | --- |
| searchCarriersByName(name, { start, size }) | Search carriers by legal or DBA name. |
| getCarrierByDot(dotNumber) | Fetch a single carrier profile by USDOT number. |
| getCarrierByDocketNumber(docketNumber) | Fetch a profile using an MC/MX docket number. |
| getCarrierBasics(dotNumber) | Retrieve BASIC measurements for a carrier. |
| getCargoCarried(dotNumber) | Cargo categories reported by the carrier. |
| getOperationClassifications(dotNumber) | Operation classifications published by FMCSA. |
| getOutOfServiceOrders(dotNumber) | Planned support once FMCSA exposes the /oos endpoint. Currently throws an explanatory error. |
| getDocketNumbers(dotNumber) | All docket numbers tied to the USDOT number. |
| getAuthority(dotNumber) | Operating authority statuses. |
⚠️ FMCSA only documents these endpoints in prose. The SDK normalizes obvious nested lists (e.g.
content[].carrier,content[].basic) but leaves the raw payload accessible so you can inspect additional fields as the agency evolves the API.
❗️ The
carriers/:dotNumber/oosendpoint currently responds with “Webkey not found” even with valid credentials. The SDK keepsgetOutOfServiceOrdersas a placeholder that throws an explanatory error so consumers know the functionality is pending FMCSA access.
Error handling
FmcsaSDKError– thrown for client side problems (missing webKey, failing to parse JSON, missingfetch, etc.).FmcsaApiError– thrown when FMCSA returns a non-2xx status. The instance exposes the HTTP status code and the parsed payload (when available).
Example:
try {
await client.getCarrierByDot('0');
} catch (error) {
if (error instanceof FmcsaApiError) {
console.error(error.status, error.payload);
}
}Configuration
const client = new FmcsaClient({
baseUrl: 'https://mobile.fmcsa.dot.gov/qc/services/',
defaultParams: { size: 25 },
fetch: customFetch,
userAgent: 'acme/fmcsa-sdk'
});baseUrllets you point the client at a mock server during tests.defaultParamsare appended to every request (for example a defaultsize).fetchallows you to bring your own fetch-compatible function in older runtimes.userAgentsets a friendly header for FMCSA logs.
Development
npm install
npm run typecheck
npm run buildPublishing is handled by npm's prepare hook so dist/ always stays in sync.
Sample payloads captured from the live FMCSA API live under /samples so you can inspect the raw schema without hitting the network.
Roadmap / ideas
- Add higher level helpers that page through large carrier search results.
- Remove the temporary error and implement
getOutOfServiceOrdersonce FMCSA exposes the endpoint publicly. - Contribute a community-maintained OpenAPI document once FMCSA expands their docs (possibly using the
/samplesfixtures as a starting point).
