Entwickler

Finnish translation here.

Create an account to get the API key.

Prior to requests for a server, the API key is mandatory. The API key will find your account settings from the page.

When a user registers an API key is created automatically. The API key is mandatory to add the request to the api parameter (See example below).

Sending a request
When submitting the request, the parameters api and url are mandatory. See the example below. To use a custom alias, simply add & custom = end.

GET https://1u.fi/api?api=APIKEY&url=SHORTENEDURL_URL&custom=CUSTOMALIAS

Server response
The server response will be in the encoded JSON format (default). This is done to work in many different programming languages. The first element of the answer tells you whether there were any errors (error: 1) or (error: 0). The next element changes according to the errors. However, if an error occurs, the name of the next element changes to "msg": contains the original error message, otherwise the name remains "short" containing the abbreviated URL. (See example below.)

// No mistakes
{
    "error": 0,
    "short": "https:\/\/1u.fi\/DkZOb"
}
// An error occurred
{
    "error": 1,
    "short": "Please enter a valid email"

}

Use text format
You can now also send a request to get the answer as text, simply add &format=text to the end of the request. For example: https://1u.fi/DkZOb from JSON's farm. Please note that if a request fails, the server will not respond.

 

Using API in PHP
To use API in your php program you need to send a request through file_get_contents or cURL. Both are fully functional options.

// Creating a new variable that includes the url of the 1u.fi API.
$ api_url = "http://1u.fi/api?api=APIKEY&url=SHORTENEDURL_URL&custom=CUSTOMALIAS"; // Downloading JSON 1u.fi API and "decoding" it with the PHP json_decode function.
$ jsonResponse = @json_decode (file_get_contents ($ api_url), true); if ($ jsonResponse ["error"])
{
    // If an error occurred while shortening the link, an error is printed.
    echo $ jsonResponse ["msg"];
}
else
{
    // Everything went down very short, a new short link was printed.
    echo $ jsonResponse ["short"];
} 
// You can also use the 1u.fi API's "format" parameter. // "text" returns the response from the server in plain text format. $ api_url = "https://1u.fi/api?api=APIKEY&url=SHORTENEDURL_URL&custom=CUSTOMALIAS&format=text"; $ plainTextResponse = file_get_contents ($ api_url);
if ($ plainTextResponse)
{
    // Request server successfully, print plain text response.
    echo $ plainTextResponse;
}


Using API with C #
In this example, we use Newtonsoft's "Json.NET" library to parse the JSON response.

using System;
using System.Net;
using Newtonsoft.Json.Linq;
// Create a new WebClient object for sending web requests.
using (var client = new WebClient ())
{
    // Loading from JSON 1u.fi API.
    var data = client.DownloadString (
        "https://1u.fi/api?api=APIKEY&url=SHORTENEDURL_URL"
    );
 
    // Parsing JSON text into "JObject" object.
    var json = JObject.Parse (data);
 
    if ((int) json ["error"] == 1)
    {
        // If an error occurred while shortening the link, an error is printed.
        Console.Writeline (JSON [ "msg"]);
    }
    else
    {
        // Everything went down very short, a new short link was printed.
        Console.Writeline (JSON [ "short"]);
    }
}
You can also download the open-source 1u.fi.Api library here!

 

Use the API with jQuery
In this example, we use JavaScript library called jQuery to send and parse the JSON response.

// When downloading a website, jQuery's own ready function is performed.
$ (Window) .ready (function ()
{
    // An AJAX request is sent to the server
    $ .ajax ({
        url: "http://1u.fi/api?api=APIKEY&url=SHORTENEDURL_URL&custom=CUSTOMALIAS",
        type: "GET",
        crossDomain: true,
        dataType: "jsonp",
        success: function (date)
        {
            // The request was successful, a new short address is added to the browser console.
            console.log (data.short);
        },
        error: function (data)
        {
            // Request failed, write error to console.
            console.log (data);
        }
    });
});