If you're looking to automate your link shortening process or integrate short link creation into your web or mobile application, the URLShortly API is a powerful tool. In this article, we'll walk you through how to use the API effectively to shorten URLs in real time.
Step 1: Generate Your API Key
To get started, you need access to the URLShortly API. Here’s how you can generate your API key:
- Log in to your URLShortly admin dashboard.
- Go to the API or Developer Tools section.
- Click on Generate API Key.
- Copy and securely store your API key. (You’ll need it for all requests.)
Step 2: API Endpoint Overview
The basic endpoint for shortening a URL usually looks like this:
POST https://urlshortly.com/api/shorten
Required Headers:
{
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
Request Body:
{
"long_url": "https://example.com/page",
"custom_slug": "optional-custom-slug"
}
Step 3: Sample API Request in cURL
curl -X POST https://urlshortly.com/api/shorten \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"long_url": "https://example.com/page"
}'
Step 4: Understanding the Response
If your request is successful, you’ll receive a JSON response like:
{
"status": "success",
"short_url": "https://urlshortly.com/xyz123",
"slug": "xyz123"
}
You can now use the short_url
in your app, emails, social media, or anywhere else.
Step 5: Handling Errors
Here are common error responses:
- 401 Unauthorized – Invalid or missing API key.
- 400 Bad Request – Missing required fields like
long_url
. - 409 Conflict – Custom slug already in use.
Always check the status code and message in the response for debugging.
Step 6: Best Practices
- Don’t expose your API key in frontend code.
- Log errors for debugging purposes.
- Use HTTPS to secure API communications.
- Rate-limit requests to avoid abuse or API throttling.
Conclusion
Using the URLShortly API can streamline your link management process, especially if you’re working with bulk links or automated workflows. With just a few lines of code, you can start generating short URLs and tracking them effectively.
Ready to build something cool? Stay tuned for the next guide on “How to Generate Bulk Short Links Using URLShortly.”