Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.0.0+

Look up an IP country with MaxMind GeoIP

See more Geolocation Examples

Call the GeoIP Country web service using HTTP Basic authentication. Set the username to your numeric MaxMind account ID and the password to your license key. The account needs access to the web service, such as a trial or service credit. Both IPv4 and IPv6 are supported.

country describes the geolocated country. registered_country describes where the network is registered. The Country service returns country-level data. City-level lookups use the separately entitled /geoip/v2.1/city/ service. A GeoLite web-service account uses geolite.info as its host.

IP geolocation estimates a network location. VPNs, mobile gateways, and anycast routing can affect the result. City and coordinate fields can be missing or approximate. The sample address 8.8.8.8 is a public DNS address used to demonstrate the request.

Service documentation

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.
    ;  Use a public IP address. Provider results can change as their databases are updated.
    ipAddress.s = "8.8.8.8"
    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkRequireSslCertVerify(http, 1)
    CkHttp::setCkConnectTimeout(http, 10)
    CkHttp::setCkReadTimeout(http, 30)
    CkHttp::setCkAccept(http, "application/json")
    CkHttp::setCkBasicAuth(http, 1)
    CkHttp::setCkLogin(http, "MAXMIND_ACCOUNT_ID")
    CkHttp::setCkPassword(http, "MAXMIND_LICENSE_KEY")
    url.i = CkStringBuilder::ckCreate()
    If url.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(url,"https://geoip.maxmind.com/geoip/v2.1/country/")
    CkStringBuilder::ckAppend(url,CkHttp::ckUrlEncode(http,ipAddress))
    response.i = CkHttpResponse::ckCreate()
    If response.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpNoBody(http,"GET",CkStringBuilder::ckGetAsString(url),response)
    If CkHttpResponse::ckStatusCode(response) = 0
        ;  Request diagnostics can contain an API key. Report transport failure without logging credentials.
        Debug "No HTTP response. Check connectivity, DNS, and TLS configuration."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(response)
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    parsed.i = CkJsonObject::ckLoad(json,CkHttpResponse::ckBodyStr(response))
    If (success <> 1) OR (CkHttpResponse::ckStatusCode(response) < 200) OR (CkHttpResponse::ckStatusCode(response) >= 300)
        Debug "Lookup failed. HTTP " + Str(CkHttpResponse::ckStatusCode(response))
        If parsed = 1
            If CkJsonObject::ckJsonTypeOf(json,"code") = 1
                Debug CkJsonObject::ckStringOf(json,"code")
            EndIf

            If CkJsonObject::ckJsonTypeOf(json,"error") = 1
                Debug CkJsonObject::ckStringOf(json,"error")
            EndIf

        EndIf

        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(response)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    If parsed <> 1
        Debug "Expected a JSON object in the lookup response."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(response)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(json,"traits.ip_address") <> 1) OR (CkJsonObject::ckStringOfEquals(json,"traits.ip_address","",1) = 1)
        Debug "The response did not contain the expected IP address."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(url)
        CkHttpResponse::ckDispose(response)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    Debug "IP address: " + CkJsonObject::ckStringOf(json,"traits.ip_address")
    If (CkJsonObject::ckJsonTypeOf(json,"country.names.en") = 1) AND (CkJsonObject::ckStringOfEquals(json,"country.names.en","",1) <> 1)
        Debug "Country: " + CkJsonObject::ckStringOf(json,"country.names.en")
    Else
        Debug "Country: unavailable"
    EndIf

    If (CkJsonObject::ckJsonTypeOf(json,"country.iso_code") = 1) AND (CkJsonObject::ckStringOfEquals(json,"country.iso_code","",1) <> 1)
        Debug "Country code: " + CkJsonObject::ckStringOf(json,"country.iso_code")
    Else
        Debug "Country code: unavailable"
    EndIf

    If (CkJsonObject::ckJsonTypeOf(json,"continent.names.en") = 1) AND (CkJsonObject::ckStringOfEquals(json,"continent.names.en","",1) <> 1)
        Debug "Continent: " + CkJsonObject::ckStringOf(json,"continent.names.en")
    Else
        Debug "Continent: unavailable"
    EndIf

    If (CkJsonObject::ckJsonTypeOf(json,"registered_country.iso_code") = 1) AND (CkJsonObject::ckStringOfEquals(json,"registered_country.iso_code","",1) <> 1)
        Debug "Registered country code: " + CkJsonObject::ckStringOf(json,"registered_country.iso_code")
    Else
        Debug "Registered country code: unavailable"
    EndIf



    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(url)
    CkHttpResponse::ckDispose(response)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure