Creating your first short link on Short.io
This guide will help you to create your first link with our API
Before you consider developing your own applications to interact with the Short.io API, we recommend exploring other optimization and automation options for your processes, including our Zapier integration. It enables seamless connectivity between different tools and services without writing a single line of code.
To create short links with Short.io API
To use our API, it is required to create a secret API key from the Integrations and API menu: https://app.short.io/settings/integrations/api-key
Then you may need to install prerequisites for HTTP requests (if necessary, depending on your programming language):
pip install requests
npm install --save @short.io/client-node
Install-Package RestSharp -Version 106.6.10
# for ubuntu and debian
apt install php-curl
# it is 2019, please install python instead
apt install python3
You can now use the following code snippet to generate a short URL with an automatically created path for the specified long URL.
Please replace example.com with your actual domain name and APIKEY with your secret API key.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.short.io/links",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode(array(
'originalURL' => 'http://yourlongdomain.com/yourlonglink',
'domain' => '<<domain_name>>'
)),
CURLOPT_HTTPHEADER => array(
"authorization: <<apiKey>>",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
import requests
res = requests.post('https://api.short.io/links', json={
'domain': '<<domain_name>>',
'originalURL': 'http://yourlongdomain.com/yourlonglink',
}, headers = {
'authorization': '<<apiKey>>',
'content-type': 'application/json'
}, )
res.raise_for_status()
data = res.json()
print(data)
using RestSharp;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var client = new RestClient("https://api.short.io/");
client.AddDefaultHeader("Authorization", "<<apiKey>>");
var req = new RestRequest("links", Method.POST, DataFormat.Json);
req.AddParameter("domain", "<<domain_name>>");
req.AddParameter("originalURL", "http://yourlongdomain.com/yourlonglink");
var res = client.Execute(req);
Console.WriteLine(res.Content);
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
client := &http.Client{}
jsonData := map[string]string{
"originalURL": "http://yourlongdomain.com/yourlonglink",
"domain": "<<domain_name>>"
}
jsonValue, _ := json.Marshal(jsonData)
req, err := http.NewRequest("POST", "https://api.short.io/links", bytes.NewBuffer(jsonValue))
if err != nil {
fmt.Printf("The request creation failed with error %s\n", err)
return;
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "<<apiKey>>")
response, err := client.Do(req)
if err != nil {
fmt.Printf("The HTTP request failed with error %s\n", err)
return;
}
data, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(data))
}
import { setApiKey, postLinks } from "@short.io/client-node";
setApiKey("<<apiKey>>")
const response = await postLinks({
body: {
originalURL: 'http://yourlongdomain.com/yourlonglink',
domain: '<<domain_name>>'
}
});
console.log(response.data)
Once you run the above code, you will see a response:
{
"idString": "lnk_49n_okhPU",
"path": "xpsmpw",
"title": null,
"icon": null,
"archived": false,
"originalURL": "http://yourlongdomain.com/yourlonglink",
"iphoneURL": null,
"androidURL": null,
"splitURL": null,
"expiresAt": null,
"expiredURL": null,
"redirectType": null,
"cloaking": null,
"source": null,
"AutodeletedAt" :null,
"createdAt": "2019-10-11T16:47:06.000Z",
"updatedAt": "2019-10-11T16:47:06.000Z",
"DomainId": 15957,
"OwnerId": 48815,
"secureShortURL": "https://<<domain_name>>/xpsmpw",
"shortURL": "https://<<domain_name>>/xpsmpw"
}
You have successfully established a connection with our API.
Most important keys in the response are shortURL (the URL of the newly-created short link) and idString (which you will need in case you would want to update or delete the created URL).
Updated about 12 hours ago