SDKs overview
CleanURI provides a URL Shortener API designed for programmatic integration into various applications. To facilitate this integration, developers can utilize a range of Software Development Kits (SDKs) and libraries. These tools abstract the underlying HTTP requests and responses, allowing developers to interact with the API using native language constructs rather than manually crafting HTTP calls. The available SDKs cover several popular programming languages, offering convenience for tasks such as shortening URLs, retrieving shortened link details, and managing API key authentication.
The core functionality of the CleanURI API, accessible through these SDKs, involves submitting a long URL and receiving a shortened version. This process typically requires an API key for authentication, which is passed in the request header as described in the CleanURI documentation. SDKs simplify this by providing methods that handle the header construction automatically. While CleanURI offers official examples in common languages, the community also contributes libraries that extend support or provide alternative implementations, conforming to the HTTP/1.1 specification for requests.
For developers, selecting an appropriate SDK often depends on the project's primary programming language and existing tech stack. The goal is to reduce boilerplate code and potential errors associated with direct API interaction, allowing focus on application logic. The SDKs typically manage serialization and deserialization of data, converting language-specific objects into JSON for API requests and vice versa for responses.
Official SDKs by language
CleanURI offers official code examples and guidance for direct API interaction across a range of popular programming languages. While not always provided as installable packages in the traditional SDK sense (like a pre-compiled library), these examples serve as official blueprints for integrating the API. They illustrate the necessary HTTP request structure, authentication methods, and response handling for each language. The following table summarizes the primary languages for which CleanURI provides direct integration examples:
| Language | Package / Method | Installation / Usage | Maturity |
|---|---|---|---|
| cURL | Command-line utility | Pre-installed on most Unix-like systems, direct command execution | Stable |
| Python | requests library (third-party) |
pip install requests, then direct API calls |
Stable |
| PHP | curl extension / Guzzle (third-party) |
Native curl or composer require guzzlehttp/guzzle |
Stable |
| Node.js | axios or node-fetch (third-party) |
npm install axios or npm install node-fetch |
Stable |
| Ruby | Net::HTTP (standard library) |
Built-in, direct API calls | Stable |
| Go | net/http (standard library) |
Built-in, direct API calls | Stable |
| Java | java.net.HttpURLConnection or Apache HttpClient (third-party) |
Built-in or add Maven/Gradle dependency for Apache HttpClient | Stable |
| C# | HttpClient (built-in) |
Built-in, direct API calls | Stable |
These official examples showcase the fundamental API interaction patterns, including how to construct the POST request body with the url parameter and how to include the API key in the X-CleanUri-Api-Key header. They also demonstrate parsing the JSON response to extract the shortened URL. For a complete overview of the API endpoints and expected parameters, refer to the CleanURI API documentation.
Installation
Since CleanURI primarily provides direct API examples rather than installable SDK packages, installation typically refers to setting up the necessary HTTP client libraries in your chosen programming language. These client libraries are standard tools for making web requests and are often already part of the language's standard library or widely used third-party packages.
Here are common installation steps for the recommended HTTP clients for several languages:
- Python: The
requestslibrary is the de facto standard for HTTP requests in Python. Install it using pip:
This command fetches thepip install requestsrequestspackage from the Python Package Index (PyPI) and installs it into your environment. - PHP: While PHP's native
curlextension can be used, many developers prefer Guzzle for its object-oriented interface and robust features. Install Guzzle via Composer:
Ensure Composer is installed first. Guzzle streamlines HTTP requests by handling many complexities.composer require guzzlehttp/guzzle - Node.js:
axiosornode-fetchare popular choices for making HTTP requests in Node.js environments. Install one using npm:
ornpm install axios
These commands add the respective package to your project'snpm install node-fetchnode_modulesdirectory and updatepackage.json. - Ruby: Ruby's standard library includes
Net::HTTP, which is sufficient for basic API interactions. No additional installation is typically required. For more advanced features or a more DSL-like experience, gems likehttpartycan be installed via Bundler:gem install httparty. - Go: The
net/httppackage is part of Go's standard library, offering comprehensive functionality for HTTP clients and servers. No external installation is needed for basic CleanURI integration. - Java: Java's built-in
HttpURLConnectioncan be used, but for more modern and flexible HTTP client capabilities, Apache HttpClient is a common choice. If using Maven, add the following to yourpom.xml:
Ensure you check for the latest stable version of Apache HttpClient.<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> - C#: The
HttpClientclass is built into .NET and is the recommended way to make HTTP requests. No separate installation is required beyond ensuring your project targets a compatible .NET version.
After installing the chosen HTTP client, the next step involves writing code to construct the API request, including your API key and the URL to be shortened, and then processing the JSON response.
Quickstart example
This Python example demonstrates how to use the requests library to shorten a URL with the CleanURI API. Replace YOUR_API_KEY with your actual API key, which can be found in your CleanURI account dashboard as per the CleanURI authentication guide.
import requests
import json
# Replace with your actual CleanURI API Key
API_KEY = "YOUR_API_KEY"
# The URL to be shortened
long_url = "https://example.com/very/long/url/that/needs/to/be/shortened/for/sharing"
# CleanURI API endpoint
api_endpoint = "https://cleanuri.com/api/v1/shorten"
# Request headers
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"X-CleanUri-Api-Key": API_KEY # Include your API key here
}
# Request data (the URL to shorten)
data = {
"url": long_url
}
try:
# Make the POST request to the CleanURI API
response = requests.post(api_endpoint, headers=headers, data=data)
response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)
# Parse the JSON response
result = response.json()
if response.status_code == 200 and result.get("result_url"):
shortened_url = result["result_url"]
print(f"Original URL: {long_url}")
print(f"Shortened URL: {shortened_url}")
elif result.get("error"):
print(f"Error shortening URL: {result['error']}")
else:
print(f"Unexpected API response: {result}")
except requests.exceptions.HTTPError as errh:
print(f"HTTP Error: {errh}")
except requests.exceptions.ConnectionError as errc:
print(f"Error Connecting: {errc}")
except requests.exceptions.Timeout as errt:
print(f"Timeout Error: {errt}")
except requests.exceptions.RequestException as err:
print(f"An unexpected error occurred: {err}")
This quickstart demonstrates the core steps: importing the necessary library, defining your API key and the target URL, constructing the headers and data payload, making the POST request, and handling the JSON response. The requests.post() function sends the data, and response.json() parses the server's reply. Error handling is included to manage potential issues like network problems or API-specific errors, which is a critical part of integrating any external service.
Community libraries
While CleanURI provides official documentation and examples for direct API integration, the developer community sometimes creates and maintains unofficial libraries or wrappers. These community-contributed tools can offer alternative approaches, additional features, or more idiomatic interfaces for specific programming languages or frameworks. They are typically open-source projects hosted on platforms like GitHub.
Key characteristics of community libraries often include:
- Language-specific abstractions: They may offer classes and methods that align more closely with the conventions of a particular language, making the API feel more integrated into the developer's environment.
- Helper functions: Often include utilities for common tasks beyond basic API calls, such as automatic retry mechanisms, rate limiting, or more sophisticated error handling.
- Framework integration: Some community libraries might be designed to work seamlessly within specific web frameworks (e.g., a Django package for Python or a Rails gem for Ruby).
- Active development: The quality and maintenance of community libraries can vary. Developers should assess the project's activity, issue tracker, and contribution guidelines before integrating them into production systems.
To discover community libraries, developers typically search package managers (like PyPI for Python, npm for Node.js, RubyGems for Ruby) or code hosting platforms (like GitHub) using keywords such as "cleanuri" or "url shortener." Always review the source code and licensing of any third-party library before use. While these libraries can accelerate development, they may not always be up-to-date with the latest API changes or carry the same level of support as official resources. Consulting the Mozilla Developer Network HTTP status code documentation can also aid in understanding API responses from any library.