Authentication overview
CleanURI offers a URL shortening API designed for programmatic integration into applications. Access to the API is secured through an API key authentication mechanism. This method requires developers to include a unique, secret key with each API request to verify their identity and authorize the operation. The API key acts as a credential that links the request to a specific CleanURI account and its associated usage limits and permissions.
The API key ensures that only authenticated clients can interact with the CleanURI service, protecting against unauthorized usage and maintaining the integrity of the platform. This approach is common in many web APIs due to its simplicity and effectiveness for managing access to resources. For detailed information on API key usage, refer to the official CleanURI API documentation.
Supported authentication methods
CleanURI exclusively supports API key authentication. This method involves transmitting a unique key within the HTTP headers of each API request. The API key serves as a token that identifies the calling application or user to the CleanURI service.
| Method | When to Use | Security Level |
|---|---|---|
| API Key (Header) | When making direct API calls from backend services or client-side applications (with care). Suitable for server-to-server communication where the key can be securely stored. | Moderate. Requires careful handling and storage to prevent unauthorized access. Best practice is to avoid exposing keys in client-side code directly. |
API keys are generally suitable for identifying project or application access rather than individual user authentication. For scenarios requiring user-specific authentication and authorization, more complex protocols like OAuth 2.0 are typically employed, as described by OAuth.net's resource. However, for a service like CleanURI, which primarily focuses on programmatic URL shortening, API keys provide a sufficiently robust and straightforward authentication mechanism.
Getting your credentials
To obtain your CleanURI API key, you must first create an account on the CleanURI website. Once registered and logged in, your API key will be available in your personal dashboard. The process generally involves these steps:
- Sign Up/Log In: Navigate to the CleanURI homepage and either create a new account or log in to an existing one.
- Access Dashboard: After successful login, you will be redirected to your user dashboard.
- Locate API Key: Within the dashboard, there will be a dedicated section, often labeled "API Key" or "Developer Settings," where your unique API key is displayed.
- Copy Key: Copy the displayed API key. This key is sensitive and should be treated as a password.
It is important to keep your API key confidential. If you suspect your API key has been compromised, most services, including CleanURI, offer a mechanism within the dashboard to regenerate or revoke the existing key and issue a new one. Regularly reviewing and rotating API keys is a recommended security practice.
Authenticated request example
When making requests to the CleanURI API, your API key must be included in the HTTP header named X-API-Key. The API endpoint for shortening URLs is typically https://cleanuri.com/api/v1/shorten. Below are examples demonstrating how to include your API key in requests using various programming languages and cURL.
cURL Example
curl -X POST \
https://cleanuri.com/api/v1/shorten \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'X-API-Key: YOUR_API_KEY' \
-d 'url=https://example.com/long-url-to-shorten'
Python Example
import requests
api_key = "YOUR_API_KEY"
long_url = "https://example.com/long-url-to-shorten"
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"X-API-Key": api_key
}
data = {
"url": long_url
}
response = requests.post("https://cleanuri.com/api/v1/shorten", headers=headers, data=data)
if response.status_code == 200:
print("Shortened URL:", response.json().get("result_url"))
else:
print("Error:", response.status_code, response.text)
Node.js Example
const axios = require('axios');
const apiKey = 'YOUR_API_KEY';
const longUrl = 'https://example.com/long-url-to-shorten';
axios.post('https://cleanuri.com/api/v1/shorten',
`url=${encodeURIComponent(longUrl)}`,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-API-Key': apiKey
}
}
)
.then(response => {
console.log('Shortened URL:', response.data.result_url);
})
.catch(error => {
console.error('Error:', error.response ? error.response.status : error.message);
});
Replace YOUR_API_KEY with your actual API key obtained from your CleanURI dashboard. These examples demonstrate the fundamental structure for including the API key in API requests.
Security best practices
Securing your API keys is crucial to prevent unauthorized access to your CleanURI account, potential misuse of your API limits, and disruption of your services. Adhering to these best practices can mitigate common security risks associated with API key usage:
- Keep API Keys Confidential: Treat your API key like a password. Do not hardcode it directly into client-side code (e.g., JavaScript in a public webpage) where it can be easily extracted. For server-side applications, store keys in environment variables, secret management services, or encrypted configuration files rather than directly in your source code repository. The Google Cloud API keys guide provides general recommendations for API key security.
- Use Environment Variables: When deploying applications, retrieve API keys from environment variables. This practice keeps sensitive information out of your codebase and allows for easy rotation without code changes.
- Implement Server-Side Calls: Whenever possible, make API calls from your backend server rather than directly from client-side applications. This approach centralizes key management and prevents exposure in the user's browser.
- Restrict API Key Permissions: While CleanURI API keys might have broad access for URL shortening, it's a general best practice with other APIs to limit the permissions of each API key to only what is necessary for its intended function.
- Monitor API Key Usage: Regularly review your CleanURI account dashboard for any unusual activity or spikes in API usage that could indicate a compromised key.
- Rotate API Keys Periodically: Change your API keys at regular intervals (e.g., every 90 days). This reduces the window of opportunity for an attacker if a key is compromised. Most dashboards, including CleanURI's, provide a function to regenerate keys.
- Use HTTPS: Always ensure that all communication with the CleanURI API occurs over HTTPS. This encrypts the data in transit, including your API key, protecting it from eavesdropping. All modern API interactions, including those with Stripe's API, mandate HTTPS for secure communication.
- IP Whitelisting (if available): If CleanURI offers IP whitelisting, configure your API key to only accept requests originating from a specific set of trusted IP addresses. This adds an extra layer of security, making it harder for unauthorized parties to use your key even if they obtain it.