In this quick tutorial we’ll learn about how to use Alexa‘s public API to retrieve the global rank for a given website.

Alexa provides a public API with an XML response containing the queried domain rank data, here’s the API endpoint URI:

Get Alexa Site Rank with Python:

Python is the best for this, running incredibly fast and on every platform. To get us going, open a new file named `alexa` and place the following code into it:

#!/usr/bin/env python
import urllib, sys, re
xml = urllib.urlopen(''%sys.argv[1]).read()
try: rank = int('<POPULARITY[^>]*TEXT="(\d+)"', xml).groups()[0])
except: rank = -1
print 'Your rank for %s is %d!\n' % (sys.argv[1], rank)

and save the file. Next up, make it executable by running

chmod +x alexa

. Now you’re done, and to get a rank run the following:


which should output:

Your rank for is 1!

Awesome. You might want to create an alias for this command up next, so you could run

alexa site.tld

easily everywhere in your command line:

nano ~/.bashrc
# towards the bottom, add the following code in a new line:
alias alexa='/path/to/alexa $@'
# save the file (Ctrl+x then y then ENTER)

To have alexa command available, you’ll want to restart the terminal (as it loads the .bashrc file on load) or just reload the .bashrc file with:

. ~/.bashrc

And now, you must have it!


Get Alexa Site Rank with PHP:

With PHP, it’s quite easier as well and less code could do the job, just as the case with Python. Open a new file named alexa.php and write the following code into it:

#!/usr/bin/env php

function alexa_rank($site) {
    $xml = file_get_contents("{$site}");

    if ( $xml ) {
        preg_match('/<POPULARITY[^>]*TEXT="(\d+)"/si', $xml, $rank);
        return isset($rank[1]) ? (int) $rank[1] : -1;

    return -1;

if ( 'cli' == php_sapi_name() ) {
    if ( isset($argv[1]) ) {
        printf ( 'Your rank for %s is %d!%s', $argv[1], alexa_rank($argv[1]), PHP_EOL );
} else {
    // custom usage, also remove the file header line if not running with CLI
    $domain = '';
    printf ( 'Your rank for %s is %d!%s', $domain, alexa_rank($domain), PHP_EOL );

And save the file. You can then use it in the command line:

php alexa.php

, `chmod +x` as well if you want to run it without `php` command, or use it in your custom project.

Get Alexa Site Rank with JavaScript:

You can do this in the front-end as well, the only very first challenge you’ll meet is the SAMEORIGIN policy which will prevent you from making XHR requests to the Alexa API. You can get over this by using combining either Python or PHP solutions above to create a server-side API response from your domain and then have access to it, or just use CORS-anywhere project as a proxy.

Here’s the full JS code, which is in native JS which means it could be wrapped in jQuery or any other library:

var domain = ''
  , req = new XMLHttpRequest()
  , cors = '';
req.addEventListener('load', function(){
    var r, n;
    r = /<POPULARITY[^>]*TEXT="(\d+)"/g
    n = r.exec(this.response)
    console.log( 'Your rank for '+domain+' is '+( null != n ? n[1] : -1 )+'!' )
});'GET', cors+''+domain);
Digital Ocean

Cheap Cloud SSD Hosting

Get a VPS now starting at $5/m, fast and perfect for WordPress and PHP applications

Sign Up with $10 Credit