Skip to content

Arcjet IP geolocation

Every decision provided by Arcjet includes IP address analysis. You can use this to customize your response based on the IP location.

Available fields

  • Latitude and longitude. Pro
  • Postal code e.g. 94104. Pro
  • City e.g. San Francisco. Pro
  • Region e.g. California. Pro
  • Country e.g. US. Free Pro
  • Country name e.g. United States. Free Pro
  • Continent e.g. NA. Pro
  • Continent name e.g. North America. Pro

Note that the availability of these fields depends on your pricing plan, indicated by the badge next to each field.

Checking for data

The IP location fields may be undefined, but you can use various methods to check their availability. See the SDK reference for details.

Examples

// ... imports, client configuration, etc
// See https://docs.arcjet.com/get-started
const decision = await aj.protect(req);
if (decision.ip.hasCity() && decision.ip.city === "San Francisco") {
// Return a custom response for San Francisco
}
if (decision.ip.hasRegion() && decision.ip.region === "California") {
// Return a custom response for California
}
if (decision.ip.hasCountry() && decision.ip.country === "US") {
// Return a custom response for the United States
}
if (decision.ip.hasContinent() && decision.ip.continent === "NA") {
// Return a custom response for North America
}

Block all countries except US

// ... imports, client configuration, etc
// See https://docs.arcjet.com/get-started
const decision = await aj.protect(req);
if (decision.ip.hasCountry() && decision.ip.country !== "US") {
// Return 403 Forbidden
}

Block all countries except US, UK, Japan

// ... imports, client configuration, etc
// See https://docs.arcjet.com/get-started
const decision = await aj.protect(req);
if (
decision.ip.hasCountry() &&
!["US", "UK", "JP"].includes(decision.ip.country)
) {
// Return 403 Forbidden
}