Skip to content

Get your public IP in code (Python, Node.js, Go)

To get your public IP address in code, send an HTTP GET request to https://ipconfig.io and read the response body — it returns your public IP as plain text with a trailing newline. Request https://ipconfig.io/json instead when you want the full record (country, city, coordinates, time zone, and the owning network/ASN) as JSON. No API key, no signup, and no third-party library — every example below uses only the language's standard library.

Maintained by the ipconfig.io team · Reviewed 17 June 2026

The two endpoints you'll use:

  • / returns just your public IP plus a newline — perfect for shells and code that needs one value.
  • /json returns the full record (geolocation + ASN) as JSON.

How do I get my public IP in Python?

In Python, use the standard-library urllib.request to GET https://ipconfig.io and strip the trailing newline — no pip install required:

python
import urllib.request

with urllib.request.urlopen("https://ipconfig.io") as resp:
    ip = resp.read().decode().strip()

print(ip)  # e.g. 203.0.113.42

For the full record, request /json and parse it with json.loads:

python
import json
import urllib.request

with urllib.request.urlopen("https://ipconfig.io/json") as resp:
    data = json.loads(resp.read())

print(data["ip"])       # 203.0.113.42
print(data["country"])  # United States
print(data["asn_org"])  # Owning network, e.g. "Google LLC"

data is a plain dict, so every field — country_iso, city, latitude, time_zone, asn, and the rest — is available by key.

How do I get my public IP in Node.js?

In Node.js 18 and later, use the built-in fetch to request https://ipconfig.io and trim the response text — no node-fetch or axios needed:

js
const res = await fetch("https://ipconfig.io");
const ip = (await res.text()).trim();

console.log(ip); // e.g. 203.0.113.42

For the full record, request /json and call .json() on the response:

js
const data = await fetch("https://ipconfig.io/json").then((r) => r.json());

console.log(data.ip);      // 203.0.113.42
console.log(data.country); // United States
console.log(data.asn_org); // Owning network, e.g. "Google LLC"

Run these inside an async function, or save the file as .mjs to use top-level await.

How do I get my public IP in Go?

In Go, use net/http to GET https://ipconfig.io and read the body — both are in the standard library:

go
package main

import (
	"fmt"
	"io"
	"net/http"
	"strings"
)

func main() {
	resp, err := http.Get("https://ipconfig.io")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}

	ip := strings.TrimSpace(string(body))
	fmt.Println(ip) // e.g. 203.0.113.42
}

For the full record, decode /json into a struct with encoding/json. Define only the fields you need:

go
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
)

type Record struct {
	IP      string `json:"ip"`
	Country string `json:"country"`
	ASNOrg  string `json:"asn_org"`
}

func main() {
	resp, err := http.Get("https://ipconfig.io/json")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	var rec Record
	if err := json.NewDecoder(resp.Body).Decode(&rec); err != nil {
		panic(err)
	}

	fmt.Printf("%s%s (%s)\n", rec.IP, rec.Country, rec.ASNOrg)
}

Add more json-tagged fields to the struct — such as City, Latitude, or ASN — to read the rest of the record.

How do I look up a different IP address from code?

Append ?ip=<addr> to any endpoint to look up an address other than your own. The same request shape works in every language:

python
import json, urllib.request

with urllib.request.urlopen("https://ipconfig.io/json?ip=1.1.1.1") as resp:
    data = json.loads(resp.read())

print(data["country"], data["asn_org"])  # Australia Cloudflare, Inc.

This is useful for enriching server logs or resolving the country and network behind an inbound address.

Should I cache the result instead of calling on every request?

Yes — fetch your public IP once and reuse it rather than calling the service on every request. Your public IP rarely changes within a session, so a single lookup at startup (or a short-lived cache) is enough. For automated checks, you can also poll https://ipconfig.io/health to confirm the service is reachable before relying on it.

Frequently asked questions

How do I get my public IP address in code? Send an HTTP GET to https://ipconfig.io and read the body — it's your public IP plus a newline. Use /json instead for the full geolocation and ASN record.

What's the difference between / and /json?/ returns only the IP as plain text (one value, ideal for code); /json returns the full record — country, city, coordinates, time zone, and ASN — as JSON.

Do I need an API key or a library? No. There's no key and no signup, and every example here uses only the standard library: urllib in Python, built-in fetch in Node.js 18+, and net/http in Go.

Next steps

Geolocation by MaxMind GeoLite2. No tracking, no keys.