How can I determine the usage type of an IP address in Erlang?

To determine the usage type of an IP address using IP2Location.io in Erlang, you can use the IP2Location.io API to retrieve geolocation and related data. The usage type field specifies how the IP is being used. For usage types supported, please see API documentation for more info.

  1. We'll assume that you have already installed Erlang and we won't cover that here. You will also need a paid subscription to the Starter plan or higher. Subscribe now!
  2. Our example is for the Debian operating system. So, we'll be installing the erlang-jiffy Debian package for parsing the JSON results. For other platforms, please refer to the Jiffy GitHub.
  3. In the command line, run the command below to install the erlang-jiffy package if you don't have it installed.
    Bash
    
    sudo apt install erlang-jiffy
    			
  4. Save the below code into a file called test.erl on your computer.
    Erlang
    
    -module(test).
    -export([runtest/0]).
    
    runtest() ->
    	Key = "YOUR_API_KEY",
    	IP = "8.8.8.8",
    
    	ssl:start(),
    	inets:start(),
    	
    	MyParams = uri_string:compose_query([{"format", "json"}, {"key", Key}, {"ip", IP}]),
    	case httpc:request(get, {"https://api.ip2location.io/?" ++ MyParams, []}, [{ssl, [{versions, ['tlsv1.2']}]}, {autoredirect, false}], []) of
    		{ok, {{_, 200, _}, _, Body}} ->
    			Result = jiffy:decode(unicode:characters_to_binary(Body,unicode,utf8),[return_maps]),
    			case maps:is_key(<<"usage_type">>, Result) of
    				true ->
    					UsageType = maps:get(<<"usage_type">>, Result),
    					io:format("The usage type for IP ~s is ~s.~n", [IP, UsageType]);
    				_ ->
    					io:format("ERROR: The usage_type field requires a paid subscription to the Starter plan or higher.~n", [])
    			end;
    		{ok, {{_, 400, _}, _, Body}} ->
    			Result = jiffy:decode(unicode:characters_to_binary(Body,unicode,utf8),[return_maps]),
    			case maps:is_key(<<"error">>, Result) of
    				true ->
    					Error = maps:get(<<"error">>, Result),
    					io:format("ERROR: ~s~n", [maps:get(<<"error_message">>, Error)]);
    				_ ->
    					io:format("ERROR: ~p~n", [Result])
    			end;
    		{ok, {{_, 401, _}, _, Body}} ->
    			Result = jiffy:decode(unicode:characters_to_binary(Body,unicode,utf8),[return_maps]),
    			case maps:is_key(<<"error">>, Result) of
    				true ->
    					Error = maps:get(<<"error">>, Result),
    					io:format("ERROR: ~s~n", [maps:get(<<"error_message">>, Error)]);
    				_ ->
    					io:format("ERROR: ~p~n", [Result])
    			end;
    		{error, Reason} ->
    			io:format("ERROR ~p~n", [Reason])
    	end.
    			
  5. In the command line, run the below command to launch the Erlang shell.
    Bash
    
    erl
    			
  6. Inside the Erlang shell, run the below commands to compile and run the test code.
    Erlang
    
    c(test).
    test:runtest().
    			

This script will output the usage type of specified IP address. Make sure to replace 8.8.8.8 with the IP address you want and replace YOUR_API_KEY to your own API key.

Other Languages

Unlock Location Insights For Free

Empower your applications with accurate IP geolocation information now.

Try It for Free