Creating your first short link on Short.io

This page will help you to create your first link with our API

This guide will show you how to create short links with our API. First of all, you need to check if you can solve the problem with our Zapier integration. It allows you to set up common integrations without writing a single line of code.

To use our API you need to get your API key here: https://app.short.io/settings/integrations/api-key

  • Click "Create API key".
  • Add a Secret key.

Then you need to install prerequisites for HTTP requests if they are needed in your programming language

pip install requests
npm install --save got
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

And now you are ready to write actual source code. The following snippet will create a short URL with an auto-generated path for given long URL

📘

Please replace example.com with your actual domain name

<?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', {
      'domain': '<<domain_name>>',
      'originalURL': 'http://yourlongdomain.com/yourlonglink',
}, headers = {
      'authorization': '<<apiKey>>'
}, json=True)

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))
}
const got = require("got");

const options = {
  method: 'POST',
  url: 'https://api.short.io/links',
  headers: {
    authorization: '<<apiKey>>',
  },
  json: {
    originalURL: 'http://yourlongdomain.com/yourlonglink',
    domain: '<<domain_name>>'
  },
  responseType: 'json'
};

got(options).then(response => {
  console.log(response.body);
});

Once you will run this 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"
}

Most important keys here are "shortURL" (the URL of the newly-created short link) and "idString" (you will need to update or delete created URL)

That's all, you have created a link with our API. Our next articles will be about more advanced short.io features