Links

Code Samples

To use the API easily, we wrote some codes in multiple programming languages to send a request to our API.

PHP

<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://gregeoip.com/GeoIP?key=YOUR_API_KEY',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET'
));
$response = curl_exec($curl);
curl_close($curl);
// Output
echo $response;

Javascript (jQuery)

var settings = {
"url": "https://gregeoip.com/GeoIP?key=YOUR_API_KEY",
"method": "GET",
"timeout": 0
};
$.ajax(settings).done(function (response) {
console.log(response);
});

Javascript (Vanilla)

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("GET", "https://gregeoip.com/GeoIP?key=YOUR_API_KEY");
xhr.send();

Go

package main
import (
"fmt"
"net/http"
"io/ioutil"
)
func main() {
url := "https://gregeoip.com/GeoIP?key=YOUR_API_KEY"
method := "GET"
client := &http.Client {
}
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println(err)
return
}
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

Java

OkHttpClient client = new OkHttpClient().newBuilder()
.build();
Request request = new Request.Builder()
.url("https://gregeoip.com/GeoIP?key=YOUR_API_KEY")
.method("GET", null)
.build();
Response response = client.newCall(request).execute();

VB.NET

Add this to the top of your project:
Imports System.Net
Imports System.IO
Imports System.Linq
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
This is the code:
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Try
request = DirectCast(WebRequest.Create("https://gregeoip.com/GeoIP?key=YOUR_API_KEY"), HttpWebRequest)
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Dim rawresp As String
rawresp = reader.ReadToEnd()
Dim jResults As JObject = JObject.Parse(rawresp)
Dim results As List(Of JToken) = jResults.Children().ToList()
For Each item As JProperty In results
item.CreateReader()
msgbox(item.Value("countryName"))
Next
Catch ex As Exception
MsgBox(ex.ToString)
Finally
If Not response Is Nothing Then response.Close(
End Try

C#

var client = new RestClient("https://gregeoip.com/GeoIP?key=YOUR_API_KEY");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

Swift

Please follow this link to retrieve the JSON data from our IP. Don't forget to replace the URL.

Objective-C

Please follow this link to retrieve the JSON data from our IP. Don't forget to replace the URL.

Kotlin

Please follow this link to retrieve the JSON data from our IP. Don't forget to replace the URL.

Python

import http.client
conn = http.client.HTTPSConnection("gregeoip.com")
payload = ''
headers = {}
conn.request("GET", "/GeoIP?key=YOUR_API_KEY", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Perl

#!/usr/bin/perl
use LWP::UserAgent;
use HTTP::Request;
my $URL = 'https://gregeoip.com/GeoIP?key=YOUR_API_KEY';
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
my $header = HTTP::Request->new(GET => $URL);
my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $ua->request($request);
if ($response->is_success){
print "URL:$URL\nHeaders:\n";
print $response->headers_as_string;
}elsif ($response->is_error){
print "Error:$URL\n";
print $response->error_as_HTML;
}

Ruby

require "uri"
require "net/http"
url = URI("https://gregeoip.com/GeoIP?key=YOUR_API_KEY")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Get.new(url)
response = https.request(request)
puts response.read_body

C++

#include <iostream>
#include <string>
#include <curl/curl.h>
#include <jsoncpp/json/json.h>
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main(void)
{
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://gregeoip.com/GeoIP?key=YOUR_API_KEY");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
Json::Reader reader;
Json::Value obj;
reader.parse(readBuffer, obj);
std::cout << obj["countryName"].asString() << std::endl;
return 0;
}

Bash

curl -s "https://gregeoip.com/GeoIP?key=YOUR_API_KEY" | grep -Pom 1 '"countryName": "\K[^"]*'