How to use IP2Proxy SDK in Elixir

Intro

Elixir is a dynamic, functional programming language designed for building scalable and maintainable applications. It runs on the Erlang Virtual Machine (BEAM), which is known for its low-latency, distributed, and fault-tolerant systems – traits that make Elixir especially suitable for real-time and concurrent applications.

For this tutorial, we will demonstrate how easy it is to include the IP2Proxy Erlang library as a dependency in an Elixir project. You can also take a look at the IP2Proxy Erlang codes at its GitHub repo. We will be using the IP2Proxy PX12 BIN file and using the codes to query proxy data for IPv4 or IPv6 addresses.

Pre-requisites

You’ll need to have Erlang and Elixir installed. Please follow https://elixir-lang.org/install.html for the installation steps. As the IP2Proxy Erlang library depends on the IP2Proxy BIN files, please download whichever BIN file you wish to use. You can purchase the commercial BIN files from https://www.ip2location.com/database/ip2proxy or download the free LITE BIN file from https://lite.ip2location.com/ip2proxy-lite. Our demo will be on a Debian Linux machine so some of the steps will be specifically for that platform.

Creating the Elixir project

Navigate to the folder where you wish to create your project, then run the below command to create the project folder. We’ll call our demo project ip2proxydemo.

mix new ip2proxydemo

Adding the IP2Proxy Erlang as a dependency

Now, navigate into the demo project folder and edit the mix.exs file.

cd ip2proxydemo
nano mix.exs

You’ll see the below.

Let’s insert the below line into the deps section and save the file. This will add the IP2Proxy Erlang library as a dependency.

{:ip2proxy_erlang, "~> 3.4.0"}

NOTE: You may need to update the version IP2Proxy Erlang library if there is a newer version.

Now run the below to actually fetch the dependency.

mix deps.get

Download and extract the IP2Proxy BIN file

As mentioned above, you’ll need to either get the commercial or LITE BIN file. In our case, we’ll be using the IP2Proxy PX12 BIN file. The BIN file will be in the root of the demo project folder.

Modify the test code

Let’s overwrite the generated test code in lib/ip2proxydemo.ex and replace it with our code below.

nano lib/ip2proxydemo.ex
defmodule Ip2proxydemo do
  def lookup(ip) do
    bin_file = "IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER-FRAUDSCORE.BIN"
    # Load the database
    :ip2proxy.open(bin_file)

    # Query an IP address
    result = :ip2proxy.getall(ip)

    # Access specific fields
    # IO.inspect(result)
    {:ip2proxyrecord, country_short, country_long, region, city, isp, proxy_type, domain, usage_type, asn, as, last_seen, threat, provider, fraud_score, is_proxy} = result
    IO.puts("country_short: #{to_string(country_short)}")
    IO.puts("country_long: #{to_string(country_long)}")
    IO.puts("region: #{to_string(region)}")
    IO.puts("city: #{to_string(city)}")
    IO.puts("isp: #{to_string(isp)}")
    IO.puts("proxy_type: #{to_string(proxy_type)}")
    IO.puts("domain: #{to_string(domain)}")
    IO.puts("usage_type: #{to_string(usage_type)}")
    IO.puts("asn: #{to_string(asn)}")
    IO.puts("as: #{to_string(as)}")
    IO.puts("last_seen: #{to_string(last_seen)}")
    IO.puts("threat: #{to_string(threat)}")
    IO.puts("provider: #{to_string(provider)}")
    IO.puts("fraud_score: #{to_string(fraud_score)}")
    IO.puts("is_proxy: #{to_string(is_proxy)}")
  end 
end

Launch the interactive shell to run the code

iex -S mix

Then, run the below to query proxy data for the IP 37.252.228.50.

Ip2proxydemo.lookup(to_charlist("37.252.228.50"))

That’s how easy it is to query the IP2Proxy proxy data using Elixir with the IP2Proxy Erlang library.

Was this article helpful?

Related Articles