A modern TypeScript library for validating tracking numbers from multiple shipping carriers.
- TypeScript First: Full TypeScript support with comprehensive type definitions
- Modern ESM: ES modules with CommonJS compatibility
- Zero Dependencies: Lightweight with no external dependencies
- Comprehensive Testing: Full test coverage with Jest
- Multiple Carriers: Support for 11+ shipping carriers
- UPS - United Parcel Service
- USPS - United States Postal Service (including barcodes starting with 95)
- FedEx - Federal Express
- DHL - DHL Express
- OnTrac - OnTrac Shipping
- Amazon - Amazon Logistics
- LaserShip - LaserShip Delivery
- Canada Post - Canada Post Corporation
- China Post - China Post Corporation
- Australia Post - Australia Post Corporation
- Royal Mail - Royal Mail Group
# Using pnpm (recommended) pnpm add tracking-number-validation # Using npm npm install tracking-number-validation # Using yarn yarn add tracking-number-validation
import { getCourier, getCourierOne, isValid, isCourier, getTrackingUrl, getAllPossibleCouriers, getValidCouriersOnly, getAllTrackingUrlsForNumber, getDetailedCourierInfo, generateTrackingNumber, generateMultipleTrackingNumbers, type CourierName } from 'tracking-number-validation'; // Get all matching couriers for a tracking number const couriers = getCourier('1Z9999W99999999999'); console.log(couriers); // ['ups'] // Get the first matching courier const courier = getCourierOne('1Z9999W99999999999'); console.log(courier); // 'ups' // Check if tracking number is valid const isValidNumber = isValid('1Z9999W99999999999'); console.log(isValidNumber); // true // Check if tracking number belongs to specific courier const isUPS = isCourier('1Z9999W99999999999', 'ups'); console.log(isUPS); // true // Get tracking URL const trackingUrl = getTrackingUrl('1Z9999W99999999999', 'ups'); console.log(trackingUrl); // 'https://www.ups.com/track?trackingNumber=1Z9999W99999999999' // Auto-detect courier and get URL const autoUrl = getTrackingUrl('1Z9999W99999999999'); console.log(autoUrl); // 'https://www.ups.com/track?trackingNumber=1Z9999W99999999999' // Get all possible couriers (including pattern matches that may not validate) const allPossible = getAllPossibleCouriers('1Z9999W99999999999'); console.log(allPossible); // ['ups'] // Get only validated couriers const validOnly = getValidCouriersOnly('1Z9999W99999999999'); console.log(validOnly); // ['ups'] // Get detailed validation info const detailedInfo = getDetailedCourierInfo('1Z9999W99999999999'); console.log(detailedInfo); // [{ courier: 'ups', valid: true, tracking_url: '...' }] // Generate tracking numbers const generatedUPS = generateTrackingNumber('ups'); console.log(generatedUPS); // '1Z12345E1512345676' // Generate multiple tracking numbers const multipleUSPS = generateMultipleTrackingNumbers('usps', 3); console.log(multipleUSPS); // ['9400100000000000000000', '9400100000000000000001', ...]
const { getCourier, isValid, getTrackingUrl, generateTrackingNumber, CourierName } = require('tracking-number-validation'); const courier = getCourier('9400100000000000000000'); console.log(courier); // ['usps']
<script src="https://unpkg.com/[email protected]/dist/index.global.js"></script> <script> const courier = TNV.getCourier('1Z9999W99999999999'); console.log(courier); // ['ups'] </script>
Returns an array of courier names that match the tracking number pattern.
getCourier('1Z9999W99999999999'); // ['ups'] getCourier('invalid'); // []
Returns the first matching courier name or undefined if no match.
getCourierOne('1Z9999W99999999999'); // 'ups' getCourierOne('invalid'); // undefined
Checks if the tracking number is valid for any supported courier.
isValid('1Z9999W99999999999'); // true isValid('invalid'); // false
Checks if the tracking number belongs to a specific courier.
isCourier('1Z9999W99999999999', 'ups'); // true isCourier('1Z9999W99999999999', 'fedex'); // false
Returns the tracking URL for the given tracking number and courier.
getTrackingUrl('1Z9999W99999999999', 'ups'); // 'https://www.ups.com/track?trackingNumber=1Z9999W99999999999' getTrackingUrl('1Z9999W99999999999'); // Auto-detects UPS // 'https://www.ups.com/track?trackingNumber=1Z9999W99999999999'
Adds a custom pattern for an existing courier.
injectPatterns('ups', /CUSTOM\d{10}/); // true isValid('CUSTOM1234567890'); // true (now matches UPS)
Returns all couriers that match the tracking number pattern (including those that may not validate).
getAllPossibleCouriers('1Z9999W99999999999'); // ['ups'] getAllPossibleCouriers('invalid'); // []
Returns only the couriers that both match the pattern and validate the tracking number.
getValidCouriersOnly('1Z9999W99999999999'); // ['ups'] getValidCouriersOnly('invalid'); // []
Returns tracking URLs for all matching couriers with validation information.
getAllTrackingUrlsForNumber('1Z9999W99999999999'); // [{ courier: 'ups', url: 'https://...', valid: true }]
Returns comprehensive validation information for all matching couriers.
getDetailedCourierInfo('1Z9999W99999999999'); // [{ courier: 'ups', valid: true, tracking_url: 'https://...' }]
Generates a valid-format tracking number for the specified courier.
generateTrackingNumber('ups'); // '1Z12345E1512345676' generateTrackingNumber('fedex'); // '999999999999'
Generates multiple tracking numbers for the specified courier.
generateMultipleTrackingNumbers('usps', 3); // ['9400100000000000000000', '9400100000000000000001', '9400100000000000000002']
Generates a tracking number and validates it against the library's own validation rules.
generateTrackingNumberWithValidation({ courier: 'ups' }); // '1Z12345E1512345676' (guaranteed to pass isValid())
1Z9999W999999999991Z12345E1512345676T9999999999
9400 1000 0000 0000 0000 009205 5000 0000 0000 0000 009500 1000 0000 0000 0000 00(95 prefix supported)EC 000 000 000 US82 000 000 00
9999 9999 99999999 9999 9999 99999999999999961299995669352455464
125-12345678125 12345678SEA1234567
C00000000000000
TBA502887274000
1LS123456789012
RP123456789CA1234567890123
EE123456789CNRR123456789CNCP123456789CN
AP123456789AU123456789012TM123456789012345
GB123456789GBRR123456789GB
Generate valid tracking numbers for testing purposes:
import { generateTrackingNumber, generateMultipleTrackingNumbers, CourierName } from 'tracking-number-validation'; // Generate single tracking numbers const upsNumber = generateTrackingNumber(CourierName.UPS); const fedexNumber = generateTrackingNumber(CourierName.FEDEX); const uspsNumber = generateTrackingNumber(CourierName.USPS); // Generate multiple tracking numbers const multipleNumbers = generateMultipleTrackingNumbers(CourierName.UPS, 5); console.log(multipleNumbers); // ['1Z...', '1Z...', '1Z...', '1Z...', '1Z...'] // Generate with specific formats (where supported) const fedexGround = generateTrackingNumber({ courier: CourierName.FEDEX, format: 'ground' }); // Available formats per courier import { courierFormats } from 'tracking-number-validation'; console.log(courierFormats[CourierName.FEDEX]); // ['express', 'ground', 'smartpost'] console.log(courierFormats[CourierName.USPS]); // ['tracking', 's10', 'certified']
- FedEx:
express(12 digits),ground(15 digits),smartpost(22 digits) - USPS:
tracking(22 digits),s10(13 chars),certified(20 digits) - DHL:
express(10 digits),ecommerce(10 chars),global(18 chars) - Australia Post:
s10,domestic,tm - Canada Post:
s10,domestic - UPS, Amazon, OnTrac, LaserShip, China Post, Royal Mail: Default formats only
- Full TypeScript rewrite with comprehensive type definitions
- Modern ESM support with CommonJS compatibility
- Updated UPS tracking URLs (removed deprecated
/mobile/path) - Enhanced USPS support for barcodes starting with 95
- New carriers added: LaserShip, Canada Post, China Post, Australia Post, and Royal Mail
- Tracking number generation: Generate valid tracking numbers for testing
- Advanced validation functions: Get detailed courier information and validation results
- Minimum Node.js version: 16+
- Full TypeScript types required
- ES modules by default
- Updated build system using
tsup
- Modern Jest testing with TypeScript
- ESLint configuration for TypeScript
- Comprehensive test coverage
- Updated documentation and examples
We welcome contributions! Please feel free to:
- Fork the repository
- Create a feature branch
- Make your changes with tests
- Submit a pull request
# Clone the repository git clone https://github.com/niradler/tracking-number-validation.git cd tracking-number-validation # Install dependencies pnpm install # Run tests pnpm test # Build the project pnpm build # Type checking pnpm type-check # Linting pnpm lint
MIT License - see LICENSE file for details.
- Complete TypeScript rewrite
- Added 5 new carriers: LaserShip, Canada Post, China Post, Australia Post, and Royal Mail
- Tracking number generation capabilities
- Advanced validation and courier detection functions
- Fixed UPS tracking URLs
- Enhanced USPS barcode support (95 prefix)
- Modern ESM build system
- Legacy JavaScript version
- Basic courier support