Quick Start Guide

In this document, you will go through the basic steps to use IP Geolocation API C# SDK.
You need a valid 'IPGeolocation API key' to use this SDK. Sign up here and get your free API key if you don't have one.

System Requirements

IP Geolocation API C# SDK has been developed and tested on .NET Core on Ubuntu.
Note: Internet connection is required to run this component.

Installation

Package Manager

$ Install-Package IpGeoLocation.IpGeoLocation -Version 1.0.3

.NET CLI

$ dotnet add package IpGeoLocation.IpGeoLocation --version 1.0.3

Paket CLI

$ paket add IpGeoLocation.IpGeoLocation --version 1.0.3

Basic Usage

Setup API

// Create IPGeolocationAPI object, passing your valid API key
IPGeolocationAPI api = new IPGeolocationAPI("YOUR_API_KEY");

Geolocation Lookup

// Query geolocation for IP address (1.1.1.1) and fields (geo, time_zone and currency)
GeolocationParams geoParams = new GeolocationParams();
geoParams.SetIp("1.1.1.1");
geoParams.SetFields("geo,time_zone,currency");

Geolocation geolocation = api.GetGeolocation(geoParams);

// Check if geolocation lookup was successful
if(geolocation.GetStatus() == 200)
{
    Console.WriteLine(geolocation.GetCountryName());
    Console.WriteLine(geolocation.GetCurrency().GetName());
    Console.WriteLine(geolocation.GetTimezone().GetCurrentTime());
}
else
{
    Console.WriteLine(geolocation.GetMessage());
}

// Query geolocation for IP address (1.1.1.1) and all fields
GeolocationParams geoParams = new GeolocationParams();
geoParams.SetIp("1.1.1.1");

Geolocation geolocation = api.GetGeolocation(geoParams);

if(geolocation.GetStatus() == 200)
{
    Console.WriteLine(geolocation.GetIp());
    Console.WriteLine(geolocation.GetCountryName());
}
else
{
    Console.WriteLine(geolocation.GetMessage());
}

// Query geolocation for the calling machine's IP address for all fields
Geolocation geolocation = api.GetGeolocation();

if(geolocation.GetStatus() == 200)
{
    Console.WriteLine(geolocation.GetCountryCode2());
    Console.WriteLine(geolocation.GetTimezone().GetCurrentTime());
}
else
{
    Console.WriteLine(geolocation.GetMessage());
}

Bulk Geolocations Lookup

// Query geolocations for multiple IP addresses and all fields
String[] ips = new String[]{"1.1.1.1", "2.2.2.2", "3.3.3.3"};
GeolocationParams geoParams = new GeolocationParams();
geoParams.SetIps(ips);

List<Geolocation> geolocations = api.GetBulkGeolocation(geoParams);

Console.WriteLine(geolocations.Count);
Console.WriteLine(geolocations[0].GetCountryName());
Console.WriteLine(geolocations[1].GetLanguages());
Console.WriteLine(geolocations[2].GetTimezone().GetCurrentTime());

// Query geolocations for multiple IP addresses but only geo field
String[] ips = new String[]{"1.1.1.1", "2.2.2.2", "3.3.3.3"};
GeolocationParams geoParams = new GeolocationParams();
geoParams.SetIps(ips);
geoParams.SetFields("geo");

List<Geolocation> geolocations = api.GetBulkGeolocation(geoParams);

Console.WriteLine(geolocations.Count);
Console.WriteLine(geolocations[0].GetCountryCode2());
Console.WriteLine(geolocations[1].GetCountryName());
Console.WriteLine(geolocations[2].GetLatitude());

Time Zone API

// Query time zone information by time zone ID
TimezoneParams tzParams = new TimezoneParams();
tzParams.SetTimezone("America/New_York");

Timezone tz = api.GetTimezone(tzParams);

if(tz.GetStatus() == 200)
{
    Console.WriteLine(tz.GetDateTimeWti());
    Console.WriteLine(tz.GetDateTimeTxt());
}
else
{
    Console.WriteLine(tz.GetMessage());
}

// Query time zone information by latitude and longitude of the location
TimezoneParams tzParams = new TimezoneParams();
tzParams.SetLocation(37.1838139, -123.8105225);

Timezone tz = api.GetTimezone(tzParams);

if(tz.GetStatus() == 200)
{
    Console.WriteLine(tz.GetTimezone());
}
else
{
    Console.WriteLine(tz.GetMessage());
}

// Query time zone information for IP address (1.1.1.1)
TimezoneParams tzParams = new TimezoneParams();
tzParams.SetIp("1.1.1.1");

Timezone tz = api.GetTimezone(tzParams);

if(tz.GetStatus() == 200)
{
    Console.WriteLine(tz.GetTimezone());
}
else
{
    Console.WriteLine(tz.GetMessage());
}

// Query time zone information for calling machine's IP address
Timezone tz = api.GetTimezone();

if(tz.getMessage())
{
    Console.WriteLine(tz.GetTimezone());
    Console.WriteLine(tz.GetDateTimeYmd());
}
else
{
    Console.WriteLine(tz.GetMessage());
}

IP Geolocation API C# SDK Objects Reference

IP Geolocation API C# SDK has following classes that you can use to fully leverage it.

Class: IPGeolocation.IPGeolocationAPI

MethodDescriptionReturn Type
IPGeolocationAPI(String apiKey)Construct the IPGeolocationAPI object. It takes a valid apiKey as a parameter and throws ArgumentException if apiKey is empty or null.
GetGeolocation()This function to query Geolocation API.Geolocation
GetGeolocation(GeolocationParams params)This function to query Geolocation API based on the parameters passed.Geolocation
GetTimezone()This function to query Timezone API based on calling machine's IP address.Timezone
GetTimezone(TimezoneParams params)This function to query Timezone API based on the parameters passed.Timezone
GetBulkGeolocation(GeolocationParams params)This function to query Geolocation API to lookup multiple IP addresses (max. 50).List
GetApiKey()This function to get the API key that you set to query the IPGeolocation API.String

Class: IPGeolocation.GeolocationParams

MethodDescriptionReturn Type
SetIp(String ip)Sets IP address to lookup geolocation.void
GetIp()Get IP address set to lookup geolocation.String
SetFields(String fields)Set fields to lookup geolocation.void
GetFields()Get fields set to lookup geolocation.String
SetIps(String[] ips)Set IP addresses to lookup multiple geolocations. Throws ArgumentException if no. of IP addresses are more than 50. Note: Multiple IP addresses lookup is only available for paid users.void
GetIps()Get IP addresses set to lookup bulk geolocations.String[]

Class: IPGeolocation.Geolocation

MethodDescriptionReturn Type
GetStatus()Returns HTTP status of the geolocation query. 200 is the successful query status.Integer
GetMessage()Returns error message, if the query was not successful.String
GetIp()Returns IP address of the geolocation.String
GetContinentCode()Returns 2-letters continent code.String
GetContinentName()Returns continent name.String
GetCountryCode2()Returns 2-letters country code.String
GetCountryCode3()Returns 3-letters country code.String
GetCountryName()Returns country name.String
GetCountryCapital()Returns country capital.String
GetStateProvince()Returns state/province.String
GetDistrict()Returns district.String
GetCity()Returns city.String
GetZipcode()Returns zip code.String
GetLatitude()Returns latitude of the city.Double
GetLongitude()Returns longitude of the city.Double
isEu()Returns is the country in European Union.Boolean
GetCallingCode()Returns country calling code.String
GetCountryTld()Returns country's top level domain like '.au' for Australia.String
GetLanguages()Returns languages spoken in the country.String
GetCountryFlag()Returns a URL to country's flag.String
GetIsp()Returns ISP name.String
GetConnectionType()Returns connection type of the IP address.String
GetOrganization()Returns organization using the IP address.String
GetGeonameId()Returns geoname ID from geonames.org database.String
GetCurrency()Returns currency information of the country.GeolocationCurrency
GetTimezone()Returns time zone information of the country.GeolocationTimezone

Class: IPGeolocation.GeolocationCurrency

MethodDescriptionReturn Type
GetCode()Returns 3-letters currency code.String
GetName()Returns currency name.String

Class: IPGeolocation.GeolocationTimezone

MethodDescriptionReturn Type
GetName()Returns standard time zone ID like "America/New_York".String
GetOffset()Returns time zone offset from UTC.Double
GetCurrentTime()Returns current date-time string in the format "yyyy-MM-dd HH:mm:ss.SSSZ".String
isDST()Returns is the country observing daylight saving time.Boolean
GetDstSavings()Returns daylight savings time (in hours).Double

Class: IPGeolocation.TimezoneParams

MethodDescriptionReturn Type
SetTimezone(String timezone)Sets time zone ID to query time zone information.void
GetTimezone()Get time zone ID set to query time zone information.String
SetIp(String ip)Sets IP address to query time zone information.void
GetIp()Get IP address set to query time zone information.String
SetLocation(Double latitude, Double longitude)Sets latitude and longitude of a location to query time zone information.void
GetLatitude()Returns latitude set to query time zone information.Double
GetLongitude()Returns longitude set to query time zone information.Double

Class: IPGeolocation.Timezone

MethodDescriptionReturn Type
GetStatus()Returns HTTP status of the geolocation query. 200 is the successful query status.Integer
GetMessage()Returns error message, if the query was not successful.String
GetTimezone()Returns time zone ID like "America/New_York".String
GetTimezoneOffset()Returns time zone offset from UTC.Double
GetDate()Returns current date in the format "yyyy-MM-dd".String
GetDateTime()Returns date-time string in the format "yyyy-MM-dd HH:mm:ss".String
GetDateTimeTxt()Returns date-time string in the format "EEEE, MMMM dd, yyyy HH:mm:ss".String
GetDateTimeWti()Returns date-time string in the format "EEE, dd MMM yyyy HH:mm:ss Z".String
GetDateTimeYmd()Returns date-time string in the format "yyyy-MM-dd'T'HH:mm:ssZ".String
GetTime24()Returns current time in the format "HH:mm:ss".String
GetTime12()Returns current time in the format "hh:mm:ss aa".String
GetWeek()Returns current week of the year.String
GetMonth()Returns current month of the year.String
GetYear()Returns current year.String
GetYearAbbr()Returns 2-letters year abbreviation like "18".String
isDst()Returns is the country observing Daylight Saving time.Boolean
GetDstSavings()Returns daylight savings time (in hours).Double
GetTimezoneGeo()Returns geolocation of timezone if you lookup timezone information from an IP address.TimezoneGeo

Class: IPGeolocation.TimezoneGeo

MethodDescriptionReturn Type
GetCountryCode2()Returns 2-letters country code.String
GetCountryCode3()Returns 3-letters country code.String
GetCountryName()Returns country name.String
GetStateProvince()Returns state/province.String
GetDistrict()Returns district.String
GetCity()Returns city.String
GetZipcode()Returns zip code.String
GetLatitude()Returns latitude of the city.Double
GetLongitude()Returns longitude of the city.Double