Skip to content

IP geolocation

Arcjet provides IP geolocation information in two places:

  • Decisions after calling protect() include IP address analysis fields depending on your pricing plan. You can use this to customize responses. See the SDK reference for details e.g. Next.js.
  • Filter rules have access to IP address analysis. You can use this to block traffic.
FiltersDecisionFreeStarterBusiness
ip.src.accuracy_radiusip.accuracyRadius🚫
ip.src.cityip.city🚫
ip.src.continent.nameip.continentName🚫
ip.src.continentip.continent🚫
ip.src.country.nameip.countryName
ip.src.countryip.country
ip.src.latip.latitude🚫
ip.src.lonip.longitude🚫
ip.src.postal_codeip.postalCode🚫
ip.src.regionip.region🚫
ip.src.timezone.nameip.timezone🚫

The IP location fields may be undefined, but you can use various methods to check their availability:

// ... 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
}

Filter rules can be used to block traffic based on IP geolocation:

// …
rules: [
filter({
allow: ['ip.src.country eq "US"'],
mode: "LIVE",
})
],
// …
const decision = await aj.protect(req);
if (decision.isDenied()) {
// Return 403 Forbidden
}

The ip field on a decision can be used to customize the response based on IP geolocation:

// …
const decision = await aj.protect(req);
if (decision.isDenied()) {
// Handle your other configured rules here.
}
// Otherwise, customize for US:
if (decision.ip.country === "US") {
return new Response("Hello, US visitor!");
}

Filter rules can be used to block traffic based on IP geolocation:

// …
rules: [
filter({
allow: ['ip.src.country eq "US" and ip.src.region eq "California"'],
mode: "LIVE",
})
],
// …
const decision = await aj.protect(req);
if (decision.isDenied()) {
// Return 403 Forbidden
}

The ip field on a decision can be used to customize the response based on IP geolocation:

// …
const decision = await aj.protect(req);
if (decision.isDenied()) {
// Handle your other configured rules here.
}
// Otherwise, customize for California:
if (decision.ip.country === "US" && decision.ip.region === "California") {
return new Response("Go California!");
}