Getting my public IP via API

59,040

Solution 1

While not from big companies, all of these should work:

curl icanhazip.com
curl -s 'http://checkip.dyndns.org' | sed 's/.*Current IP Address: \([0-9\.]*\).*/\1/g'
host -t a dartsclink.com | sed 's/.*has address //'
curl curlmyip.com
curl ifconfig.me # this has a lot of different alternatives too, such as ifconfig.me/host

Source

Solution 2

You can get your IP, and a whole bunch of other information, from my service http://ipinfo.io:

$ curl ipinfo.io
{
  "ip": "67.188.232.131",
  "hostname": "c-67-188-232-131.hsd1.ca.comcast.net",
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "loc": "37.385999999999996,-122.0838",
  "org": "AS7922 Comcast Cable Communications, Inc.",
  "phone": 650
}

If you want just the IP:

$ curl ipinfo.io/ip
67.188.232.131

That also works for other fields, eg:

$ curl ipinfo.io/org
AS7922 Comcast Cable Communications, Inc.

More details available at http://ipinfo.io/developers

Solution 3

In JSON format giving more details like the country and the user-agent:

https://www.trackip.net/ip?json

or only the IP

https://www.trackip.net/ip

for pfsense (Check IP Services):

http://trackip.net/pfsense

Solution 4

I use this one, easy to remember and it's shorter than others:

$ curl ifconfig.me

PROTIP: Remember that if you use curl in shell scripts you have to use -s option, otherwise it prints the download progress table.

$ echo `curl -s ifconfig.me`

Share:
59,040

Related videos on Youtube

Slava V
Author by

Slava V

Updated on July 05, 2022

Comments

  • Slava V
    Slava V over 1 year

    Is there a public API from some big company to get my public ip from within a program?

    I've found http://ip-address.domaintools.com/myip.xml, which is exactly what I want, but unfortunately it blocks Python's urllib as well as many other http libraries, unless you spoof user-agent (anything doing that can't be called an API at all).

    The problem is that I need this data for some potentially open source library, so we have to play by the rules. I also know that I can just do smth like print $_SERVER[REMOTE_ADDR], but I don't want to server traffic to everyone using my library.

    So, is there something like the URL above, but real API, that allows robots?

  • Slava V
    Slava V almost 14 years
    That's awesome! Thanks a lot, esp. for dyndns one, that'll do!
  • mnagel
    mnagel over 10 years
    icanhazip.com returns the cleanest result imho
  • Ben Dowling
    Ben Dowling over 10 years
  • Navin
    Navin over 10 years
    Does not support HTTPS
  • Navin
    Navin over 10 years
    @mnagel It also seems to be the only one that supports HTTPS. How ironic...
  • Ben Dowling
    Ben Dowling about 10 years
    The paid plans all have HTTPS support. See ipinfo.io/pricing
  • rodvlopes
    rodvlopes about 9 years
    I am moving from ifconfig.me to ipinfo.io due to slowness
  • Yoan Tournade
    Yoan Tournade over 7 years
    You may check ipify.org, that have a clear API and does support HTTPS.
  • Ryan H.
    Ryan H. over 7 years
    icanhazip.com is so far the only one I've encountered that is both IPv6 capable and ATS (TLSv1.2 & PFS) compliant. Now, if I can just find an API set like ipv6-test.com/api that allows forcing IPv4 or IPv6 and is ATS compliant, that would be terrific!
  • نرم افزار حضور و غیاب
    نرم افزار حضور و غیاب over 6 years
    ip-api.com/json is fastest way and it has best response time. 150 requests/minute
  • rustyx
    rustyx about 4 years
    Nice. Also supports IPv6.
  • rustyx
    rustyx about 4 years
    No IPv6 support...
  • rustyx
    rustyx about 4 years
    No IPv6 support...
  • rustyx
    rustyx about 4 years
    Nice, icanhazip.com supports IPv6.
  • Ben Dowling
    Ben Dowling about 4 years
    It supports IPv6 via v6.ipinfo.io
  • Raman
    Raman about 4 years
    Looks like https is now supported, no paid plan required.
  • Ayan Sengupta
    Ayan Sengupta over 3 years
    Supports https too. Upvoted.
  • Pierre Carrier
    Pierre Carrier about 2 years
    @RyanH. how about api.ident.me? Provides IPv4 vs IPv6, supports HTTPS and TLSv1.2 (works in nscurl). Disclaimer: I'm the author.