Skip to content

k.net.IP ​

Current requesting IP and IP geographic information

Execution ContextPrerequisitesTypical Use
KScript API, Page / View server scriptsmyIP reads inbound request context; property name IP is uppercaseReading visitor IP, country/city geolocation, and regional checks

k.net.IP reads the client IP of the current inbound request and looks up city or country geolocation for any IP address. The property name is IP (case-sensitive).

Quick Start ​

js
k.api.get("geo", () => {
  const clientIp = k.net.IP.myIP
  const info = k.net.IP.getCountry("8.8.8.8")
  return {
    clientIp,
    countryCode: info && info.countryCode ? info.countryCode : null
  }
})

Quick Reference ​

Method / PropertyDescriptionReturns
myIPClient IP of the current inbound requeststring
getCity(ip)Look up city-level geolocation for an IP (online server mode only; otherwise falls back to country)IPInfo | null
getCountry(ip)Look up country-level geolocation for an IP (queries country DB first; never sets city on hit)IPInfo | null
getCityOrCountry(ip)Performs the same city-level geolocation lookup as getCity(ip)IPInfo | null

myIP ​

The client IP of the current request (consistent with the k.request context).

Parameters: None.

Returns: string. The client IP of the current inbound request.

js
k.api.get("whoami", () => {
  return { ip: k.net.IP.myIP }
})

getCity() / getCountry() / getCityOrCountry() ​

Query geographical information based on an IP address.

MethodDescription
getCity(ip)City-level geolocation
getCountry(ip)Country-level geolocation
getCityOrCountry(ip)Performs the same city-level geolocation lookup as getCity(ip)
ParameterTypeRequiredDescription
ipstringYesIP address to query

Returns: IPInfo | null. Returns null only when ip cannot be parsed as a valid IP address; valid IPs that have no match in the database still return a non-null IPInfo object with null string fields, 0 coordinates, and iPUserType set to "Unknown". Check info && info.countryCode rather than just info.

City database availability and fallback behavior

  • City-level lookup is only active when AppSettings.IsOnlineServer is true: otherwise getCity(ip) and getCityOrCountry(ip) skip the city database and fall back to country-level and ASN data (city remains null).
  • getCountry(ip) queries the country database first and never populates city when a country record is found. Use getCity(ip) when reading city.
js
k.api.get("geo", () => {
  const info = k.net.IP.getCity("8.8.8.8")
  return {
    countryCode: info && info.countryCode ? info.countryCode : null,
    city: info && info.city ? info.city : null
  }
})

IPInfo common fields ​

FieldDescription
cityCity (populated only when city DB hits in online server mode)
stateProvince/State (unused; always null)
countryCodeCountry code
countryNameCountry name (currently returns countryCode)
continentCodeContinent code
latitude / longitudeLatitude and longitude
aSNDomainASN domain name
iPUserTypeUser type enum

Kooboo's KDefine ToCamelCase() only lowercases the first character, so TypeScript definitions in Kooboo Studio expect aSNDomain and iPUserType (at runtime, Jint resolves CLR property names case-insensitively).

TypeScript Definitions ​

ts
interface KIP {
  myIP: string;
  getCity(IpAddress: string): IPInfo;
  getCountry(IpAddress: string): IPInfo;
  getCityOrCountry(IpAddress: string): IPInfo;
}

interface IPInfo {
  city: string;
  state: string;
  countryCode: string;
  countryName: string;
  continentCode: string;
  latitude: number;
  longitude: number;
  aSNDomain: string;
  iPUserType: IpUserType;
}

type IpUserType = 'Unknown' | 'Residential' | 'Hosting' | 'Education' | 'Business' | 'Government';