client.geo.country_code

STRING, read-only.

Available inall subroutines.

Two-letter ISO 3166-1 alpha-2 country code for the country.

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 code examples. Examples apply VCL to real-world use cases and can be deployed as they are, or adapted for your own service. See the full list of code examples for more inspiration.

Click RUN on a sample below to provision a Fastly service, execute the code on Fastly, and see how the function behaves.

Tag requests with geolocation data

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

Geofence / block access to content by region

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

Block all traffic from certain countries

Check for a country code on an incoming request, and if it's present, deny access with a 403.