Tly URL Shortener
URL ShortenersIntroduction to https://t.ly/docs API
https://t.ly/docs is a public API website that provides developers with a wide range of features to shorten, expand and manage URLs. It provides two endpoints to shorten URLs programmatically, one using RESTful API and the other using JSON-RPC.
In this blog post, we will explore how to use this API making the requests in JavaScript with some possible examples.
Shortening a URL
To shorten a URL using https://t.ly/docs API, we need to send a POST request with a JSON payload to https://api.t.ly/v1/shorten. Here's an example:
const url = 'https://www.example.com'
const apiKey = 'YOUR_API_KEY_HERE'
const form = new FormData();
form.append('url', url);
form.append('apiKey', apiKey);
fetch('https://api.t.ly/v1/shorten', {
method: 'POST',
body: form
})
.then(response => response.json())
.then(data => {
console.log(data)
})
.catch(error => {
console.error(error)
})
In the example above, we are using fetch
to send a POST request to the https://api.t.ly/v1/shorten
endpoint with the url
and apiKey
parameters in a FormData
object. The response data is logged to the console.
You will need to replace YOUR_API_KEY_HERE
with your actual API key.
Expanding a URL
To expand a URL, we need to send a GET request to https://api.t.ly/v1/expand/[SHORTENED_URL], where [SHORTENED_URL]
is the shortened URL returned by the /shorten
endpoint. Here's an example:
const shortenedUrl = 'https://t.ly/abcd123'
const apiKey = 'YOUR_API_KEY_HERE'
fetch(`https://api.t.ly/v1/expand/${shortenedUrl}?apiKey=${apiKey}`)
.then(response => response.json())
.then(data => {
console.log(data)
})
.catch(error => {
console.error(error)
})
In the example above, we are using fetch
to send a GET request to the https://api.t.ly/v1/expand/[SHORTENED_URL]
endpoint with the shortenedUrl
and apiKey
parameters in the URL. The response data is logged to the console.
You will need to replace YOUR_API_KEY_HERE
with your actual API key.
Conclusion
In this blog post, we took a look at how to use https://t.ly/docs API to shorten and expand URLs programmatically using JavaScript. The examples provided should give you a good starting point to integrate this API into your own projects.