featured image for make a free url shortener

Make a free url shortener

In this article I will show you how you can make your own url shortener. Before really starting to make a free url shortener. Let’s first understand a few basics

What is a url shortener?              

A url shortener is actually a service that is provided by a website. This service turns your long and ugly urls into short and easy to remember urls. You might have come across with a few url shorteners like bitly, cuttly and some others. They all have the same prupose that is to shorten a url.

What you will be making?

screenshot of final result of our free url shortener
Working screenshot of url shortener

This is what we are making, the style is not outstanding, I know that. I am focusing more on the functionality and the basics so that you will understand the true concept of making a free url shortener. So, you see that it is converting my original link: https://www.youtube.com/watch?v=dQw4w9WgXcQ to https://shrtco.de/uRfG3I.

Steps involved in making free url shortener

We have to follow these steps to make our free url shortener

  • HTML – for markup
  • CSS – for styling our free url shortener
  • Javascript – to make our free url shortener functional by calling api and listening events

Step 1: HTML

Now Let’s start adding html. Now I am going for a simple page with a form that has a button and an input field where user can paste the original url. That’s all we need and then I will enclose the form in a parent div just to add a background and make form align perfectly in the center.


<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>URL Shortener</title>

  <link rel="stylesheet" href="style.css">


  <div class="wrapper">

    <h1>URL Shortener</h1>

      <form id="form">

          <input id="link" type="text" placeholder="Enter your link">

        <input type="submit" value="shorten it!">


    <ul id="parent" class="shortLinks">



  <script src="script.js"></script>

Step 2: CSS

Now that we have written the html now we have to add css file to the html file. And you can do that by using link tag inside your head tags in html as explained in the snippet below.


<link rel=”stylesheet” href=”style.css”>


Now lets start writing css. First thing that I do in my css file is remove the margin and padding of all the elements. And I do that using “*” which represents all the elements in html and I set margin and padding to zero.


@import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro:[email protected]&display=swap');


Now I will style the parent by giving it width and height and a display of flex. I am setting display flex so that I can align the form in the center by setting justify-content to center.

    background-image:linear-gradient(135deg, rgb(30, 110, 162), salmon);
    padding:50px 0;

I decided to add a gradient because that makes it look cooler.

Now lets style the other elements I have written the code for that here it is:

  ul {list-style-type:none;
  li{margin:30px 0;
    font-family: 'Source Sans Pro', sans-serif;
  a {color:inherit;}

  form{margin:50px 0;
      margin:0 15px;
    border:5px solid lightgrey;
    font-family: 'Source Sans Pro', sans-serif;

Step 3: Javascript

Ok so now we have added the html and css to make it look good. Now we also have to add javascript to make it functional. With the help of javascript we will be able to get user input and process it. We will be using fetch to send a get request to an api service. The api service that I am using is shortcode. First of all lets get user input from the form. And we also have to select the container where we want to output the short link.


const form = document.getElementById("form");
const linkInput = document.getElementById("link");
const parent = document.getElementById("parent");

Now we have to add an event listener on form.

form.addEventListener("submit", async (e) => {
    const originalLink = linkInput.value;
    const apiUrl = `https://api.shrtco.de/v2/shorten?url=${originalLink}`;
      const response = await fetch(apiUrl);
      const data = await response.json();
      let link = document.createElement("li");
      link.className = "output"
      link.innerHTML = `<a href="${data.result.full_short_link}" target="_blank">${data.result.full_short_link} </a>`;
      parent.prepend (link);

There are a lot of things that are happening in this code. Let me explain you what this is. First of all we are sending a get request using fetch. Then we are converting the response from json to javascript object. We are doing this because initially our response is in json format and we have to convert it into a javasrcipt object to be able to use it. So we do that using .json(); method. This is what our object looks like after conversion from json.

screenshot of response from the api for our free url shortener
screenshot of response in the console

You can also check that in your console by using console.log().

Now to display the short link nicely we are making another element using document.create()  this will create an element dynamically whenever a user requests a short link and then we will give it a class by using ( link.className = “output”; )After that we have to add the text and we can do that by using link.innerHTML method and set it equal to the short link from the object.

And at the end we will prepend it the parent container so that new short links appear first using .prepend(link). Instead of using prepend you can also use .appendChild(link); method which will add the new element at the end.

Resources for making free url shortener

Now if you have followed all the steps that I mentioned, your final result will look like this. I have shared my result using codepen so you will be able to see all the things that I have done. And if you have missed something you can check in my code and fix the issues.

API service: https://shrtco.de/

Font: https://fonts.google.com/specimen/Source+Sans+Pro?query=Source+Sans+Pro

Codepen: url shortener

Feel free to use the code and do some experiments. Or you can make your own version of free url shortener by making changes to layout or design and maybe add more functionality to it.

If you are interested in hosting your free url shortener then you can read this article that will show you how you can host your website for free using three different platforms.