@countrystatecity/phonecodes
v1.0.0
Published
Country phone/dial codes with ISO2 mapping, search, and formatting utilities
Maintainers
Readme
@countrystatecity/phonecodes
Country phone/dial codes for all 250 countries — lookup by ISO2, reverse lookup by dial code, search, and formatting utilities.
Install
npm install @countrystatecity/phonecodesQuick Start
import {
getPhonecodeByCountry,
getCountriesByDialCode,
formatWithDialCode,
} from '@countrystatecity/phonecodes';
const india = await getPhonecodeByCountry('IN');
// { iso2: 'IN', name: 'India', dialCode: '+91', phonecode: '91' }
const countries = await getCountriesByDialCode('+1');
// [{ iso2: 'US', ... }, { iso2: 'CA', ... }, ...]
const formatted = await formatWithDialCode('9876543210', 'IN');
// "+91 9876543210"API
getPhonecodes()
Returns all 250 country phonecode entries sorted by ISO2.
const all = await getPhonecodes();
// [{ iso2: 'AD', name: 'Andorra', dialCode: '+376', phonecode: '376' }, ...]getPhonecodeByCountry(iso2)
Returns the phonecode entry for a country by ISO2 code, or undefined.
await getPhonecodeByCountry('GB');
// { iso2: 'GB', name: 'United Kingdom', dialCode: '+44', phonecode: '44' }getCountriesByDialCode(dialCode)
Returns all countries sharing a dial code (useful for +1, +7, +44, etc.).
await getCountriesByDialCode('+1');
// [{ iso2: 'AG', ... }, { iso2: 'AI', ... }, { iso2: 'CA', ... }, { iso2: 'US', ... }, ...]isValidDialCode(dialCode)
Returns true if the dial code exists.
await isValidDialCode('+91'); // true
await isValidDialCode('+9999'); // falsesearchPhonecodes(query)
Searches by country name, ISO2, or dial code (case-insensitive).
await searchPhonecodes('india');
// [{ iso2: 'IN', name: 'India', dialCode: '+91', phonecode: '91' }]
await searchPhonecodes('+44');
// [{ iso2: 'GB', name: 'United Kingdom', dialCode: '+44', phonecode: '44' }]getDialCode(iso2)
Shorthand — returns just the dial code string.
await getDialCode('IN'); // "+91"getPhonecode(iso2)
Returns the raw phonecode without the + prefix.
await getPhonecode('IN'); // "91"formatWithDialCode(localNumber, iso2)
Prepends the country dial code to a local phone number.
await formatWithDialCode('9876543210', 'IN'); // "+91 9876543210"Data Coverage
- 250 countries
- All entries include
iso2,name,dialCode(with+), andphonecode(raw) - Sourced from dr5hn/countries-states-cities-database, updated weekly
