a RESTful API wrapper for TinyURL
TinyURL API, https://tinyurl.com/api-create.php?url=<url-to-shorten>
will be blocked on a normal fetch() or XMLHttpRequest() command because of Cross-Origin Resource Sharing (CORS) policy. This is a RESTful API built on top of the old TinyURL api and will allow you to use TinyURL url shortener using fetch() or XMLHttpRequest() methods.
API URL
https://tinyurl-rest-wrapper.herokuapp.com/shorten?url=<url-to-shorten>
Example usage using fetch()
let url = "https://www.google.com" //url to shorten
fetch(`https://tinyurl-rest-wrapper.herokuapp.com/shorten?url=${encodeURIComponent(url)}`) //encode url using encodeURIComponent() to avoid any possible errors
.then(response => response.json())
.then(json => console.log(json))
If you're planning on using this api for your app and expects high usage traffic, please fork this repo and deploy your very own api. This api is deployed using Heroku and is using the free version. The server can only handle minimal concurrent requests and have a very small compute power. Here's a very good tutorial on deploying the wrapper api in Heroku. You can also deploy this on Google Cloud Platform (GCP), Microsoft Azure, Amazon Web Services (AWS), or in your own server.