A Node.js library for querying United Nations Location Code (UN/LOCODE) data
npm install @geoapify/un-locodeA lightweight Node.js helper for querying United Nations Location Code (UN/LOCODE) data locally. Look up a single location by country and place code and get back normalized metadata, functional designations, and coordinates (when available).
``bash`
npm install @geoapify/un-locode
query is synchronous and returns either a UnlocodeItem object or null. Country and location codes must be uppercase ISO 3166-1 alpha-2 and UN/LOCODE values respectively.
CommonJS:
`javascript
const { query } = require('@geoapify/un-locode');
const aberdeen = query('US', 'APG');
console.log(aberdeen?.locationName); // "Aberdeen"
`
ESM / TypeScript:
`ts
import { query } from '@geoapify/un-locode';
import type { UnlocodeItem } from '@geoapify/un-locode';
const aberdeen: UnlocodeItem | null = query('US', 'APG');
`
- countryCode (string): ISO 3166-1 alpha-2 code (US, DE, SG, ...).locationCode
- (string): Three-character UN/LOCODE location code (APG, HAM, ...).
Example return value:
`json`
{
"country": "US",
"location": "APG",
"locationName": "Aberdeen",
"subdivision": "MD",
"status": "AI",
"functionCodes": ["3", "4"],
"coordinates": {
"lat": 39.5120347,
"lon": -76.1643289
}
}
- country: ISO country code.location
- : UN/LOCODE location code.locationName
- : Name without diacritics.subdivision
- : Optional state/province value.status
- : Two-letter UN/LOCODE status code.functionCodes
- : Array of functional designations (see below).coordinates
- : Optional WGS84 latitude/longitude pair.
> Note: Some rows in the original UN/LOCODE dump lack coordinates. We enrich those rows with the Geoapify Geocoding API when generating the bundled dataset.
- 1 = Port (any waterborne transport)
- 2 = Rail terminal
- 3 = Road terminal
- 4 = Airport
- 5 = Postal exchange office
- 6 = Inland Clearance Depot (ICD) / dry port
- 7 = Fixed transport functions (pipelines, power lines, ropeways, etc.)
- B = Border crossing
- 0 = Function unknown / not specified
- AA: Approved by a competent national government agency
- AC: Approved by Customs Authority
- AF: Approved by a national facilitation body
- AI: Adopted by an international organization (e.g., IATA or ECLAC)
- AM: Approved by the UN/LOCODE Maintenance Agency
- AQ: Entry approved, functions not verified
- AS: Approved by a national standardization body
- RL: Recognized location confirmed by a gazetteer or other reference
- RN: Request from credible national sources for locations within their country
- RQ: Request under consideration
- UR: Entry included on user’s request; not officially approved
- RR: Request rejected
- QQ: Original entry not verified since the indicated date
- XX: Entry to be removed in the next issue of UN/LOCODE
The library lazily loads CSV files per-country and keeps them in memory for 24 hours. Long-running processes benefit from repeated queries, while the cache automatically purges stale entries. If you need tighter control (for instance, to release memory after batch processing), restart the process or load only the countries you require.
- The repository currently ships with the UNECE 2024-2 UN/LOCODE release (published January 2025) found in data-source/2024-2 UNLOCODE CodeList.csv.data-source/
- Geoapify’s Geocoding API is used to supplement coordinates for rows that lack latitude/longitude in the official dump.
- To regenerate the JSON/CSV assets for a newer dataset:
1. Place the new CSV/XLS file inside .API_KEY
2. Add your to src/utility/utility.ts (used during geocoding).npm run generate-files
3. Run (see BUILD.md for details).
- Node.js 18.0.0 or newer (the generator scripts rely on modern ECMAScript features).
- npm (comes with Node) for running the provided scripts.
Install dependencies once via npm install, then use the commands below as needed.
`bash`
npm test
`bash`
npm run build-all
This compiles the TypeScript sources and bundles the packaged JSON assets.
1. Drop the latest CSV/XLS export into the root data-source/ folder.src/utility/utility.ts
2. Add your Geoapify API key to .
3. Run:
`bash`
npm run generate-files
1. Build the project (npm run build-all).
2. Start the demo app:
`bash`
npm run start:demo
query returns null when the provided country/location combination does not exist. Make sure to guard for this in your application.
Contributions are welcome! Please fork the repository, make your changes, and submit a pull request. See BUILD.md` for full data-generation instructions.