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

Look up an IP address with IP2Location.io

See more Geolocation Examples

Look up a public IPv4 or IPv6 address. The sample runs keyless. To use your account allowance, set apiKey and Chilkat will send it as a Bearer token. IP2Location.io recommends header authentication and also accepts its older key query parameter.

Keyless requests have a daily allowance. Registered plans have their own quotas and field availability. Check the provider’s current limits. An HTTP 401 with error 10000 can indicate an invalid key or an exhausted free allowance. Coordinates can default to the capital city when a city is unknown. The returned time_zone is a UTC offset.

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 SQL Server Downloads

SQL Server
-- 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 @sTmp1 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'
    --  Leave empty for keyless evaluation, or set the API key from your account.
    DECLARE @apiKey nvarchar(4000)
    SELECT @apiKey = ''
    IF @apiKey <> ''
      BEGIN
        EXEC sp_OASetProperty @http, 'AuthToken', @apiKey
      END
    DECLARE @parameters int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @parameters OUT

    EXEC sp_OAMethod @parameters, 'UpdateString', @success OUT, 'ip', @ipAddress
    EXEC sp_OAMethod @parameters, 'UpdateString', @success OUT, 'format', 'json'
    DECLARE @response int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @response OUT

    EXEC sp_OAMethod @http, 'HttpParams', @success OUT, 'GET', 'https://api.ip2location.io/', @parameters, @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 @parameters
        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, 'error.error_message'
            IF @iTmp0 = 1
              BEGIN
                EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'error.error_message'
                PRINT @sTmp0
              END
          END
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @parameters
        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 @parameters
        EXEC @hr = sp_OADestroy @response
        EXEC @hr = sp_OADestroy @json
        RETURN
      END
    EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'error'
    IF @iTmp0 = 3
      BEGIN

        PRINT 'The service could not complete the lookup.'
        EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'error.error_message'
        IF @iTmp0 = 1
          BEGIN
            EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'error.error_message'
            PRINT @sTmp0
          END
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @parameters
        EXEC @hr = sp_OADestroy @response
        EXEC @hr = sp_OADestroy @json
        RETURN
      END
    EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'ip'
    EXEC sp_OAMethod @json, 'StringOfEquals', @iTmp1 OUT, 'ip', '', 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 @parameters
        EXEC @hr = sp_OADestroy @response
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'ip'
    PRINT 'IP address: ' + @sTmp0
    EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'country_name'
    EXEC sp_OAMethod @json, 'StringOfEquals', @iTmp1 OUT, 'country_name', '', 1
    IF (@iTmp0 = 1) and (@iTmp1 <> 1)
      BEGIN

        EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'country_name'
        PRINT 'Country: ' + @sTmp0
      END
    ELSE
      BEGIN

        PRINT 'Country: unavailable'
      END
    EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'country_code'
    EXEC sp_OAMethod @json, 'StringOfEquals', @iTmp1 OUT, 'country_code', '', 1
    IF (@iTmp0 = 1) and (@iTmp1 <> 1)
      BEGIN

        EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'country_code'
        PRINT 'Country code: ' + @sTmp0
      END
    ELSE
      BEGIN

        PRINT 'Country code: unavailable'
      END
    EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'region_name'
    EXEC sp_OAMethod @json, 'StringOfEquals', @iTmp1 OUT, 'region_name', '', 1
    IF (@iTmp0 = 1) and (@iTmp1 <> 1)
      BEGIN

        EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'region_name'
        PRINT 'Region: ' + @sTmp0
      END
    ELSE
      BEGIN

        PRINT 'Region: unavailable'
      END
    EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'city_name'
    EXEC sp_OAMethod @json, 'StringOfEquals', @iTmp1 OUT, 'city_name', '', 1
    IF (@iTmp0 = 1) and (@iTmp1 <> 1)
      BEGIN

        EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'city_name'
        PRINT 'City: ' + @sTmp0
      END
    ELSE
      BEGIN

        PRINT 'City: unavailable'
      END
    EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'time_zone'
    EXEC sp_OAMethod @json, 'StringOfEquals', @iTmp1 OUT, 'time_zone', '', 1
    IF (@iTmp0 = 1) and (@iTmp1 <> 1)
      BEGIN

        EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'time_zone'
        PRINT 'UTC offset: ' + @sTmp0
      END
    ELSE
      BEGIN

        PRINT 'UTC offset: unavailable'
      END
    --  Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
    EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp0 OUT, 'latitude'
    EXEC sp_OAMethod @json, 'JsonTypeOf', @iTmp1 OUT, 'longitude'
    IF (@iTmp0 = 2) and (@iTmp1 = 2)
      BEGIN

        EXEC sp_OAMethod @json, 'StringOf', @sTmp0 OUT, 'latitude'

        EXEC sp_OAMethod @json, 'StringOf', @sTmp1 OUT, 'longitude'
        PRINT 'Coordinates: ' + @sTmp0 + ', ' + @sTmp1
      END
    ELSE
      BEGIN

        PRINT 'Coordinates: unavailable'
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @parameters
    EXEC @hr = sp_OADestroy @response
    EXEC @hr = sp_OADestroy @json


END
GO