This repository provides a JSON file containing standardized country code information, including name, international calling code, and ISO codes.
The file follows this format:
{
"AF": {
"name": "Afghanistan",
"callingCode": 93,
"isoA2": "AF",
"isoA3": "AFG",
"isoNum": 4
},
"AL": {
"name": "Albania",
"callingCode": 355,
"isoA2": "AL",
"isoA3": "ALB",
"isoNum": 8
}
}const fs = require('fs');
fs.readFile('countries.json', 'utf8', (err, data) => {
if (err) throw err;
const countries = JSON.parse(data);
console.log(countries["AF"]); // Output: Data about Afghanistan
});import json
with open('countries.json', 'r', encoding='utf-8') as file:
countries = json.load(file)
print(countries["AF"]) # Output: Data about AfghanistanIn addition to the JSON file, this repository also provides a simple API to access country code information.
- Endpoint:
/api/countries - Method:
GET - Description: Returns a list of all countries with their details (name, ISO codes, dialing code).
- Endpoint:
/api/countries/{isoCode} - Method:
GET - Parameters:
isoCode: The 2-letter ISO code of the country (e.g.,USfor the United States).
- Description: Returns the details of a country by its ISO code.
Example Request:
GET /api/countries/USExample Response:
{
"name": "United States",
"callingCode": 1,
"isoA2": "US",
"isoA3": "USA",
"isoNum": 840
}- Endpoint:
/api/countries/search - Method:
GET - Parameters:
name: A query string to filter country names (e.g.,anto find all countries with "an" in their name).
- Description: Returns a list of countries whose names contain the provided query string.
Example Request:
GET /api/countries/search?name=anExample Response:
[
{
"name": "Afghanistan",
"callingCode": 93,
"isoA2": "AF",
"isoA3": "AFG",
"isoNum": 4
},
{
"name": "Albania",
"callingCode": 355,
"isoA2": "AL",
"isoA3": "ALB",
"isoNum": 8
}
]The countries.json file follows this format:
{
"AF": {
"name": "Afghanistan",
"callingCode": 93,
"isoA2": "AF",
"isoA3": "AFG",
"isoNum": 4
},
"US": {
"name": "United States",
"callingCode": 1,
"isoA2": "US",
"isoA3": "USA",
"isoNum": 840
}
}-
Clone this repository:
git clone https://github.com/gregdalzotto/countries.git cd countries -
Install the dependencies:
npm init -y npm install express
-
Create a
server.jsfile in the root of your project with the following code:const express = require('express'); const fs = require('fs'); const path = require('path'); const app = express(); const port = 3000; // load data from countries.json const countriesData = JSON.parse(fs.readFileSync(path.join(__dirname, 'countries.json'), 'utf8')); // Endpoint get all countries app.get('/api/countries', (req, res) => { res.json(countriesData); }); // Endpoint get ISO code app.get('/api/countries/:isoCode', (req, res) => { const isoCode = req.params.isoCode.toUpperCase(); const country = countriesData[isoCode]; if (country) { res.json(country); } else { res.status(404).json({ message: 'Country not found' }); } }); // Endpoint get by name (filter) app.get('/api/countries/search', (req, res) => { const name = req.query.name ? req.query.name.toLowerCase() : ''; const filteredCountries = Object.values(countriesData).filter(country => country.name.toLowerCase().includes(name) ); res.json(filteredCountries); }); // Start Server app.listen(port, () => { console.log(`Server is running on http://localhost:${port}`); });
-
Start the server:
node server.js
-
Visit
http://localhost:3000in your browser or use a tool like Postman to make requests to the API.
This project is licensed under the MIT License - see the LICENSE file for details.
Feel free to contribute! If you find an error or want to add more data, open an issue or submit a pull request. 😊