FOR DEVELOPERS

Free API to get your public IP address

Two simple endpoints that return the IP address of the machine that calls them: /ip for plain text and /ip.json for JSON with approximate location.

Try it
These buttons call the same endpoints as the examples below, so you see the live response for your own connection.
Endpoints

Two endpoints, no sign-up

There is nothing to register for and no API key. Make a plain GET request to either address:

EndpointReturnsContent type
/ipYour public IP address as plain text, followed by a newlinetext/plain
/ip.jsonYour IP address with approximate location as JSONapplication/json

The address returned is always the one that made the request, which is why the service can only tell you about your own connection. It cannot look up any other address (the IP lookup page does that). Responses are never cached, so you always get the current address.

Plain text

Examples: /ip

curl
curl https://www.whatismypublicip.com/ip
Force IPv4
curl -4 https://www.whatismypublicip.com/ip
Force IPv6
curl -6 https://www.whatismypublicip.com/ip
wget
wget -qO- https://www.whatismypublicip.com/ip
Windows PowerShell
Invoke-RestMethod https://www.whatismypublicip.com/ip
Bash variable
MYIP=$(curl -s https://www.whatismypublicip.com/ip)

Always use the full https://www. address. The shorter form without www is redirected first, and some tools do not follow redirects.

JSON

Examples: /ip.json

curl
curl https://www.whatismypublicip.com/ip.json
curl with jq
curl -s https://www.whatismypublicip.com/ip.json | jq .ip
PowerShell
(Invoke-RestMethod https://www.whatismypublicip.com/ip.json).ip
Python (requests)
import requests
data = requests.get("https://www.whatismypublicip.com/ip.json", timeout=10).json()
print(data["ip"], data["country_code"])
Node.js (fetch)
const r = await fetch("https://www.whatismypublicip.com/ip.json");
const data = await r.json();
console.log(data.ip);
PHP
$data = json_decode(file_get_contents("https://www.whatismypublicip.com/ip.json"), true);
echo $data["ip"];

Sample response

{
  "ok": true,
  "ip": "203.0.113.9",
  "version": 4,
  "country_code": "US",
  "continent": "NA",
  "region": "New Jersey",
  "region_code": "NJ",
  "city": "Newark",
  "postal": "07102",
  "timezone": "America/New_York",
  "latitude": 40.7357,
  "longitude": -74.1724,
  "location_source": "cloudflare"
}

Response fields

FieldTypeDescription
okbooleantrue when the request worked.
ipstringThe public IP address your request came from, IPv4 or IPv6.
versionnumber4 or 6.
country_codestringTwo-letter country code, such as US. Empty when unknown.
continentstringTwo-letter continent code. Empty when unknown.
region, region_codestringState or province name and its code. Empty when unknown.
city, postalstringApproximate city and postal code. Empty when unknown.
timezonestringIANA time zone name, such as America/New_York. Empty when unknown.
latitude, longitudenumber or nullApproximate coordinates of the IP address, or null when unknown.
location_sourcestringcloudflare when location fields were supplied, none otherwise.

Location is estimated from the IP address by Cloudflare, our network provider, so it is usually accurate to a city or region and sometimes wrong, especially on mobile, VPN and satellite connections. Treat missing fields as normal: empty strings and null mean the value was not available.

Good to know

Usage notes

IPv4 or IPv6

The address you get depends on which protocol your client used to connect. In curl, -4 and -6 choose one explicitly. If you only have IPv4 you will not get an IPv6 address.

Errors

If the address cannot be determined, /ip returns HTTP 400 with a short message, and /ip.json returns HTTP 400 with {"ok": false, "error": "..."}. Check the status code before using the body.

Use it politely

The service is free. Please cache the result for a few minutes rather than calling it for every request or on a tight loop. Traffic heavy enough to affect the site may be blocked, and no uptime guarantee is made.

Calling it from a web page

The endpoints do not send CORS headers, so JavaScript running on another website cannot read the response in a browser. Call it from a server, a script or the command line. Websites that need the visitor's address should read it on their own server.

Privacy

The address is read from your request and returned to you. See the privacy policy for how request data is handled.

FAQ

Frequently asked questions

How do I get my public IP address in a script?

Send a GET request to https://www.whatismypublicip.com/ip. For example, curl https://www.whatismypublicip.com/ip prints your address and nothing else, so it can go straight into a variable.

Is there an API key or sign-up?

No. Both endpoints are free and open. Please keep usage reasonable and cache results.

Can I look up another IP address with this API?

No. It only returns the address of whoever makes the request. To look up any IP address, use the IP lookup page.

How do I get an IPv6 address from the command line?

Run curl -6 https://www.whatismypublicip.com/ip. It works only if your connection has working IPv6. You can check that with the IPv6 test.

How accurate is the location in /ip.json?

Location comes from IP geolocation data, which is generally right at country level and less reliable for city and postal code. It describes where the network address is registered or routed, not the exact place of the device.

Can I use this API in a mobile app or a website?

From a server or from your own backend, yes. A web page running in someone else's browser cannot read the response because the service does not send CORS headers. If you build a client app, keep traffic light and cache results.