An API to retrieve location details of Sri Lanka, including cities, districts, and provinces. Built with Node.js, Express, and TypeScript, this project is ideal for accessing and searching geographic information for Sri Lankan locations.
Available on: https://slcities.live/api/cities
Postman Collection: LK Location Details Workspace
- Retrieve all cities, districts, and provinces in Sri Lanka
- Search cities by:
- District
- Province
- Postcode
- GPS coordinates (latitude, longitude, radius)
- Query strings (name, language)
- Fetch districts by province name
- Get summary statistics of provinces with district and city counts
- Easy-to-use REST API with OpenAPI 3.1.0 documentation
The location data for this project was sourced from madurapa's repository. You can visit the repository and download the data as SQL for your own projects.
- Node.js: Backend runtime
- Express: REST API framework
- TypeScript: Strongly typed JavaScript
- Jest: Testing framework
- Docker: Containerization
- Azure: Cloud deployment
- ESLint & Prettier: Code quality and formatting
- Node.js (>= 18.x)
- Docker (optional for containerized deployment)
- Azure CLI (optional for Azure deployment)
-
Clone the repository:
git clone https://github.com/dinethsiriwardana/Sri-Lanka-Location-Details.git cd sl-cities-api
-
Install dependencies:
npm install
-
Set up the environment:
- Create a
.env
file in the root directory with the required variables.
- Create a
-
Run the application:
- In development mode:
npm run dev
- In production mode:
npm start
- In development mode:
- Local:
http://localhost:3000/api
- Production:
https://slcities.live/api
- Legacy:
http://slcities.centralindia.azurecontainer.io:3000/api
GET /cities
: Retrieve all cities.GET /cities/district/{districtName}
: Retrieve cities by district.GET /cities/province/{provinceName}
: Retrieve cities by province.GET /cities/postcode/{postcode}
: Retrieve a city by postcode.GET /cities/search?q={searchTerm}&lang={language}
: Search for cities by name and language.GET /cities/nearby?lat={latitude}&lon={longitude}&radius={radius}
: Find cities near a GPS coordinate.
GET /provinces
: Retrieve all provinces.
GET /districts
: Retrieve all districts.GET /districts/province/{provinceId}
: Retrieve districts by province ID.GET /districts/province-name/{provinceName}?lang={language}
: Retrieve districts by province name.
GET /summary
: Get a summary of provinces with district counts and city counts.
GET /api-docs
: Interactive API documentation (Swagger UI).GET /api-spec
: Raw OpenAPI specification in JSON format.
-
Build and start the container:
docker-compose up --build
-
Access the API at
http://localhost:3000/api
. -
For Azure Container Registry deployment:
docker buildx build --platform linux/amd64 -t slcities.azurecr.io/slcities:latest . --push
- An Azure account with active subscription
- Azure CLI installed on your machine
-
Login to Azure:
az login
-
Create a Resource Group:
az group create --name sl-cities-rg --location eastus
-
Create Azure Container Registry (ACR):
az acr create --name slcitiesregistry --resource-group sl-cities-rg --sku Basic --admin-enabled true
-
Get ACR credentials:
az acr credential show --name slcitiesregistry
-
Build and push Docker image to ACR:
# Login to ACR az acr login --name slcitiesregistry # Build the image docker build -t slcitiesregistry.azurecr.io/sl-cities:latest . # Push the image to ACR docker push slcitiesregistry.azurecr.io/sl-cities:latest
-
Create an App Service Plan:
az appservice plan create --name sl-cities-plan --resource-group sl-cities-rg --is-linux --sku B1
-
Create a Web App:
az webapp create --resource-group sl-cities-rg --plan sl-cities-plan --name sl-cities --deployment-container-image-name slcitiesregistry.azurecr.io/sl-cities:latest
-
Configure the Web App to use ACR:
az webapp config container set --name sl-cities --resource-group sl-cities-rg --docker-custom-image-name slcitiesregistry.azurecr.io/sl-cities:latest --docker-registry-server-url https://slcitiesregistry.azurecr.io --docker-registry-server-user slcitiesregistry --docker-registry-server-password <password_from_step_4>
Your app will be accessible at: https://slcities.live/
- Run in Development:
npm run dev
- Build:
npm run build
- Start:
npm start
- Lint:
npm run lint
- Format:
npm run format
- Test:
npm run test
The API documentation is available in multiple formats:
- Swagger UI: Access the interactive API documentation at:
- Local:
http://localhost:3000/api-docs
- Production:
https://slcities.live/api-docs
- Local:
- OpenAPI Specification: View or download the raw OpenAPI specification at:
- Local:
http://localhost:3000/api-spec
- Production:
https://slcities.live/api-spec
- Local:
- Postman Collection: Available at the Postman workspace
The API documentation follows the OpenAPI 3.1.0 specification and provides detailed information about all endpoints, request parameters, and response schemas.
/src
/controllers # API business logic
cityController.ts
districtController.ts
provinceController.ts
homeController.ts
docController.ts
summaryController.ts
/middlewares # Express middlewares
errorHandler.ts
/routes # API route definitions
cityRoutes.ts
districtRoutes.ts
provinceRoutes.ts
homeRoutes.ts
summaryRoutes.ts
index.ts
/types # TypeScript type definitions
city.ts
/utils # Utility functions
dataService.ts
index.ts # Application entry point
/data # JSON data for locations
cities.json
/public # Static web frontend
index.html
app.js
styles.css
explorer.html
provinces.html
about.html
/css
/js
/templates
/dist # Compiled JavaScript output
docker-compose.yml
Dockerfile
openapi.yaml
package.json
tsconfig.json
azure-deployment.md
Contributions are welcome! To contribute:
- Fork the repository.
- Create a feature branch:
git checkout -b feature-name
. - Commit changes:
git commit -m "Add feature"
. - Push to the branch:
git push origin feature-name
. - Open a pull request.
This project is licensed under the MIT License.
Feel free to reach out for any questions or issues! 🎉