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.
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Use a public IP address. Provider results can change as their databases are updated.
DECLARE @ipAddress nvarchar(4000)
SELECT @ipAddress = '8.8.8.8'
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @http, 'ConnectTimeout', 10
EXEC sp_OASetProperty @http, 'ReadTimeout', 30
EXEC sp_OASetProperty @http, 'Accept', 'application/json'
EXEC sp_OASetProperty @http, 'BasicAuth', 1
EXEC sp_OASetProperty @http, 'Login', 'MAXMIND_ACCOUNT_ID'
EXEC sp_OASetProperty @http, 'Password', 'MAXMIND_LICENSE_KEY'
DECLARE @url int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @url OUT
EXEC sp_OAMethod @url, 'Append', @success OUT, 'https://geoip.maxmind.com/geoip/v2.1/country/'
EXEC sp_OAMethod @http, 'UrlEncode', @sTmp0 OUT, @ipAddress
EXEC sp_OAMethod @url, 'Append', @success OUT, @sTmp0
DECLARE @response int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @response OUT
EXEC sp_OAMethod @url, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', @sTmp0, @response
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
-- Request diagnostics can contain an API key. Report transport failure without logging credentials.
PRINT 'No HTTP response. Check connectivity, DNS, and TLS configuration.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @response
RETURN
END
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
DECLARE @parsed int
EXEC sp_OAGetProperty @response, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @json, 'Load', @parsed OUT, @sTmp0
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp1 OUT
IF (@success <> 1) or (@iTmp0 < 200) or (@iTmp1 >= 300)
BEGIN
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
PRINT 'Lookup failed. HTTP ' + @iTmp0
IF @parsed = 1
BEGIN
EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'code'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'code'
PRINT @sTmp0
END
EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'error'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'error'
PRINT @sTmp0
END
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @json
RETURN
END
IF @parsed <> 1
BEGIN
PRINT 'Expected a JSON object in the lookup response.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @json
RETURN
END
EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'traits.ip_address'
EXEC sp_OAMethod @json, 'StringOfEquals', @iTmp1 OUT, 'traits.ip_address', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'The response did not contain the expected IP address.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @json
RETURN
END
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'traits.ip_address'
PRINT 'IP address: ' + @sTmp0
EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'country.names.en'
EXEC sp_OAMethod @json, 'StringOfEquals', @iTmp1 OUT, 'country.names.en', '', 1
IF (@iTmp0 = 1) and (@iTmp1 <> 1)
BEGIN
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'country.names.en'
PRINT 'Country: ' + @sTmp0
END
ELSE
BEGIN
PRINT 'Country: unavailable'
END
EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'country.iso_code'
EXEC sp_OAMethod @json, 'StringOfEquals', @iTmp1 OUT, 'country.iso_code', '', 1
IF (@iTmp0 = 1) and (@iTmp1 <> 1)
BEGIN
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'country.iso_code'
PRINT 'Country code: ' + @sTmp0
END
ELSE
BEGIN
PRINT 'Country code: unavailable'
END
EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'continent.names.en'
EXEC sp_OAMethod @json, 'StringOfEquals', @iTmp1 OUT, 'continent.names.en', '', 1
IF (@iTmp0 = 1) and (@iTmp1 <> 1)
BEGIN
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'continent.names.en'
PRINT 'Continent: ' + @sTmp0
END
ELSE
BEGIN
PRINT 'Continent: unavailable'
END
EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'registered_country.iso_code'
EXEC sp_OAMethod @json, 'StringOfEquals', @iTmp1 OUT, 'registered_country.iso_code', '', 1
IF (@iTmp0 = 1) and (@iTmp1 <> 1)
BEGIN
EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'registered_country.iso_code'
PRINT 'Registered country code: ' + @sTmp0
END
ELSE
BEGIN
PRINT 'Registered country code: unavailable'
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @json
END
GO