@geoapify/un-locode
v1.0.3
Published
A Node.js library for querying United Nations Location Code (UN/LOCODE) data
Maintainers
Readme
@geoapify/un-locode
A 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).
Installation
npm install @geoapify/un-locodeUsage
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:
const { query } = require('@geoapify/un-locode');
const aberdeen = query('US', 'APG');
console.log(aberdeen?.locationName); // "Aberdeen"ESM / TypeScript:
import { query } from '@geoapify/un-locode';
import type { UnlocodeItem } from '@geoapify/un-locode';
const aberdeen: UnlocodeItem | null = query('US', 'APG');Parameters
countryCode(string): ISO 3166-1 alpha-2 code (US,DE,SG, ...).locationCode(string): Three-character UN/LOCODE location code (APG,HAM, ...).
Response
Example return value:
{
"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.
Function Codes
- 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
Status Codes
- 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
Caching Behavior
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.
Data Source & Updates
- The repository currently ships with the UNECE 2024-2 UN/LOCODE release (published January 2025) found in
data-source/2024-2 UNLOCODE CodeList.csv. - 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:
- Place the new CSV/XLS file inside
data-source/. - Add your
API_KEYtosrc/utility/utility.ts(used during geocoding). - Run
npm run generate-files(seeBUILD.mdfor details).
- Place the new CSV/XLS file inside
Requirements
- 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.
Development Workflow
Running tests
npm testBuilding the library
npm run build-allThis compiles the TypeScript sources and bundles the packaged JSON assets.
Regenerating JSON data
Drop the latest CSV/XLS export into the root
data-source/folder.Add your Geoapify API key to
src/utility/utility.ts.Run:
npm run generate-files
Running the demo
Build the project (
npm run build-all).Start the demo app:
npm run start:demo
Error Handling
query returns null when the provided country/location combination does not exist. Make sure to guard for this in your application.
Contributing
Contributions are welcome! Please fork the repository, make your changes, and submit a pull request. See BUILD.md for full data-generation instructions.
