How to Get a List of ISO 3166–1 Countries using Java

Cloudmersive
2 min readOct 25, 2022

--

ISO codes represent a standardized way of defining countries, territories and other important geographical areas. Considering the scope of ISO codes, retrieving that much information can be a real pain on a large-scale basis; thankfully, our ISO 3166–1 API has them all in one spot for your convenience. Calling this API will enumerate an exhaustive list of all available ISO 3166–1 codes, including additional information about each country on the list. Below, I’ve included the first API response from this list (Afghanistan) as an example/reference:

    {
"CountryName": "Afghanistan",
"ThreeLetterCode": "AFG",
"ISOTwoLetterCode": "AF",
"IsEuropeanUnionMember": false,
"ISOCurrencyCode": "AFN",
"CurrencySymbol": "؋",
"CurrencyEnglishName": "Afghani",
"Region": "Asia",
"Subregion": "SouthernAsia"
},

In the remainder of this article, I’ll demonstrate how you can easily use this API & structure your API call using complementary Java code examples.

The first step is to install the API client with Maven. We can do so by first adding a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next, we can wrap up installation by adding an additional reference to the dependency in pom.xml:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Finally, we can add in our imports and call the function. In order to authenticate the API call, you’ll need to provide a Cloudmersive API key in the indicated field below (you can get one by registering a free account on our website). After that, all you need to do is call the API, and you’ll receive an extensive list of ISO codes:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.AddressApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
AddressApi apiInstance = new AddressApi();
try {
CountryListResult result = apiInstance.addressCountryList();
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AddressApi#addressCountryList");
e.printStackTrace();
}

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet