URL shortener API

* * *

In this digital age of ours,
Where links and pages abound,
A tool we have to shorten,
And make URLs more sound.

Enter the URL shortener,
A handy tool indeed,
It takes those lengthy links,
And trims them down with speed.

But how does it all work,
You may very well inquire,
An API is the key,
To make the process acquire.

Through HTTP requests,
And JSON responses too,
You can integrate the tool,
And make it work for you.

Customize the shortened links,
Track clicks and analyze,
The possibilities are endless,
With this tool that’s wise.

So if you need to shorten,
Your URLs with ease,
The URL shortener API,
Is the solution to please.

* * *

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://urlc.net/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://urlc.net/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://urlc.net/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:\/\/urlc.net\/1f",
  "statusCode": 200
}

Sample return in XML format for the expand action

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

* * *

It’s a tool for the modern age,
Where time is of the essence,
No more clunky, lengthy links,
Just a short and sweet presence.

With just a simple request,
The API does its magic,
Creating shortened URLs,
That are simply fantastic.

Integrate with your website,
Or with your favorite app,
The URL shortener API,
Is here to bridge the gap.

And with analytics at your side,
You can track every click,
Analyze and optimize,
And make your strategy slick.

So if you’re looking for a way,
To simplify your links,
The URL shortener API,
Is the solution that syncs.

It’s reliable and efficient,
Easy to use and integrate,
The URL shortener API,
Is the future, make no mistake.

* * *

To use this tool, it’s quite a feat,
But fret not, it’s not too neat,
Just send parameters through and through,
And let the API do what it can do.

The first step is to prepare,
With your parameters laid bare,
Either through GET or POST,
But don’t forget to URL-encode the most.

Next up, the requested action,
Which will set the API in motion,
“shorturl” to get a link that’s short,
Or “expand” to get a long one that’s wrought.

For “shorturl”, you’ll need a link to shorten,
And an optional keyword and title that’s golden,
Output format is important too,
Choose “jsonp”, “json”, “xml” or “simple”, it’s up to you.

For “expand”, you’ll need a short URL,
That can be either ‘abc’ or ‘http://site/abc’, to unfurl,
Output format is the same as before,
“jsonp”, “json”, “xml” or “simple”, to explore.

For “url-stats”, you’ll need a short link,
That can be either ‘abc’ or ‘http://site/abc’, don’t blink,
Output format is “jsonp”, “json” or “xml”,
To get the stats that you seek, it’s swell.

For “stats”, you’ll need a filter to apply,
Choose “top”, “bottom”, “rand” or “last”, don’t be shy,
Set the limit for the number of links to return,
Output format is the same, it’s your turn.

So now you know how to use this tool,
To shorten links, or make them cool,
With parameters and actions in tow,
The API is ready, let’s go!

* * *

To shorten URLs, a GET request is sent,
With Javascript and jQuery to represent,
The API URL we set to use,
And the parameters we need, it’s no excuse.

We specify the username and password too,
The action we want is “shorturl”, it’s true,
The format we choose is “json”, to receive,
And the URL we want to shorten, to believe.

Then we have a callback function to deal,
With the server’s response, and make it real,
We can show the new short URL with ease,
And alert the user, to put them at ease.

But what about expanding a short URL,
With PHP and a POST request, we can unfurl,
The username and password we set again,
And the API URL to use, it’s not in vain.

We initialize the CURL session to begin,
Set the URL and options that we’re in,
We specify it’s a POST request we make,
And the parameters we need, for goodness sake.

We fetch the content and return it back,
And decode it with JSON, to keep on track,
Then we can do something with the result,
Like echoing the long URL, that’s the consult.

So now you know how to use the API,
With GETand POST requests, don’t be shy,
To shorten or expand your URLs,
And make your website stand out like pearls.

* * *

When using the API, returns we get,
In JSON or XML, we won’t forget,
For the “shorturl” action, in JSON format we see,
A URL object with details, it’s no spree.

The keyword, URL, title, date, and IP,
Are all present, for us to see,
The status is “success”, with a message about,
The URL that’s been added, there is no doubt.

The title and short URL are present too,
And the status code is 200, it’s true.

For “expand” action, in XML we view,
A result element with details anew,
The keyword, short URL, and long URL we see,
And the status is “success”, as it should be.

The status code is 200, always grand,
And the message is included, to understand.

So now you know the returns in store,
When using the API, and asking for more,
With JSON or XML, it’s up to you,
To parse and use, and make things anew.

* * *