Get visitor location details from IP in worker

cf request object has some Request · Cloudflare Workers docs

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"
3 Likes