client.geo.country_code

STRING, read-only.

Available in all subroutines.

Two-letter ISO 3166-1 alpha-2 country code for the country. The US country code is returned for regions associated with overseas United States military bases.

These values include subdivisions which are assigned their own country codes in ISO 3166-1. For example, subdivisions NO-21 and NO-22 are presented with the country code SJ for Svalbard and the Jan Mayen Islands.

See client.geo.country_code3 for the corresponding alpha-3 country code.

Example

The following VCL fragment uses a two-letter country code to construct an emoji flag from its corresponding Unicode regional indicator symbols:

table unicode_ri {
"A": "%u{1F1E6}", "B": "%u{1F1E7}", "C": "%u{1F1E8}", "D": "%u{1F1E9}",
"E": "%u{1F1EA}", "F": "%u{1F1EB}", "G": "%u{1F1EC}", "H": "%u{1F1ED}",
"I": "%u{1F1EE}", "J": "%u{1F1EF}", "K": "%u{1F1F0}", "L": "%u{1F1F1}",
"M": "%u{1F1F2}", "N": "%u{1F1F3}", "O": "%u{1F1F4}", "P": "%u{1F1F5}",
"Q": "%u{1F1F6}", "R": "%u{1F1F7}", "S": "%u{1F1F8}", "T": "%u{1F1F9}",
"U": "%u{1F1FA}", "V": "%u{1F1FB}", "W": "%u{1F1FC}", "X": "%u{1F1FD}",
"Y": "%u{1F1FE}", "Z": "%u{1F1FF}"
}
set resp.http.X-flag = table.lookup(unicode_ri, substr(client.geo.country_code, 0, 1))
table.lookup(unicode_ri, substr(client.geo.country_code, 1, 1));

For example, the country code SE will produce 🇸🇪 (the Swedish flag).

Try it out

client.geo.country_code is used in the following solution recipes, which show real world use cases. Click RUN to provision a Fastly service, execute the code on Fastly, and see how the function behaves.

Decorating origin requests with GeoIP

Add GeoIP data about the client browser as extra headers in any requests from Fastly to your origin.

Geofencing / geoblocking content by region

Group countries to cache content by custom regions or reject requests from some regions entirely.

User contributed notes

We welcome comments that add use cases, ideas, tips, and caveats. All comments will be moderated before publication. To post support questions, visit our support center and we'll find you the help you need.