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">
Example
<img src="https://flagcdn.com/48x36/za.png">
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>
);
}
);
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)
This is Awesome!
Thanks!!
🤣🤣🤣🤣🤣🤣
Love it! 🔥
Thanks!!
Great.
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.
Thanks for the information :)
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.
Thanks for the information. Definitely, I will try your api structure.
🇧🇷
Nov 11th 2021 - 4:00 a.m. WAT - countryflags.io is down
Yes, so I had add alternative solution. Hope you like it :)
Did something happen to this domain? it's been down for at least 2 days now :(
Yes, the site is down so I had added alternative solution. Hope you like it :)
I guess countryflags.io is not working now.
Yes, the site is down so I had added alternative solution. Hope you like it :)
Very neat! I made the svelte version of this!
Great 🙂🙂
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-...
Thanks 😌