URL shortener API

URL shortener API allows:

  • Shorten URL with API.
  • Customize a short URL via API.
  • Get info about shortened URLs.
  • Get short URLs statistics: the most popular shortened links, least popular links, latest short links.
  • Available output formats: TXT, XML, and JSON.

How to use:

Send parameters to https://shortest.link/yourls-api.php either via GET or POST (remember to URL-encode parameters if via GET). These parameters are:

  • Username and password.
  • The requested action"shorturl" (get short URL for a link), "expand" (get long URL of a shorturl), "url-stats" (get stats about one short URL), "stats" (get stats about your links) .
  • With action = "shorturl" :
    • the url to shorten
    • optional keyword and title for custom short URLs
    • output format: either "jsonp""json""xml" or "simple"
  • With action = "expand" :
    • the shorturl to expand (can be either ‘abc’ or ‘http://site/abc’)
    • output format: either "jsonp""json""xml" or "simple"
  • With action = "url-stats" :
    • the shorturl for which to get stats (can be either ‘abc’ or ‘http://site/abc’)
    • output format: either "jsonp""json" or "xml"
  • With action = "stats" :
    • the filter: either "top""bottom" , "rand" or "last"
    • the limit (maximum number of links to return)
    • output format: either "jsonp""json" or "xml"

Sample requests

Example of a GET request with Javascript (using jQuery) to shorten a URL

var api_url  = 'https://shortest.link/yourls-api.php';
var response = $.get( api_url, {
    username: "your_username",
    password: "your_password",
    action:   "shorturl",
    format:   "json",
    url:      "http://site.com/"
    },
    // callback function that will deal with the server response
    function( data) {
        // now do something with the data, for instance show new short URL:
        alert(data.shorturl);
    }
);

Example of a POST request with PHP to expand a short URL

<?php
$username = 'your_username';
$password = 'your_password';
$api_url =  'https://shortest.link/yourls-api.php';

// Init the CURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HEADER, 0);            // No header in the result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
curl_setopt($ch, CURLOPT_POST, 1);              // This is a POST request
curl_setopt($ch, CURLOPT_POSTFIELDS, array(     // Data to POST
        'shorturl' => 'blah1',
        'format'   => 'json',
        'action'   => 'expand',
        'username' => $username,
        'password' => $password
    ));

// Fetch and return content
$data = curl_exec($ch);
curl_close($ch);

// Do something with the result. Here, we echo the long URL
$data = json_decode( $data );
echo $data->longurl;

Sample returns

Sample return in JSON format for the shorturl action

{
  "url": {
    "keyword": "ozh",
    "url": "http:\/\/site.com",
    "title": "Some title \u00ab site.com",
    "date": "2020-11-05 17:10:02",
    "ip": "127.0.0.1"
  },
  "status": "success",
  "message": "http:\/\/site.com added to database",
  "title": "Some title \u00ab site.com",
  "shorturl": "https:\/\/shortest.link\/1f",
  "statusCode": 200
}

Sample return in XML format for the expand action

<result>
    <keyword>blah1</keyword>
    <shorturl>https://shortest.link/blah1</shorturl>
    <longurl>http://site.com/</longurl>
    <message>success</message>
    <statusCode>200</statusCode>
</result>