Get the location from an IP Address
How to get the location (country/city/region/postal code) from an IP Address in 26 languages! Inspired by Randall Degges at ipify!

Bash

1
#!/bin/bash
2
3
country=$(curl -s https://api.ipdata.co/country_name?api-key=test)
4
echo "My country is: $country"
Copied!

NGS (Next Generation Shell)

1
country=`curl -s https://api.ipdata.co/country_name?api-key=test`
2
echo("My country is: $country")
Copied!

Python

1
# This example requires the requests library be installed. You can learn more
2
# about the Requests library here: http://docs.python-requests.org/en/latest/
3
from requests import get
4
5
country = get('https://api.ipdata.co/country_name?api-key=test').text
6
print('My country is: {}'.format(country))
Copied!

Ruby

1
require "net/http"
2
3
country = Net::HTTP.get(URI("https://api.ipdata.co/country_name?api-key=test"))
4
puts "My country is: " + country
Copied!

PHP

1
<?php
2
$country = file_get_contents('https://api.ipdata.co/country_name?api-key=test');
3
echo "My country is: " . $country;
4
?>
Copied!

Java

1
try (java.util.Scanner s = new java.util.Scanner(new java.net.URL("https://api.ipdata.co/country_name?api-key=test").openStream(), "UTF-8").useDelimiter("\\A")) {
2
System.out.println("My current country is " + s.next());
3
} catch (java.io.IOException e) {
4
e.printStackTrace();
5
}
Copied!

Perl

1
use strict;
2
use warnings;
3
use LWP::UserAgent;
4
5
my $ua = new LWP::UserAgent();
6
my $country = $ua->get('https://api.ipdata.co/country_name?api-key=test')->content;
7
print 'My country is: '. $country;
Copied!

C#

1
var httpClient = new HttpClient();
2
var country = await httpClient.GetStringAsync("https://api.ipdata.co/country_name?api-key=test");
3
Console.WriteLine(quot;My country is: {country}");
Copied!

VB.NET

1
Dim httpClient As New System.Net.Http.HttpClient
2
Dim country As String = Await httpClient.GetStringAsync("https://api.ipdata.co/country_name?api-key=test")
3
Console.WriteLine(quot;My country is: {country}")
Copied!

NodeJS

1
var http = require('http');
2
3
http.get({'host': 'api.ipdata.co?api-key=test', 'port': 80, 'path': '/'}, function(resp) {
4
resp.on('data', function(ip) {
5
console.log("My country is: " + country);
6
});
7
});
Copied!

Go

1
package main
2
3
import (
4
"io/ioutil"
5
"net/http"
6
"os"
7
)
8
9
func main() {
10
res, _ := http.Get("https://api.ipdata.co/country_name?api-key=test")
11
country, _ := ioutil.ReadAll(res.Body)
12
os.Stdout.Write(country)
13
}
Copied!

Racket

1
(require net/url)
2
3
(define country (port->string (get-pure-port (string->url "https://api.ipdata.co/country_name?api-key=test"))))
4
(printf "My country is: ~a" country)
Copied!

Lisp

1
;This example requires the drakma http package installed.
2
;It can be found here: http://www.weitz.de/drakma
3
4
(let ((stream
5
(drakma:http-request "https://api.ipdata.co/country_name?api-key=test" :want-stream t)))
6
(let ((public-ip (read-line stream)))
7
(format t "My country is: ~A" public-country)))
Copied!

Xojo

1
Dim s As New HTTPSecureSocket
2
Dim t As String = s.Get("https://api.ipdata.co/country_name?api-key=test",10)
3
MsgBox "My country is: " + t
4
Scala
5
val country = scala.io.Source.fromURL("https://api.ipdata.co/country_name?api-key=test").mkString
6
println(s"My country is: $country")
Copied!

Javascript

1
<script type="application/javascript">
2
function getIP(json) {
3
document.write("My country is: ", json.country);
4
}
5
</script>
6
7
<script type="application/javascript" src="https://api.ipdata.co/country_name?api-key=test?format=jsonp&callback=getCountry"></script>
Copied!

jQuery

1
<script type="application/javascript">
2
$(function() {
3
$.getJSON("https://api.ipdata.co/country_name?api-key=test?format=jsonp&callback=?",
4
function(json) {
5
document.write("My country is: ", json.country);
6
}
7
);
8
});
9
</script>
Copied!

C#

1
using System;
2
using System.Net;
3
4
namespace Ipdata.Examples {
5
class Program {
6
public static void Main (string[] args) {
7
WebClient webClient = new WebClient();
8
string publicCountry = webClient.DownloadString("https://api.ipdata.co/country_name?api-key=test");
9
Console.WriteLine("My country is: {0}", publicCountry);
10
}
11
}
12
}
Copied!

Elixir

1
:inets.start
2
{:ok, {_, _, inet_addr}} = :httpc.request('http://api.ipdata.co?api-key=test')
3
:inets.stop
4
nim
5
import HttpClient
6
var country = newHttpClient().getContent("https://api.ipdata.co/country_name?api-key=test")
7
echo("My country is: ", country)
Copied!

PowerShell

1
$response = Invoke-RestMethod -Uri 'https://api.ipdata.co?api-key=test'
2
"My country is: $($response.country_name)"
Copied!

Lua

1
http.Fetch("https://api.ipdata.co/country_name?api-key=test", function(body) print("My country is: " .. body ) end
Copied!

PureBasic

1
InitNetwork()
2
*Buffer = ReceiveHTTPMemory("https://api.ipdata.co/?api-key=test")
3
If *Buffer
4
ParseJSON(0, PeekS(*Buffer, MemorySize(*Buffer), #PB_UTF8))
5
FreeMemory(*Buffer)
6
Debug GetJSONString(GetJSONMember(JSONValue(0), "country_name"))
7
EndIf
Copied!

LiveCode

1
put "My country is" && url "https://api.ipdata.co/country_name?api-key=test"
2
Objective-C
3
NSURL *url = [NSURL URLWithString:@"https://api.ipdata.co/country_name?api-key=test/"];
4
NSString *country = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
5
NSLog(@"My country is: %@", country);
Copied!

Arduino

1
if (client.connect("api.ipdata.co?api-key=test", 80)) {
2
Serial.println("connected");
3
client.println("GET / HTTP/1.0");
4
client.println("Host: api.ipdata.co/country_name?api-key=test");
5
client.println();
6
} else {
7
Serial.println("connection failed");
8
}
Copied!
Last modified 2yr ago