Request.cf is undefined

The documentation says:

In addition to the information available on the Request object, such as headers, Cloudflare provides additional attributes of the request using the request.cf object.

Attributes available on request.cf :

  • country : the two letter country code on the request (this is the same value as the one provided by the CF-IPCountry header)
  • colo : the three letter airport code of the colo the request hit.

When I try to console.log request.cf.colo and request.cf.country it returns undefined.

some of the headers not available in the debug mode only in production, I use it like so(saw it somewhere in the docs):
(request.cf || {}).colo

1 Like

I wish that was mentioned in the documentation! Thanks for the info

4 Likes

also sometimes in rare cases country can return undefined so prepare for that

1 Like

Another solution is to just write it like this request?.cf?.colo
That way it shows undefined whenever it is not available

2 Likes

Try the following (I have not tried it!)

const country = request.headers.get(‘cf-ipcountry’)
console.log('Country: '+country);

const city = request.cf.city
console.log('City: '+city);

The same for values below:

const connectingip = request.headers.get(‘cf-connecting-ip’)
const city = request.cf.city
const timezone = request.cf.timezone
const region = request.cf.region
const regionCode = request.cf.regionCode
const asOrganization = request.cf.asOrganization
const postalCode = request.cf.postalCode