SDKs overview

GoTiny offers a straightforward API for URL shortening, primarily centered around a single POST endpoint for creating short links. To facilitate integration, GoTiny provides official SDKs for several popular programming languages, alongside a growing ecosystem of community-contributed libraries. These SDKs and libraries encapsulate the underlying HTTP requests and response handling, allowing developers to interact with the GoTiny API using native language constructs rather than direct HTTP client calls. This approach aims to streamline development, reduce potential errors, and improve code readability for tasks such as generating short URLs, creating custom links, and retrieving basic link analytics.

The core functionality exposed by most GoTiny SDKs includes methods for submitting a long URL and an optional custom alias to the /api endpoint. Authentication is handled by including an API key directly within the request body, a method that differs from common header-based or OAuth 2.0 authentication patterns found in some other APIs. Developers should consult the GoTiny developer documentation for the most current information on API usage and best practices. The API's simplicity makes it accessible for direct HTTP client usage, but SDKs offer a more idiomatic experience for specific language environments.

Official SDKs by language

GoTiny maintains official SDKs for key programming languages, ensuring direct support and compatibility with the latest API features. These libraries are designed to provide a consistent and reliable interface for developers. Each official SDK typically includes methods for the primary GoTiny operations, such as creating a short URL and specifying a custom alias. The following table details the officially supported SDKs, their package names, installation commands, and general maturity status.

Language Package Name Install Command Maturity
JavaScript @gotiny/client npm install @gotiny/client or yarn add @gotiny/client Stable
Python gotiny-py pip install gotiny-py Stable
PHP gotiny/php-client composer require gotiny/php-client Stable
Ruby gotiny-rb gem install gotiny-rb Stable

These official SDKs are the recommended approach for integrating GoTiny into applications, as they are directly maintained by the GoTiny team and are typically kept up-to-date with any API changes or new features. For detailed usage instructions and method references, developers should refer to the specific SDK's documentation, often linked from the main GoTiny developer portal.

Installation

Installation of GoTiny SDKs follows the standard package management practices for each respective language. The process generally involves using a command-line tool to add the library to your project's dependencies. Below are specific installation instructions for the officially supported languages.

JavaScript (Node.js/Browser)

For JavaScript environments, including Node.js projects and front-end browser applications, the @gotiny/client package is installed via npm or Yarn:

# Using npm
npm install @gotiny/client

# Using Yarn
yarn add @gotiny/client

After installation, the client can be imported into your JavaScript files:

import { GoTinyClient } from '@gotiny/client';
// or for CommonJS
// const { GoTinyClient } = require('@gotiny/client');

Python

Python developers can install the gotiny-py library using pip, the Python package installer:

pip install gotiny-py

Once installed, the library can be imported into Python scripts:

from gotiny import GoTiny

PHP

PHP projects typically use Composer for dependency management. The gotiny/php-client package is installed as follows:

composer require gotiny/php-client

After installation, Composer's autoloader will make the classes available:

<?php
require 'vendor/autoload.php';

use GoTiny\Client;

Ruby

Ruby developers use RubyGems to install the gotiny-rb gem:

gem install gotiny-rb

Then, require the gem in your Ruby application:

require 'gotiny'

For any specific environment configurations or troubleshooting, referring to the GoTiny developer documentation and the respective SDK's repository is recommended.

Quickstart example

The following quickstart examples demonstrate how to create a short URL using the GoTiny API via its official SDKs. These snippets assume you have already installed the respective SDK and have a valid GoTiny API key. Replace YOUR_API_KEY with your actual key and https://example.com/very/long/url with the URL you wish to shorten. The examples focus on the most common use case: generating a basic short link.

JavaScript Example

This example uses the @gotiny/client package in a Node.js environment to shorten a URL.

import { GoTinyClient } from '@gotiny/client';

const apiKey = 'YOUR_API_KEY'; // Replace with your actual API key
const client = new GoTinyClient(apiKey);

async function shortenUrl() {
  try {
    const longUrl = 'https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency';
    const response = await client.shorten({ url: longUrl });
    console.log('Short URL:', response.data.tiny_url);
  } catch (error) {
    console.error('Error shortening URL:', error.message);
  }
}

shortenUrl();

This JavaScript example initializes the client with the API key and then calls the shorten method, passing an object with the url property. The response contains the tiny_url field with the generated short link.

Python Example

This Python example demonstrates URL shortening using the gotiny-py library.

from gotiny import GoTiny

api_key = 'YOUR_API_KEY' # Replace with your actual API key
gotiny = GoTiny(api_key)

def shorten_url():
    try:
        long_url = 'https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-lifecycle.html'
        response = gotiny.shorten(url=long_url)
        print(f"Short URL: {response['tiny_url']}")
    except Exception as e:
        print(f"Error shortening URL: {e}")

shorten_url()

The Python client is instantiated with the API key. The shorten method is then called with the url parameter. The resulting dictionary includes the tiny_url key.

PHP Example

Here's a PHP example showing how to use the gotiny/php-client to create a short link.

<?php
require 'vendor/autoload.php';

use GoTiny\Client;

$apiKey = 'YOUR_API_KEY'; // Replace with your actual API key
$client = new Client($apiKey);

try {
    $longUrl = 'https://cloud.google.com/docs/authentication/api-keys';
    $response = $client->shorten(['url' => $longUrl]);
    echo 'Short URL: ' . $response['tiny_url'] . "\n";
} catch (\Exception $e) {
    echo 'Error shortening URL: ' . $e->getMessage() . "\n";
}
?>

The PHP client is initialized with the API key, and the shorten method accepts an associative array with the url key. The tiny_url is then extracted from the response array.

Ruby Example

This Ruby example uses the gotiny-rb gem to perform URL shortening.

require 'gotiny'

api_key = 'YOUR_API_KEY' # Replace with your actual API key
client = GoTiny::Client.new(api_key)

begin
  long_url = 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200'
  response = client.shorten(url: long_url)
  puts "Short URL: #{response['tiny_url']}"
rescue StandardError => e
  puts "Error shortening URL: #{e.message}"
end

The Ruby client is created with the API key, and the shorten method takes a hash with the url key. The tiny_url is retrieved from the response hash.

These examples demonstrate the fundamental process. For advanced features like custom aliases or specific error handling, consult the GoTiny API documentation and the respective SDK's specific usage guide.

Community libraries

Beyond the official offerings, the GoTiny developer community contributes libraries and wrappers for various programming languages. These community-driven projects extend GoTiny's reach to environments not covered by official SDKs or offer alternative implementations. While not officially supported by GoTiny, they can provide valuable resources for developers working in specific ecosystems. It is important to review the source code, documentation, and maintenance status of community libraries before integrating them into production systems, as their reliability and currency may vary.

Some notable community-contributed libraries include:

  • Go: A Go (Golang) client library might be available on platforms like GitHub, often leveraging Go's standard net/http package for requests. Developers interested in Go integration should search for gotiny go client on code hosting platforms.
  • C#: For .NET developers, community efforts might provide a C# wrapper, typically utilizing HttpClient for API interactions. Searching for gotiny c# client on NuGet or GitHub can yield relevant projects.

When using community libraries, developers are responsible for verifying their compatibility with the current GoTiny API version and ensuring they adhere to security best practices, particularly regarding API key handling. The GoTiny API's simplicity, primarily involving HTTP POST requests with JSON payloads, also makes it feasible to implement custom client logic in any language using standard HTTP client libraries, such as AWS SDK for PHP's Guzzle HTTP client or .NET's HttpClient, if a suitable community library is not found or preferred.