Flag API
Country flags as SVG, PNG, WebP or JPEG, in any size. URLs follow the flagcdn.com scheme, so pointing existing code here is a hostname swap.
Quick start
Build a URL from a size, a two-letter country code and a format. No key or sign-up is needed.
{origin}/w320/gb.png
In a page, add a second size for high-density screens:
<img src="{origin}/w160/gb.png"
srcset="{origin}/w320/gb.png 2x"
width="160" alt="United Kingdom">
From a script:
curl -o gb.png {origin}/w320/gb.png
curl {origin}/en/codes.json
URL builder
Endpoints
| GET | Returns |
|---|---|
/{code}.svg | The vector flag, e.g. /gb.svg. |
/w{width}/{code}.{format} | Fixed width; height follows the flag's own aspect ratio. /w320/gb.png |
/h{height}/{code}.{format} | Fixed height; width follows the aspect ratio. /h120/gb.webp |
/{width}x{height}/{code}.{format} | Exact box, flag fitted inside and padded. /64x48/gb.png |
/{lang}/codes.json | Every code with its name in that language. /en/codes.json |
/sizes.json | Preset sizes, formats and the size limits of this instance. |
/healthz | Liveness check with flag count and cache usage. |
/docs | This page. |
Sizes
Fixed width (w320) and fixed height (h120) keep each flag's real proportions, so the other dimension varies by country: /w320/gb.png is 320 × 160, /w320/ch.png is 320 × 320. Use fixed height for a row of flags that should line up.
Fixed box (64x48) always returns exactly that many pixels. The flag is scaled to fit inside and centred; the leftover space is transparent (white for JPEG). Use it when every image must be the same size, such as icons in a list.
Presets, matching flagcdn.com:
Width
Height
Fixed box (4:3)
Formats
| Extension | Content type | Notes |
|---|---|---|
.svg | image/svg+xml | Vector original, sharp at any size. No size segment in the URL. Sent gzipped when the client accepts it. |
.png | image/png | Lossless, with transparency. The safe default. |
.webp | image/webp | Lossless, with transparency, usually smaller than PNG. |
.jpg / .jpeg | image/jpeg | No transparency: padding and non-rectangular flags (Nepal) sit on white. |
Country codes
Codes are lower-case ISO 3166-1 alpha-2 (gb, us, de), plus eu, xk (Kosovo) and the UK nations gb-eng, gb-sct, gb-wls and gb-nir. The full list is browsable below.
/{lang}/codes.json returns a single object mapping each code to its name. {lang} is a language tag such as en, de, fr, ja or pt-br; /codes.json is the English list.
{ "ad": "Andorra", "ae": "United Arab Emirates", … }
Responses and errors
| Status | When |
|---|---|
200 | The image or JSON you asked for. |
304 | Your cached copy is still current (If-None-Match matched the ETag). |
400 | The size is malformed or over the limit, or the format is not supported. |
404 | There is no flag for that code, no names for that language, or no such path. |
405 | Any method other than GET, HEAD or OPTIONS. |
Errors are JSON with a single error message:
{ "error": "No flag for code \"zz\". See /en/codes.json for the full list." }
Caching and CORS
Every image and codes list carries an ETag and a Cache-Control header, so browsers and proxies can keep copies and revalidate cheaply. URLs are stable: a flag that changes keeps its URL and gets a new ETag.
All responses send Access-Control-Allow-Origin: *, so flags can be fetched from scripts or drawn to a canvas from any internal site.