API

This tutorial will show you how to use the API of LNKS.ES - URL Shortener and Link Management Platform.

Registering for an API Key

As before, an API key is required for requests to be processed by the system. Once a user registers, an API key is automatically generated for this user. The API key must be attached to request via the api parameter (see full example below). You can check your API key on this page: https://lnks.es/user/tools/api (ONLY after login)

Sending a request for shortening a URL

To send a request, the user must use the following format where the variables api and url are required. In the example below, the URL of the demo is used but you should use your own domain name. To request a custom alias, simply add &custom= at the end.
GET https://lnks.es/api?key=APIKEY&url=THELONGURLTOBESHORTENED&custom=CUSTOMALIAS
Parameter
Description
key
(required) Your API key.
url
(required) Long URL to shorten.
custom
(optional) Custom alias instead of random alias.
type
(optional) Redirection type. [direct, frame, splash]
password
(optional) Password protection.
format
(optional) Output format. Default is json. [json, text]

Server response

As before, the response will encoded in JSON format (default). This is done to facilitate cross-language usage. The first element of the response will always tell if an error has occurred (error: 1) or not (error: 0). The second element will change with respect to the first element. If there is an error, the second element will be named “msg”. which contains the source of error, otherwise it will be named “short” which contains the short URL. (See below for an example)
// No errors
{
  "error":0,
  "short":"https:\/\/lnks.es\/DkZOb"
}
// An error has occurred
{
  "error":1,
  "msg":"Please enter a valid URL"
}

USING PLAIN TEXT FORMAT

You can now request the response to be in plain text by just adding &format=text at the end of your request. This will return just https://lnks.es/DkZOb instead of the JSON response. Note that if an error occurs, it will not output anything.

Statistics data for a short URL

You can get more detail on a short URL by using the following endpoint along with the following parameters.
GET https://lnks.es/api/details
Parameter
Description
key
(required) Your API key.
alias
(required) Short URL alias.

All of your Short URLs

You can access all of your short urls on your account by using the following endpoint along with the following parameters.
GET https://lnks.es/api/urls
Parameter
Description
key
(required) Your API key.

Using the API in PHP

To use the API in your PHP application, you have to send a GET request through file_get_contents or cURL: Both are reliable methods. You can see a sample code below using file_get_contents.
  // Using JSON Response
  $api_url="http://lnks.es/api?key=APIKEY&url=THELONGURLTOBESHORTENED&custom=CUSTOMALIAS";
  $res= @json_decode(file_get_contents($api_url),TRUE);
  if($res["error"]){
    echo $res["msg"];
  }else{
    echo $res["short"];
  }
  // Using Plain Text Response
  $api_url="http://lnks.es/api?api=APIKEY&url=THELONGURLTOBESHORTENED&custom=CUSTOMALIAS&format=text";
  $res= @file_get_contents($api_url);
  if($res){
    echo $res;
  }
?>

SAMPLE FUNCTION

Here is a sample function you can use to generate short URLs procedurally.
define("SHORTENER_URL", "https://lnks.es");
define("SHORTENER_KEY", "123456789");
/**** Sample PHP Function ***/
function shorten($url, $custom = "", $format = "json") { 
  $api_url = SHORTENER_URL."/api/?key=".SHORTENER_KEY;
  $api_url .= "&url=".urlencode(filter_var($url, FILTER_SANITIZE_URL));
    if(!empty($custom)){
      $api_url .= "&custom=".strip_tags($custom);
    }
  $curl = curl_init();
  curl_setopt_array($curl, array(
      CURLOPT_RETURNTRANSFER => 1,
      CURLOPT_URL => $api_url
  ));
  $Response = curl_exec($curl);
  curl_close($curl);
  
  if($format == "text"){
    $Ar = json_decode($Response,TRUE);
      if($Ar["error"]){
          return $Ar["msg"];
      }else{
          return $Ar["short"];
      }
  }else{
    return $Response;
  }
}
?>