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
#!/bin/bash
country=$(curl -s https://api.ipdata.co/country_name?api-key=<<apiKey>>)
echo "My country is: $country"
NGS (Next Generation Shell)
country=`curl -s https://api.ipdata.co/country_name?api-key=<<apiKey>>`
echo("My country is: $country")
Python
# This example requires the requests library be installed. You can learn more
# about the Requests library here: http://docs.python-requests.org/en/latest/
from requests import get
country = get('https://api.ipdata.co/country_name?api-key=<<apiKey>>').text
print('My country is: {}'.format(country))
Ruby
require "net/http"
country = Net::HTTP.get(URI("https://api.ipdata.co/country_name?api-key=<<apiKey>>"))
puts "My country is: " + country
PHP
<?php
$country = file_get_contents('https://api.ipdata.co/country_name?api-key=<<apiKey>>');
echo "My country is: " . $country;
?>
Java
try (java.util.Scanner s = new java.util.Scanner(new java.net.URL("https://api.ipdata.co/country_name?api-key=<<apiKey>>").openStream(), "UTF-8").useDelimiter("\\A")) {
System.out.println("My current country is " + s.next());
} catch (java.io.IOException e) {
e.printStackTrace();
}
Perl
use strict;
use warnings;
use LWP::UserAgent;
my $ua = new LWP::UserAgent();
my $country = $ua->get('https://api.ipdata.co/country_name?api-key=<<apiKey>>')->content;
print 'My country is: '. $country;
C#
var httpClient = new HttpClient();
var country = await httpClient.GetStringAsync("https://api.ipdata.co/country_name?api-key=<<apiKey>>");
Console.WriteLine($"My country is: {country}");
VB.NET
Dim httpClient As New System.Net.Http.HttpClient
Dim country As String = Await httpClient.GetStringAsync("https://api.ipdata.co/country_name?api-key=<<apiKey>>")
Console.WriteLine($"My country is: {country}")
NodeJS
var http = require('http');
http.get({'host': 'api.ipdata.co?api-key=<<apiKey>>', 'port': 80, 'path': '/'}, function(resp) {
resp.on('data', function(ip) {
console.log("My country is: " + country);
});
});
Go
package main
import (
"io/ioutil"
"net/http"
"os"
)
func main() {
res, _ := http.Get("https://api.ipdata.co/country_name?api-key=<<apiKey>>")
country, _ := ioutil.ReadAll(res.Body)
os.Stdout.Write(country)
}
Racket
(require net/url)
(define country (port->string (get-pure-port (string->url "https://api.ipdata.co/country_name?api-key=<<apiKey>>"))))
(printf "My country is: ~a" country)
Lisp
;This example requires the drakma http package installed.
;It can be found here: http://www.weitz.de/drakma
(let ((stream
(drakma:http-request "https://api.ipdata.co/country_name?api-key=<<apiKey>>" :want-stream t)))
(let ((public-ip (read-line stream)))
(format t "My country is: ~A" public-country)))
Xojo
Dim s As New HTTPSecureSocket
Dim t As String = s.Get("https://api.ipdata.co/country_name?api-key=test",10)
MsgBox "My country is: " + t
Scala
val country = scala.io.Source.fromURL("https://api.ipdata.co/country_name?api-key=<<apiKey>>").mkString
println(s"My country is: $country")
Javascript
<script type="application/javascript">
function getIP(json) {
document.write("My country is: ", json.country);
}
</script>
<script type="application/javascript" src="https://api.ipdata.co/country_name?api-key=<<apiKey>>?format=jsonp&callback=getCountry"></script>
JQuery
<script type="application/javascript">
$(function() {
$.getJSON("https://api.ipdata.co/country_name?api-key=<<apiKey>>?format=jsonp&callback=?",
function(json) {
document.write("My country is: ", json.country);
}
);
});
</script>
C#
using System;
using System.Net;
namespace Ipdata.Examples {
class Program {
public static void Main (string[] args) {
WebClient webClient = new WebClient();
string publicCountry = webClient.DownloadString("https://api.ipdata.co/country_name?api-key=<<apiKey>>");
Console.WriteLine("My country is: {0}", publicCountry);
}
}
}
Elixir
:inets.start
{:ok, {_, _, inet_addr}} = :httpc.request('http://api.ipdata.co?api-key=<<apiKey>>')
:inets.stop
nim
import HttpClient
var country = newHttpClient().getContent("https://api.ipdata.co/country_name?api-key=test")
echo("My country is: ", country)
Powershell
$response = Invoke-RestMethod -Uri 'https://api.ipdata.co?api-key=<<apiKey>>'
"My country is: $($response.country_name)"
Lua
http.Fetch("https://api.ipdata.co/country_name?api-key=<<apiKey>>", function(body) print("My country is: " .. body ) end
PureBasic
InitNetwork()
*Buffer = ReceiveHTTPMemory("https://api.ipdata.co/?api-key=<<apiKey>>")
If *Buffer
ParseJSON(0, PeekS(*Buffer, MemorySize(*Buffer), #PB_UTF8))
FreeMemory(*Buffer)
Debug GetJSONString(GetJSONMember(JSONValue(0), "country_name"))
EndIf
LiveCode
put "My country is" && url "https://api.ipdata.co/country_name?api-key=<<apiKey>>"
Objective-C
NSURL *url = [NSURL URLWithString:@"https://api.ipdata.co/country_name?api-key=test/"];
NSString *country = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"My country is: %@", country);
Arduino
if (client.connect("api.ipdata.co?api-key=test", 80)) {
Serial.println("connected");
client.println("GET / HTTP/1.0");
client.println("Host: api.ipdata.co/country_name?api-key=<<apiKey>>");
client.println();
} else {
Serial.println("connection failed");
}
Updated 9 months ago