Getting started overview
Getting started with the CleanURI API involves a series of steps designed to enable programmatic URL shortening for developers. The API is suitable for integrating into applications where custom short links, often for tracking purposes, are required. This guide covers account creation, API key retrieval, and the execution of a basic URL shortening request. The process is streamlined, focusing on direct API interaction rather than extensive dashboard configurations. CleanURI provides a free tier offering 100 requests per month, allowing developers to test functionality before committing to a paid plan.
The primary interaction with CleanURI is through its URL Shortener API, which accepts POST requests with a target URL and returns a shortened version. Authentication is managed via an API key, which must be included in the request header. This approach is common among web APIs, ensuring that requests are authorized and attributed to a specific account, as detailed in IETF RFC 6750 regarding Bearer Token usage.
To summarize the initial setup:
| Step | What to Do | Where |
|---|---|---|
| 1. Sign Up | Create a CleanURI account. | CleanURI homepage |
| 2. Get API Key | Locate your unique API key in your dashboard. | CleanURI Dashboard > API Settings |
| 3. Make Request | Send a POST request with your URL and API key. | Your preferred HTTP client/programming language |
Create an account and get keys
The first step to using CleanURI is to create an account. Navigate to the CleanURI website and follow the sign-up process. This typically involves providing an email address and creating a password. Once registered, you will gain access to your personal dashboard.
Within your CleanURI dashboard, you will find your unique API key. This key is essential for authenticating all your API requests. The API key acts as a credential that identifies you as an authorized user. It is crucial to keep this key confidential to prevent unauthorized use of your account and API quota. Best practices for API key management include storing them securely and avoiding hardcoding them directly into public repositories. For instance, environments variables are a common method for handling sensitive data like API keys, as described in AWS Lambda environment variable documentation.
To locate your API key:
- Log in to your newly created CleanURI account.
- Navigate to the section typically labeled "API Settings," "Developer," or "Dashboard."
- Your API key will be displayed there. Copy it for use in your applications.
CleanURI's authentication method relies on passing this API key in the request header. This is a standard security practice for web APIs, ensuring that only authenticated requests are processed. The specific header name for the API key will be detailed in the official CleanURI API documentation, but it commonly follows patterns like X-API-Key or Authorization: Bearer YOUR_API_KEY. Always consult the latest documentation for the exact header name and format.
Your first request
After obtaining your API key, you can make your first request to the CleanURI API. The API endpoint for shortening URLs is straightforward. You will send a POST request to this endpoint with the long URL you wish to shorten, along with your API key for authentication.
The basic structure of a POST request to the CleanURI API involves two main components: the API key in the header and the target URL in the request body. The API expects the URL to be shortened as a form-encoded parameter or a JSON body, depending on the specific endpoint design. For simplicity, many APIs accept a url parameter in the request body.
Example using cURL
cURL is a command-line tool and library for transferring data with URLs, commonly used for testing web APIs. Replace YOUR_API_KEY with your actual key and https://example.com/long-url-to-shorten with your desired URL.
curl -X POST \
https://cleanuri.com/api/v1/shorten \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'X-API-Key: YOUR_API_KEY' \
--data-urlencode 'url=https://example.com/long-url-to-shorten'
In this example, -X POST specifies the HTTP method, -H sets the request headers (Content-Type and X-API-Key), and --data-urlencode sends the URL parameter in the request body.
Example using Python
Python's requests library simplifies HTTP requests. Install it using pip install requests if you haven't already.
import requests
api_key = "YOUR_API_KEY"
long_url = "https://example.com/very-long-url-that-needs-shortening"
url = "https://cleanuri.com/api/v1/shorten"
headers = {
"X-API-Key": api_key,
"Content-Type": "application/x-www-form-urlencoded"
}
data = {
"url": long_url
}
response = requests.post(url, headers=headers, data=data)
if response.status_code == 200:
short_url = response.json().get("result_url")
print(f"Shortened URL: {short_url}")
else:
print(f"Error: {response.status_code} - {response.text}")
This Python script sends a POST request with the API key in the header and the long URL in the form-encoded body. It then parses the JSON response to extract the shortened URL. Successful responses typically return a JSON object containing the result_url field.
Expected Response
A successful request will return a JSON object similar to this:
{
"result_url": "https://cleanuri.com/xyz123"
}
An unsuccessful request will return an error status code (e.g., 400, 401, 429) and a JSON object detailing the error, such as:
{
"error": "Invalid API key"
}
Always check the HTTP status code and the response body for error messages to diagnose issues effectively, as recommended by Mozilla's HTTP status code reference.
Common next steps
Once you have successfully shortened your first URL, several common next steps can enhance your use of the CleanURI API:
- Integrate into Applications: Embed the URL shortening functionality directly into your web applications, mobile apps, or backend services. This could involve automating the process of generating short links for user-generated content, marketing campaigns, or internal tracking systems. The CleanURI API is well-suited for programmatic integration due to its straightforward request and response model.
- Implement Error Handling: Develop robust error handling mechanisms in your code. This includes catching various HTTP status codes (e.g., 400 for bad request, 401 for unauthorized, 429 for rate limiting) and parsing error messages from the API response. Proper error handling ensures your application remains stable and provides meaningful feedback to users or logs for debugging.
- Monitor API Usage: Regularly check your API usage within your CleanURI dashboard. This helps you stay within your free tier limits or monitor your consumption on a paid plan. Understanding your usage patterns can also inform decisions about scaling your plan if your application's needs grow.
- Explore Advanced Features (if available): Review the CleanURI documentation for any advanced features. While the core offering is a simple URL shortener, some APIs offer custom domains, analytics, or link management features. Although the provided payload does not explicitly state these, it is good practice to confirm the full capabilities.
- Review Pricing and Upgrade: If your usage exceeds the free tier's 100 requests per month, consider upgrading to a paid plan. Paid plans start at $19/month for 10,000 requests, providing significantly more capacity for growing applications.
- Implement Rate Limiting Strategies: If your application makes a high volume of requests, be aware of CleanURI's rate limits. Implement client-side rate limiting or backoff strategies to prevent exceeding these limits and receiving 429 Too Many Requests errors. This is a standard practice for interacting with external APIs, as detailed in Cloudflare's API rate limits documentation.
Troubleshooting the first call
Encountering issues during your first API call is common. Here are some troubleshooting steps and common problems you might face with CleanURI:
- Invalid API Key (401 Unauthorized):
- Issue: You receive a 401 Unauthorized error or a message indicating an invalid API key.
- Solution: Double-check that you have copied the correct API key from your CleanURI dashboard. Ensure there are no leading or trailing spaces. Verify that the API key is being sent in the correct HTTP header, typically
X-API-Key, as specified in the CleanURI API reference.
- Missing or Incorrect URL Parameter (400 Bad Request):
- Issue: The API returns a 400 Bad Request error, possibly stating that the 'url' parameter is missing or invalid.
- Solution: Ensure that your request body correctly includes the
urlparameter and that its value is a valid, properly encoded URL. Forapplication/x-www-form-urlencoded, ensure the URL is URL-encoded. Confirm that theContent-Typeheader matches the format of your request body (e.g.,application/x-www-form-urlencodedorapplication/jsonif supported).
- Rate Limit Exceeded (429 Too Many Requests):
- Issue: You receive a 429 Too Many Requests error.
- Solution: This indicates you've exceeded your hourly or daily request limit (e.g., 100 requests/month on the free tier). Wait for the rate limit to reset, or consider upgrading your plan if sustained higher usage is necessary. Implement a delay or exponential backoff in your code for retries.
- Network Connectivity Issues:
- Issue: Your request times out or fails to connect.
- Solution: Verify your internet connection. If running from a server, check firewall rules or proxy settings that might block outbound HTTP requests to
https://cleanuri.com.
- Incorrect Endpoint:
- Issue: Your request fails with a 404 Not Found error.
- Solution: Confirm that you are sending the request to the exact endpoint specified in the CleanURI documentation, which is typically
https://cleanuri.com/api/v1/shorten. Typographical errors in the URL can lead to this error.
- CORS Issues (Cross-Origin Resource Sharing):
- Issue: If making requests directly from a web browser (client-side JavaScript), you might encounter CORS errors.
- Solution: CleanURI's API is primarily designed for server-side integration. If you need client-side shortening, you might need to route requests through your own backend server to avoid CORS restrictions, or check if CleanURI explicitly supports client-side requests with appropriate CORS headers.
When troubleshooting, use tools like your browser's developer console (for client-side issues), cURL, or Postman to isolate the problem. Examine the full HTTP request and response, including headers and body, to pinpoint discrepancies. Referencing the official CleanURI API documentation for specific error codes and messages is always the most direct path to resolution.