Creating short links from JavaScript application

Short links can be created by using:

How to create short links by using JavaScript

  • Click "Create API key".
  • Add a Public key.

2) Use the script below.


Remember to paste appropriate values to the following objects: "domain" and "authorization".

var data = {
    "originalURL": document.getElementById(ID).value
 fetch('', {
    method: 'post',
    headers: {
      'accept': 'application/json',
      'Content-Type': 'application/json',
      'authorization': 'PUBLIC_API_KEY'
    body: JSON.stringify(data)
     return response.json();

3) JSON Response (a new link is created).

Most important parameter here is the "shortURL" (the URL of the newly-created short link).


4) Here's how the script and HTML are operating:


5) Use this code to reproduce the above behavior.

    <h1 class="message">Shorten your first link!</h1>
          <input name="text" type="url" value  id="linkinput" placeholder="Paste a URL to shorten" >
          <input type="submit" id="myinput" value="Shorten">
    <p id="message"></p>
document.getElementById("myinput").onclick = function() {
  var link = document.getElementById("linkinput").value;
  var data = {
    "originalURL": link,
    "allowDuplicates":false }; 
 fetch('', {
    method: 'post',
    headers: {
      'accept': 'application/json',
      'Content-Type': 'application/json',
      'authorization': 'API_KEY'
    body: JSON.stringify(data)
  }) .then(function(response) {
        return response.json();
    document.getElementById("message").innerHTML = "Your short link is " + data.shortURL })

Down below, you can find a video tutorial on creating a public link shortener:

(Video courtesy of Ryder Cragie)