Detecting State

States are first-level administrative divisions of a country. Use stateCode and stateName for this purpose. Note that smaller countries such as Singapore will return empty string for state identifiers.

let response = {
    "locationData": {
        "countryCode": "US",
        "stateName":"Massachusetts",
        "stateCode":"MA",
        "cityName": "Boston, MA",
        // ... (other fields)
    }
};

if (response.locationData.stateCode === "MA") {
    console.log("The user is from the state of ", response.locationData.stateName)
}
package main

import "fmt"

var response = map[string]map[string]string{
	"locationData": {
		"countryCode": "US",
    "stateName": "Massachusetts",
    "stateCode": "MA",
		"cityName": "Boston, MA",
	},
}

func main() {
	if response["locationData"]["stateCode"] == "MA" {
		fmt.Println("The user is from the state of", response["locationData"]["stateName"])
	}
}
# Build API request
url = f"https://ep.api.getfastah.com/whereis/v1/json/{ip}"
headers = {"Fastah-Key": FASTAH_API_KEY}
response = requests.get(url, headers=headers)
response = response.json()

if response['locationData']['countryCode'] == "US":
    print("The user is from the state of ", response['locationData']['stateName'])

Identifying U.S. Territories

US territories and possessions have their own ISO 3166-1 alpha-2 country codes. Check membership against a predefined list to identify all US jurisdictions.

// List of ISO2 codes for all US jurisdictions
const usJurisdictionsISO2 = ['US', 'PR', 'GU', 'AS', 'MP', 'VI', 'UM', 'FM', 'MH', 'PW'];
// Add 'IO' (Diego Garcia) to include British Indian Ocean Territory

let response = {
    // ... (other fields)
    locationData: {
        countryCode: 'US',
        // ... (other fields)
    }
};

if (usJurisdictionsISO2.includes(response.locationData.countryCode)) {
    console.log("The user is visiting from a US jurisdiction");
}
package main

import "fmt"

// List of ISO2 codes for all US jurisdictions
var usJurisdictionsISO2 = []string{"US", "PR", "GU", "AS", "MP", "VI", "UM", "FM", "MH", "PW"}
// Add "IO" (Diego Garcia) to include British Indian Ocean Territory

var response = map[string]interface{}{
    // ... (other fields)
    "locationData": map[string]interface{}{
        "countryCode": "US",
        // ... (other fields)
    },
}

func main() {
    countryCode := response["locationData"].(map[string]interface{})["countryCode"]
    for _, jurisdiction := range usJurisdictionsISO2 {
        if countryCode == jurisdiction {
            fmt.Println("The user is visiting from a US jurisdiction")
            break
        }
    }
}
# List of ISO2 codes for all US jurisdictions. Add 'IO' for Diego Garcia.
us_jurisdictions_iso2 = ['US', 'PR', 'GU', 'AS', 'MP', 'VI', 'UM', 'FM', 'MH', 'PW']

# Build API request
url = f"https://ep.api.getfastah.com/whereis/v1/json/{ip}"
headers = {"Fastah-Key": FASTAH_API_KEY}
response = requests.get(url, headers=headers)
response = response.json()

if response['locationData']['countryCode'] in us_jurisdictions_iso2:
    print("The user is visiting from a US jurisdiction")