country-codes-flags
v1.26.4
Published
**country-codes-flags** is a backend utility package that provides a **complete dataset of over 200 countries** with accurate details — including:
Maintainers
Readme
🌍 country-codes-flags
country-codes-flags is a backend utility package that provides a complete dataset of over 200 countries with accurate hierarchical details — including:
- 🏳️ Country Name
- 🔤 ISO Short Code (Alpha-2 / Alpha-3)
- ☎️ Dial Code
- 🏁 Flag Image (Local SVG File)
- 🗺️ States / Provinces
- 🏙️ Districts / Cities
This package is built for Node.js backends, offering an easy way to access standardized country, state, and city data — without maintaining them manually.
🚀 Powered By
This package is proudly powered by Petabyte Innovations 💡
📦 Installation
Install using npm:
npm install country-codes-flags📊 Data Structure
The package follows a nested hierarchical structure:
Country → State → District (City)Example Structure:
{
"id": 1,
"name": "Afghanistan",
"short_name": "AFG",
"status": "Country",
"dial_code": "+93",
"max_nsn_length": 9,
"flags_icon": "flags/af.svg",
"national_language": "Dari",
"states": [
{
"name": "Badakhshan",
"districts": ["Argo", "Baharak", "Darayim"]
}
]
}⚙️ Setup in Express
Step-1 :- Making Flags Image Files Accessible
import express from "express";
const app = express();
app.use("/flags", express.static("node_modules/country-codes-flags/flags"));
app.listen(5000, () => {
console.log("Server running on port 5000");
});Step-2 :- 🌐 Access the Flags
http://localhost:5000/flags/in.svg🧩 Step 3:- Create an API and Use Package Functions
import express from "express";
import country from "country-codes-flags";
const app = express();
app.use("/flags", express.static("node_modules/country-codes-flags/flags"));
app.get("/getAllCountries", (req, res) => {
let allcountries = country.getAllCountriesInfo();
res.send({
allcountries,
apiurl: "http://localhost:8000/",
});
});
app.listen(8000, () => {
console.log("✅ Server running on http://localhost:8000");
});📦 API Response Example
{
"name": "India",
"short_name": "IND",
"dial_code": "+91",
"states": [
{
"name": "Rajasthan",
"districts": ["Jaipur", "Jodhpur", "Udaipur"]
}
]
}🖼️ Step 4: Display Flags in Your Frontend
HTML
<img src="http://localhost:8000/flags/in.svg" alt="India Flag" width="40" />JSX
<img src={`${apiurl}flags/${country.flags_icon}`} alt={country.name} width={40} />Vue
<img :src="`${apiurl}flags/${country.flags_icon}`" :alt="country.name" width="40" />⚡ Methods & Example
1. Get All Countries Info
const country = require("country-codes-flags");
const allCountries = country.getAllCountriesInfo();
console.log(allCountries);2. Get Country Info
country.getCountryInfo("India");
country.getCountryInfo("IND");
country.getCountryInfo("+91");
country.getCountryInfo(100);3. Get All Country Names
country.getCountryNames();4. Get Single Country Name
country.getCountryName("India");5. Get All Country Short Names
country.getCountryShortNames();6. Get Single Country Short Name
country.getCountryShortName("India");7. Get All Country Dial Codes
country.getCountryDialCodes();8. Get Single Country Dial Code
country.getCountryDialCode("India");9. Get States of a Country 🗺️
const states = country.getStates("India");
console.log(states);10. Get Districts / Cities 🏙️
const districts = country.getDistricts("India", "Rajasthan");
console.log(districts);🚀 Features
- ✅ Country + State + City hierarchy
- ✅ No external APIs required
- ✅ Fast and lightweight
- ✅ Easy to integrate
- ✅ Backend optimized
📌 Use Cases
- 🌐 Country → State → City dropdowns
- 📱 User registration forms
- 🛒 E-commerce shipping systems
- 📊 Geo-based analytics
📁 Package Structure
country-codes-flags/
│
├── data/
│ └── countries.json
│
├── flags/
│ └── *.svg
│
├── index.js
└── package.json👨💻 Author
Made with ❤️ by Petabyte Innovations
📄 License
MIT License
