Get visitor location details from IP in worker

The default worker request headers have cf-ipcountry which gives me the country code of the visitor. There’s also cf-connecting-ip which gives me their IP.

But can I get more detailed information like region, city or lat / lon?

Otherwise, I’d have to make another API call to get more detailed info from the IP address, which would significantly up the latency.

Other than the Country header, there’s really nothing. There might be some documentation that infers that there’s more localized information from Cloudflare. If it ever really existed, it’s been deprecated.

EDIT: Awesome! @eva2000 found the docs I couldn’t locate.

2 Likes

Thanks!

Looks like Fastly lets you access precise location data via VCL:

https://docs.fastly.com/vcl/geolocation/

And Cloudflare seems to almost be getting there:

Would be very useful if these fields were accessible from the worker too.

That looks like the same country code as the header method.

Yeah! I just meant it would be very useful if Cloudflare had the fields that the Fastly page lists, and exposed them to workers.

1 Like

cf request object has some https://developers.cloudflare.com/workers/reference/apis/request/

The cf Object

In addition to the properties on the standard Request object, you can use a request.cf object to control how Cloudflare features are applied as well as other custom information provided by Cloudflare.

Note: Currently, settings in the cf object cannot be tested in the playground.

Special information from an incoming request to help with your app’s logic. All plans have access to:

  • asn : ASN of the incoming request. (e.g. 395747 )
  • colo : The three-letter airport code of the data center that the request hit. (e.g. "DFW" )
  • weight: The browser-requested weight for the HTTP/2 prioritization.
  • exclusive: The browser-requested HTTP/2 exclusive flag (1 for Chromium-based browsers, 0 for others).
  • group: HTTP/2 stream ID for the request group (only non-zero for Firefox).
  • group-weight : HTTP/2 weight for the request group (only non-zero for Firefox).
  • tlsCipher : The cipher for the connection to Cloudflare. (e.g. "AEAD-AES128-GCM-SHA256" )
  • country : Country of the incoming request. The two-letter country code in the request. This is the same value as that provided in the CF-IPCountry header. (e.g. "US" )
  • tlsClientAuth : Only set when using Cloudflare Access. Object with the following properties: certIssuerDNLegacy , certIssuerDN , certIssuerDNRFC2253 , certSubjectDNLegacy , certVerified , certNotAfter , certSubjectDN , certFingerprintSHA1 , certNotBefore , certSerial , certPresented , certSubjectDNRFC2253
  • tlsVersion : The TLS version of the connection to Cloudflare (e.g. TLSv1.3 )

Business and Enterprise scripts have access to:

  • requestPriority : The browser-requested prioritization information in the request object. (e.g. “weight=192;exclusive=0;group=3;group-weight=127” )
  • city : City of the incoming request. (e.g. "Austin" )
  • continent : Continent of the incoming request. (e.g. "NA" )
  • httpProtocol : HTTP Protocol (e.g. "HTTP/2" )
  • latitude : Latitude of the incoming request. (e.g. "30.27130" )
  • longitude : Longitude of the incoming request. (e.g. "-97.74260" )
  • postalCode : PostalCode of the incoming request. (e.g. "78701" )
  • region : If known, the ISO 3166-2 name for the first level region associated with the IP address of the incoming request. If not known, this is an empty string. (e.g. "Texas" )
  • regionCode : If known, the ISO 3166-2 code for the first level region associated with the IP address of the incoming request. 1 If not known, this is an empty string. (e.g. "TX" )
  • timezone : Timezone of the incoming request. (e.g. "America/Chicago"
2 Likes

Great, this is what I was looking for, thank you!

1 Like