How to Get the Region, Subregion and Continent of an Input Country using Java

Cloudmersive
2 min readOct 26, 2022

--

If your application is utilizing or storing country names (for any reason), it’s very useful to have your own way of retrieving information about those countries without having to rely on users entering that data themselves. When it comes to country metadata, we’re talking about a pretty big field of information — and thankfully, our Address Validation APIs can be used to access whatever information you need on a large-scale or limited basis. In this article, I’m going to highlight our Get Region & Subregion API which will quickly retrieve the region & subregion that any given country belongs to. Below, I’ve included an example JSON response for the input country “Ireland”:

{
"Successful": true,
"CountryFullName": "Ireland",
"ISOTwoLetterCode": "IE",
"FIPSTwoLetterCode": "EI",
"ThreeLetterCode": "IRL",
"IsEuropeanUnionMember": true,
"Timezones": [],
"ISOCurrencyCode": "EUR",
"CurrencySymbol": "€",
"CurrencyEnglishName": "Euro",
"Region": "Europe",
"Subregion": "NorthernEurope"
}

As you can see, the API response identified Ireland as being located in Europe (region) first, and Northern Europe (subregion) second. In addition to that, it supplied metadata that we didn’t ask for but can use if we want to, including ISO country codes, currency names and codes, and more. In the remaining portion of this article, I’ll demonstrate how you can easily take advantage of this API for free using complementary, ready-to-run Java code examples to structure your API call.

Our first step is to install the API client with Maven. Let’s begin by adding a reference to the repository in pom.xml:

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

Next, let’s add a 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>

With our library compiled, let’s now add the imports and call the function. Before we do so, however, we’ll need to get an API key to authenticate the service — and we can do that for free by registering a free account on the Cloudmersive website (free accounts come with a limit of 800 API calls per month & zero additional commitments). Once we have our API key, we can include that in the appropriate field below:

// 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();
ValidateCountryRequest input = new ValidateCountryRequest(); // ValidateCountryRequest | Input request
try {
ValidateCountryResponse result = apiInstance.addressGetCountryRegion(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AddressApi#addressGetCountryRegion");
e.printStackTrace();
}

When we parse our input request, we’ll want to do so using the below JSON format:

{
"RawCountryInput": "string"
}

And after that, we’re all done! Nice & easy.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet