How to Use IP2Location.io API in Elixir

How to Use IP2Location.io in Elixir

Introduction

Elixir is a dynamic, functional language and is used for building scalable and maintainable applications. It runs on the Erlang Virtual Machine, which is also called the Bogdan’s Erlang Abstract Machine(BEAM). José Valim was the creator of this language back in year 2012. Several web applications had adopted Elixir to build their services, such as Heroku and Discord.

Elixir had pre-built libraries, but they also support third-party libraries to extend their functionality. For example, user can use a library to enable more features such as advance authentication or visitor blocking. In this article, we will learn how to use the IP2Location.io API in Elixir through a library called GeoIP. IP2Location.io provides a fast and accurate IP Geolocation API tool to determine a user’s location and use the geolocation information in different use cases.

Requirement

Before we start, please make sure that you have installed Erlang and Elixir. If you haven’t do so, you can refer to the Elixir documentation to get started. This article will use GeoIP, you can get more information at https://github.com/navinpeiris/geoip. The IP2Location.io API requires an API key to function. You may sign up for a free API key if you do not have one. Please note that the continent, country, region and city translations are only available in the IP2Location.io Plus and Security plans.

Step by step guide to use the IP2Location.io API in GeoIP library.

  1. We will get started by create a new project using an Elixir build tool called Mix. Use the following command to create a new Elixir project: mix new newproject1 --module NEWPROJECT1.
Create a new Elixir project using Mix
  1. In your project root directory mix.exs, add :geoip to your list of dependencies like this:
  defp deps do
    [
      {:geoip, git: "https://github.com/navinpeiris/geoip.git"}
    ]
  end
  1. Remember to update your project mix dependencies using this command in the terminal: mix deps.get.
  2. Create a new folder under project root directory called config. Create a new file under this config directory called config.exs. In config/config.exs, add the following lines to save your configuration for IP2Location.io:
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
import Config

config :geoip, provider: :ip2locationio, api_key: "your_api_key"
  1. You can now use GeoIP.lookup() function to lookup the geolocation information from IP2Location.io API. Open your lib/newprojec_t1.ex and paste the following into the line before the last end:
  def iplookup do:
    GeoIP.lookup("8.8.8.8")
  end
  1. After that, you will need to execute this command mix compile to compile your project. You will see the following outcome once compilation is successful:
Output of successful compilation process
  1. Next, we can start an iex session inside the project by using this command: iex -S mix. You will see the following output: Output of an iex session
  2. You can execute the function iplookup by running the following command: NEWPROJECT1.iplookup(). You will see the following output like this:
iex(1)> NEWPROJECT1.iplookup()
{:ok,
 %{
   as: "Google LLC",
   is_proxy: false,
   domain: "google.com",
   weather_station_code: "USCA0746",
   area_code: "650",
   isp: "Google LLC",
   geotargeting: %{metro: "807"},
   city_name: "Mountain View",
   address_type: "Anycast",
   asn: "15169",
   mobile_brand: "-",
   country_code: "US",
   district: "Santa Clara County",
   usage_type: "DCH",
   ip: "8.8.8.8",
   ads_category_name: "Data Centers",
   net_speed: "T1",
   time_zone: "-07:00",
   time_zone_info: %{
     current_time: "2023-09-20T01:20:27-07:00",
     gmt_offset: -25200,
     is_dst: true,
     olson: "America/Los_Angeles",
     sunrise: "06:54",
     sunset: "19:08"
   },
   region_name: "California",
   mcc: "-",
   city: %{name: "Mountain View", translation: %{lang: nil, value: nil}},
   longitude: -122.078515,
   ads_category: "IAB19-11",
   idd_code: "1",
   mnc: "-",
   weather_station_name: "Mountain View",
   latitude: 37.405992,
   continent: %{
     code: "NA",
     hemisphere: ["north", "west"],
     name: "North America",
     translation: %{lang: nil, value: nil}
   },
   elevation: 32,
   country: %{
     alpha3_code: "USA",
     capital: "Washington, D.C.",
     currency: %{code: "USD", name: "United States Dollar", symbol: "$"},
     demonym: "Americans",
     flag: "https://cdn.ip2location.io/assets/img/flags/us.png",
     language: %{code: "EN", name: "English"},
     name: "United States of America",
     numeric_code: 840,
     population: 331002651,
     tld: "us",
     total_area: 9826675,
     translation: %{lang: nil, value: nil}
   },
   region: %{
     code: "US-CA",
     name: "California",
     translation: %{lang: "", value: ""}
   },
   zip_code: "94043",
   proxy: %{
     is_botnet: false,
     is_data_center: true,
     is_public_proxy: false,
     is_residential_proxy: false,
     is_scanner: false,
     is_spammer: false,
     is_tor: false,
     is_vpn: false,
     is_web_crawler: false,
     is_web_proxy: false,
     last_seen: 19,
     provider: "-",
     proxy_type: "DCH",
     threat: "-"
   },
   country_name: "United States of America"
 }}

Conclusion

At the completion of this article, you are able to perform an IP geolocation lookup using GeoIP and IP2Location.io API in Elixir. This will enable you to obtain the geolocation information of an IP address, which can be use in various scenarios later on. For example, analysis where the IP address come from, and identify whether the IP address poses threat or not.

Was this article helpful?

Related Articles