Generate a static, versioned JSON API for countries, states and cities. The files can be served from a CDN or the included Docker image, with no runtime database. This repository produces the data under dist/api/v2/ and provides a separate optional fetch client.
Node.js 22 or newer and npm are required. The checked-in db/countries.json, db/states.json, and db/cities.json provide the source snapshot.
npm install
npm run build
npm run verify
npm test
npm run serveOpen http://127.0.0.1:3000 for the site, /playground/index.html for the cascading location picker and search playground, and /docs/index.html for the API guides. The site also works under a deployment subpath. npm run build:docs regenerates the checked-in Markdown documentation; an ordinary build leaves it alone. npm run clean removes the generated v2 tree only; npm run clean:all removes the whole local dist/ directory, including legacy or stale build output.
To deploy the data separately, upload the contents of dist/api/v2/ beneath your host's api/v2/ path. Configure the host's CORS, caching, and content types for your application. The included website expects the API at the same deployment root. Static hosts must support the large number of city files.
Paths are relative to your api/v2/ base. Use lowercase country codes in paths; ISO codes in JSON are uppercase. Static paths are case-sensitive. State paths use the path_key supplied by the selected state record, including id-{numeric-id} fallbacks when the source subdivision code is unavailable or unsafe.
| Path | Data |
|---|---|
countries.json |
All countries. |
countries/{iso2}.json |
One country. |
countries/{iso2}/states.json |
All states in a country. |
countries/{iso2}/states/{state}.json |
One state. |
countries/{iso2}/states/{state}/cities.json |
City summaries for one state. |
countries/{iso2}/cities/index.json |
Index of city-summary pages for a country. |
countries/{iso2}/cities/{page}.json |
Numbered city-summary page, starting at 1. |
cities/{id}.json |
Full city record by numeric source ID. |
states/all.json |
All states. |
search/countries.json, search/states.json |
Complete country and state search records. |
search/cities/index.json |
Global index of country city-page indexes. |
api-info.json, documentation.json, manifest.json, build-report.json |
Discovery, checksums and source validation. |
Region, state-type and timezone groupings remain available where the source data supplies those values. Known country and state collection files exist even when empty; an empty country city index has no page entries. Unknown files return HTTP 404.
Resource and list responses have { "data": ..., "meta": ... } with type, count, api_version: "v2" and generated_at metadata; discovery catalogs, the manifest and the build report have their own top-level structures. Country and state lists use the same record shape as details. City lists contain summaries; cities/{id}.json adds coordinates, timezone and wikidata_id. Coordinates are numeric or null, missing scalars are null, and city timezones are not inferred from their state. City, state and country IDs identify records within a snapshot and may change when upstream data is updated.
For country search, load the country index and follow every API-relative filename in its data array. The index metadata reports total_cities, page_size, and country_code; pages contain at most 5,000 records by default. The global city index links to every country index. Filter the summaries in your app: the static server does not perform searches from query strings.
const base = new URL("https://your-host.example/api/v2/");
async function read(path) {
const response = await fetch(new URL(path, base));
if (!response.ok) throw new Error(`Request failed: ${response.status}`);
return (await response.json()).data;
}
const states = await read("countries/br/states.json");
const state = states.find(item => item.name === "São Paulo");
const cities = state ? await read(`countries/br/states/${state.path_key}/cities.json`) : [];
const index = await read("countries/br/cities/index.json");
const pages = await Promise.all(index.map(page => read(page.filename)));
const query = "Sao Paulo".toLowerCase().normalize("NFD").replace(/\p{M}/gu, "");
const matches = pages.flat().filter(city => city.name_normalized.includes(query));
console.log(cities, matches);This normalization folds combining marks; it is not full transliteration. Read the endpoint reference and data structure for more detail.
geo-data-apiis the data and generator package. Its generated v2 files can also be hosted directly.geo-data-api-clientis a small, dependency-free CommonJS fetch client with TypeScript declarations. Its methods return unwrapped data; unknown single resources returnnullon 404, while missing collection files and other HTTP errors reject.
The two packages have separate release lifecycles. The repository changes do not publish either package: a maintainer must publish each intended version before it is available through npm or a package CDN. Pin an actual published version when consuming one; inspect the package contents before release with npm run check:package and npm pack --dry-run.
The data package contains more than 150,000 individual city files, so installing it can take several minutes and use substantial disk space. The client package avoids that installation by fetching the files an application needs from a hosted API.
The data package does not include the original db/ source snapshot. To use its generator after installation, supply your own directory containing countries.json, states.json, and cities.json:
npx geo-data-api --input ./my-dataset --output ./generated-apiAfter publishing and installing the client, for example:
const { createGeoClient } = require("geo-data-api-client");
async function main() {
const client = createGeoClient({ baseUrl: "https://your-host.example/api/v2/" });
const countries = await client.getCountries();
const states = await client.getStates("BR");
const cities = states.length ? await client.getCities("BR", states[0].path_key) : [];
const matches = await client.searchCities("BR", "Sao Paulo");
const city = await client.getCity(57995); // null when this ID is absent
console.log(countries, cities, matches, city);
}
main().catch(console.error);The client also exposes getCountry(iso2) and getState(iso2, state). Calling getCities(iso2) or searching without a state follows every country page.
docker compose -f compose.yaml up --build -dThe local compose.yaml exposes the service on 127.0.0.1:3000 by default. Set GEO_DATA_HOST_PORT to choose a different host port; the container still listens on port 3000. Stop it with docker compose -f compose.yaml down. The image builds the dataset and serves the website and API as a non-root user. Rebuild the image after changing the source data.
For Dokploy, use the Dokploy Compose file, select the geo-data-api service and point its domain at container port 3000. See the step-by-step Dokploy deployment guide for DNS, HTTPS, updates and troubleshooting.
The included server and deployment headers require revalidation for mutable /api/v2/ URLs. For immutable CDN caching, use an exact published package version. npm run build:compress can generate .gz and .br JSON siblings; a host must be configured to negotiate those files because the included Node server does not select them automatically.
The image starts one Node process per available CPU core with WEB_CONCURRENCY=auto. Workers share the listening port, so concurrent requests are spread across cores instead of one event loop. Override it when you need a different number of processes:
# Local preview with four workers
WEB_CONCURRENCY=4 docker compose -f compose.yaml up --build -d
# Single process (the default outside Docker)
WEB_CONCURRENCY=1 npm run serveWEB_CONCURRENCY accepts auto or an integer from 1 to 128. Each worker is a separate Node process using roughly 50–80 MB of memory, so cap the value on small containers (for example WEB_CONCURRENCY=2). Docker reports the container's CPU quota through auto, but a cap keeps memory predictable when the host has many cores. docker compose down sends SIGTERM; workers finish in-flight requests and exit before the 20-second grace period.
Clustering applies when you run node src/server.js directly (including the container command); requiring the file from another script always starts one server. For the highest static-file throughput, put nginx, Caddy, or a CDN in front of the container — they serve pre-generated JSON with sendfile and offload TLS while the Node workers handle what the proxy passes through.
The optional npm run deploy:s3 command does not delete remote files. Use a new versioned S3 prefix for each snapshot, or separately reconcile removed files and compressed variants before switching traffic; otherwise older records can remain available.
The checked-in snapshot is derived from the Countries States Cities Database release v5.2.0-export.1 (published 2026-11-03). That database is licensed under the Open Database License (ODbL) v1.0, which requires attribution and share-alike for derivative databases. The required credit and the exact changes made for this API are documented in ATTRIBUTION.md; upstream source notices are in NOTICE-SOURCES.txt and NOTICE-Unicode-3.0.txt. The application code remains under the MIT License.
Run npm run data:import to download the latest upstream release and rebuild db/. The importer keeps the fields the v2 model uses, validates record relationships, and reports any records it had to skip. Updates are deliberate: import, set RELEASE_TIMESTAMP in src/config.js (or SOURCE_DATE_EPOCH), build, verify, and review the resulting manifest before publishing or deploying.
meta.generated_at is the stable dataset release time, not the wall-clock time of each build. The checked-in snapshot defaults to 2026-11-03T08:18:19.000Z; set SOURCE_DATE_EPOCH to a Unix timestamp in seconds when preparing a different release. With the same source, generator, runtime, options and release timestamp, output bytes are reproducible. The manifest records the source hash and release timestamp and checksums the output files; build-report.json records source validation. Builds stage and verify the generated output before replacement. npm run verify also enforces a 25 MiB manifest budget by default and accepts --max-manifest-bytes when a host needs a different limit; country city pages can be bounded with --max-search-shard-bytes.