k.net.IP
Current requesting IP and IP geographic information
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| KScript API, Page / View server scripts | myIP reads inbound request context; property name IP is uppercase | Reading 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
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 / Property | Description | Returns |
|---|---|---|
myIP | Client IP of the current inbound request | string |
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.
k.api.get("whoami", () => {
return { ip: k.net.IP.myIP }
})getCity() / getCountry() / getCityOrCountry()
Query geographical information based on an IP address.
| Method | Description |
|---|---|
getCity(ip) | City-level geolocation |
getCountry(ip) | Country-level geolocation |
getCityOrCountry(ip) | Performs the same city-level geolocation lookup as getCity(ip) |
| Parameter | Type | Required | Description |
|---|---|---|---|
ip | string | Yes | IP 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.IsOnlineServeristrue: otherwisegetCity(ip)andgetCityOrCountry(ip)skip the city database and fall back to country-level and ASN data (cityremainsnull). getCountry(ip)queries the country database first and never populatescitywhen a country record is found. UsegetCity(ip)when readingcity.
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
| Field | Description |
|---|---|
city | City (populated only when city DB hits in online server mode) |
state | Province/State (unused; always null) |
countryCode | Country code |
countryName | Country name (currently returns countryCode) |
continentCode | Continent code |
latitude / longitude | Latitude and longitude |
aSNDomain | ASN domain name |
iPUserType | User 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
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';