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.
Two endpoints, no sign-up
There is nothing to register for and no API key. Make a plain GET request to either address:
| Endpoint | Returns | Content type |
|---|---|---|
| /ip | Your public IP address as plain text, followed by a newline | text/plain |
| /ip.json | Your IP address with approximate location as JSON | application/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.
Examples: /ip
curl https://www.whatismypublicip.com/ipcurl -4 https://www.whatismypublicip.com/ipcurl -6 https://www.whatismypublicip.com/ipwget -qO- https://www.whatismypublicip.com/ipInvoke-RestMethod https://www.whatismypublicip.com/ipMYIP=$(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.
Examples: /ip.json
curl https://www.whatismypublicip.com/ip.jsoncurl -s https://www.whatismypublicip.com/ip.json | jq .ip(Invoke-RestMethod https://www.whatismypublicip.com/ip.json).ipimport requests
data = requests.get("https://www.whatismypublicip.com/ip.json", timeout=10).json()
print(data["ip"], data["country_code"])const r = await fetch("https://www.whatismypublicip.com/ip.json");
const data = await r.json();
console.log(data.ip);$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
| Field | Type | Description |
|---|---|---|
| ok | boolean | true when the request worked. |
| ip | string | The public IP address your request came from, IPv4 or IPv6. |
| version | number | 4 or 6. |
| country_code | string | Two-letter country code, such as US. Empty when unknown. |
| continent | string | Two-letter continent code. Empty when unknown. |
| region, region_code | string | State or province name and its code. Empty when unknown. |
| city, postal | string | Approximate city and postal code. Empty when unknown. |
| timezone | string | IANA time zone name, such as America/New_York. Empty when unknown. |
| latitude, longitude | number or null | Approximate coordinates of the IP address, or null when unknown. |
| location_source | string | cloudflare 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.
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.
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.