DEV Community

Cover image for Get Instant Country Flags
⚡ Nirazan Basnet ⚡ for YoungInnovations

Posted on • Updated on

Get Instant Country Flags

While working on one of my projects, there is one feature where I need to display a specific flag depending upon the country code.

My first approach was to get the country flags' zip file and extract it locally from the images folder.
You can download the list of country flags from here.

I thought this can be easy. Then, I thought as the project was on ReactJs, I wonder there might be an API for the country flags.

Then I came across,
Country Flags - A simple API to load any country flags

So, how does it work:

HTML

<img src="https://flagcdn.com/:size/:country_code.png">
Enter fullscreen mode Exit fullscreen mode

Example

<img src="https://flagcdn.com/48x36/za.png">
Enter fullscreen mode Exit fullscreen mode

Simple Data Map Syntax in ReactJs

Code:

function CountryList() {
  const countries_code = ["au", "af", "bd", "br", "in"];

  return (
    <div className="country-list">
      <ul>
        {countries_code.map((code, index) => (
          <li key={index} className="mr-16">
            <img src={`https://flagcdn.com/48x36/${code}.png`} alt="..." />
          </li>
        ))}
      </ul>
    </div>
  );
}
);
Enter fullscreen mode Exit fullscreen mode

You can check out the demo link here


Conclusion
👏👏 By coming this far I hope you can implement this awesome country flag CDN & API on your project. So, I suggest you give it a try on your project and enjoy it!

Feel free to share your thoughts and opinions and leave me a comment if you have any problems or questions.

Till then,
Keep on Hacking, Cheers

Top comments (22)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited
const getFlagEmoji = countryCode=>String.fromCodePoint(...[...countryCode.toUpperCase()].map(x=>0x1f1a5+x.charCodeAt()))

getFlagEmoji('GB') // 🇬🇧
getFlagEmoji('JP') // 🇯🇵
getFlagEmoji('ZA') // 🇿🇦
Enter fullscreen mode Exit fullscreen mode
Collapse
 
arunmurugan78 profile image
Arun Murugan

This is Awesome!

Collapse
 
nirazanbasnet profile image
⚡ Nirazan Basnet ⚡

Thanks!!

Collapse
 
simaodeveloper profile image
Daniel Simão

🤣🤣🤣🤣🤣🤣

Collapse
 
kaspermroz profile image
Kasper Mróz

Love it! 🔥

Collapse
 
nirazanbasnet profile image
⚡ Nirazan Basnet ⚡

Thanks!!

Collapse
 
nirazanbasnet profile image
⚡ Nirazan Basnet ⚡

Great.

Collapse
 
davidkrmela profile image
David Krmela

Hi Nirazan, you mentioned Flagpedia.net as a place where you can download the archive with flags. But it also supports API access:

Examples:

There are a lot of different resolution. Maybe someone will find it useful.

Collapse
 
nirazanbasnet profile image
⚡ Nirazan Basnet ⚡

Thanks for the information :)

Collapse
 
jeremypersing profile image
Jeremy Persing

Hey, I was working on a project recently and used your post to implement flags into the project. Unfortunately, half way through the project countryflags.io went down. Because of this, I decided to make my own API, countryflagsapi.com. The API allows you to get a country by it's two letter code (ISO Alpha-2 code), along with the country's name, it's three letter code (ISO Alpha-3 code), or UN code. Thanks for the post Nirazan, and hopefully this API can help anyone that's run into trouble with countryflags.io being down.

Collapse
 
nirazanbasnet profile image
⚡ Nirazan Basnet ⚡

Thanks for the information. Definitely, I will try your api structure.

Collapse
 
tuliocalil profile image
Tulio Calil

🇧🇷

Collapse
 
banky profile image
Bankole

Nov 11th 2021 - 4:00 a.m. WAT - countryflags.io is down

Collapse
 
nirazanbasnet profile image
⚡ Nirazan Basnet ⚡

Yes, so I had add alternative solution. Hope you like it :)

Collapse
 
hgpit profile image
hgpit

Did something happen to this domain? it's been down for at least 2 days now :(

Collapse
 
nirazanbasnet profile image
⚡ Nirazan Basnet ⚡

Yes, the site is down so I had added alternative solution. Hope you like it :)

Collapse
 
mohitm15 profile image
Mohit Maroliya

I guess countryflags.io is not working now.

Collapse
 
nirazanbasnet profile image
⚡ Nirazan Basnet ⚡

Yes, the site is down so I had added alternative solution. Hope you like it :)

Collapse
 
shriji profile image
Shriji

Very neat! I made the svelte version of this!

Collapse
 
nirazanbasnet profile image
⚡ Nirazan Basnet ⚡

Great 🙂🙂

Collapse
 
jorik profile image
Jorik

Great written article! Inspired by your piece, I wrote a small function to achieve this using emoji's! dev.to/jorik/country-code-to-flag-...

Collapse
 
nirazanbasnet profile image
⚡ Nirazan Basnet ⚡

Thanks 😌