DEV Community

Cover image for Country Data API Free npm Worldwide
Zayed Hossain
Zayed Hossain

Posted on

Country Data API Free npm Worldwide

Are you building a global application that requires reliable and structured data about countries, states, and cities? Introducing Country Data API, a lightweight and easy-to-use npm package designed to simplify your development process. With this package, you can access country-specific details, state and city data, and much more — all in a few lines of code.

Why Use Country Data API?

  • Comprehensive Data: Access a wealth of information about countries, states, and cities in a single library.

  • Ease of Use: The API is designed with simplicity in mind, making it ideal for developers at all levels.

  • Lightweight: No bloated dependencies, ensuring quick installation and execution.

Key Features

  • Retrieve a list of all countries.

  • Get country names and details.

  • Fetch states by country name.

  • Access city data by state names.

Installation

To get started, install the package via npm:

npm install country-data-api
Enter fullscreen mode Exit fullscreen mode

How to Use the Country Data API

Below are some common use cases with code examples to help you integrate the package into your project seamlessly.

1. Get All Countries

Fetch a list of all countries with this simple method:

import { getAllCountries } from 'country-data-api';

const countries = getAllCountries();
console.log(countries);
Enter fullscreen mode Exit fullscreen mode

2. Get All Country Names

Need just the names of the countries? This method has you covered:

import { getAllCountryNames } from 'country-data-api';

const countryNames = getAllCountryNames();
console.log(countryNames);
Enter fullscreen mode Exit fullscreen mode

3. Get Country Details by Name

Retrieve detailed information about a specific country by its name:

import { getCountryDetailsByName } from 'country-data-api';

const countryDetails = getCountryDetailsByName('Bangladesh');
console.log(countryDetails);
Enter fullscreen mode Exit fullscreen mode

4. Get States by Country Name

Fetch a list of states within a specific country:

import { getStatesByCountryName } from 'country-data-api';

const statesInCountry = getStatesByCountryName('Bangladesh');
console.log(statesInCountry);
Enter fullscreen mode Exit fullscreen mode

5. Get Cities by State Names

Easily fetch cities within a state by passing a state name or an array of state names:

import { getCitiesByStateNames } from 'country-data-api';

// Get cities by a single state name
const citiesInState = getCitiesByStateNames('Dhaka');
console.log(citiesInState);

// Get cities by an array of state names
const citiesInStates = getCitiesByStateNames(['Dhaka', 'Chittagong']);
console.log(citiesInStates);
Enter fullscreen mode Exit fullscreen mode

Real-World Applications

The Country Data API is perfect for use in applications like:

  • E-commerce Platforms: Display country and state dropdowns during checkout.

  • Travel Websites: Show detailed information about destinations.

  • Educational Tools: Create geography-based learning apps.

Conclusion

The Country Data API is the ultimate tool for developers building location-aware applications. Its simplicity and powerful features make it a must-have for anyone working with geographical data.

So, what are you waiting for? Install the Country Data API and take your applications to the next level today!

Have questions or feedback? Drop them in the comments below. Let’s build something amazing together!

Top comments (2)

Collapse
 
jackfd120 profile image
JAKER HOSSAIN

Such a useful npm, Thanks for sharing your execution.

Collapse
 
zayedhossain120 profile image
Zayed Hossain

Thanks you so much.