Freegeoip

Build Status Coverage Status Inline docs

Simple Elixir wrapper for freegeoip.net HTTP API.

This package allows to get geolocation information about a specified IP.

Usage

  {:ok, result} = FreeGeoIP.Search.search("192.30.252.128")

or

  {:ok, result} = FreeGeoIP.Search.search({192, 30, 252, 128})

Sample result

%{
  "city" => "San Francisco", 
  "country_code" => "US", 
  "country_name" => "United States", 
  "ip" => "192.30.252.128",
  "latitude" => 37.7697, 
  "longitude" => -122.3933, 
  "metro_code" => 807, 
  "region_code" => "CA",
  "region_name" => "California", 
  "time_zone" => "America/Los_Angeles", 
  "zip_code" => "94107"
}

Errors

You can receive mutiple errors, check the documentation for details:

%{:error, %{reason: :some_error, error: "some error description"}}

Localizing results

You can specify the language you want the results to be. Just use the ISO 639-1 code of the desired language.

FreeGeoIP.Search.search(ip, "es")

You can also put the locale as a string, using the same format as used on Accept-language HTTP header specification:

FreeGeoIP.Search.search(ip, "da, en-gb;q=0.8, en;q=0.7")

Using a custom server

You can use a custom hosted freegeoip server. I personally don’t recommend using the public server for production because is normally oversaturated, causing many requests to time out.

For instructions to install your own server check freegeoip project.

Once installed, you can specify the server base URL by setting FREEGEOIP_HOST environment variable:

export FREEGEOIP_HOST=http://my_own_server.com

Alternatively you can set the server host in your config.exs file:

use Mix.Config

config :freegeoip, base_url: "http://mydomain.com"

Server authentication

If you choose to protect your server with basic authentication you can specify username and password the same way you set the server host, this time using FREEGEOIP_USERNAMEand FREEGEOIP_PASSWORD env variables:

export FREEGEOIP_USERNAME=your_username
export FREEGEOIP_PASSWORD=your_password

Alternatively you can include this data into your server host in your config.exs file:

use Mix.Config

config :freegeoip, auth_username: "your_username"
config :freegeoip, auth_password: "your_password"

Installation

You can install this package like any other HEX packages:

  1. Add freegeoip to your list of dependencies in mix.exs:

    def deps do
      [{:freegeoip, "~> 0.0.3"}]
    end
  2. Ensure freegeoip is started before your application:

    def application do
      [applications: [:freegeoip]]
    end

Credits

freegeoip.net is community funded, therefore consider donating if you use and like it.