Skip to main content Accessibility Feedback

How to shorten a URL with vanilla JavaScript and the Shrtcode API

Shrtcode is a free link shortening service.

They have a web GUI, but they also have an API you can call to shorten URLs. Today, we’re going to look at how to use it to shorten URLs.

The API endpoint is https://api.shrtco.de/v2/shorten, with the url to shorten as a query string parameter.

To shorten the URL for my website, I would do this.

// The URL to shorten
let url = 'https://gomakethings.com';

// Call the API
let request = await fetch(`https://api.shrtco.de/v2/shorten?url=${encodeURIComponent(url)}`);

I’m using async and await in this example. I’m also passing the url into the encodeURIComponent() function to encode it for use in a URL.

Next, you would use the Response.json() method to convert the API response into an object.

// Call the API
let request = await fetch(`https://api.shrtco.de/v2/shorten?url=${encodeURIComponent(url)}`);

// Get the response
let response = await request.json();

The API response object has a few properties and variants under the result property. The one we want is full_short_link.

// The shortened URL
let shortened = response.result.full_short_link;