Own-IP request examples
Each example calls the same endpoint. The connecting process determines whose IP address is returned.
Use a JavaScript module or browser console with native fetch and AbortSignal.timeout.
const response = await fetch(
"https://api.ipquery.io/?format=json",
{ signal: AbortSignal.timeout(5000) }
);
if (!response.ok) {
const message = await response.text();
throw new Error("IPQuery " + response.status + ": " + message);
}
const data = await response.json();
console.log(JSON.stringify(data, null, 2));
Run in a shell with cURL. HTTP errors exit nonzero; diagnostics go to stderr.
curl --fail --silent --show-error --max-time 5 \
"https://api.ipquery.io/?format=json"
Python 3, standard library only. The timeout applies to socket operations.
import json
from urllib.request import urlopen
with urlopen("https://api.ipquery.io/?format=json", timeout=5) as response:
data = json.load(response)
print(json.dumps(data, indent=2))
Go, standard library only. Save as main.go and run go run main.go.
package main
import (
"io"
"log"
"net/http"
"os"
"time"
)
func main() {
client := &http.Client{Timeout: 5 * time.Second}
response, err := client.Get("https://api.ipquery.io/?format=json")
if err != nil { log.Fatal(err) }
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
log.Fatalf("IPQuery %s", response.Status)
}
if _, err := io.Copy(os.Stdout, response.Body); err != nil {
log.Fatal(err)
}
}
The examples use five-second timeouts and the language’s standard HTTP tools. Handle failures in your application; do not substitute an empty IP record.