API for custom short URLs - Code snippet

Copy-and-paste in your application/script to create a custom short URL:

let linkRequest = {
  destination: "https://www.youtube.com/channel/UCHK4HD0ltu1-I212icLPt3g",
  domain: { fullName: "rebrand.ly" }
  //, slashtag: "A_NEW_SLASHTAG"
  //, title: "Rebrandly YouTube channel"
}

let requestHeaders = {
  "Content-Type": "application/json",
  "apikey": "YOUR_API_KEY",
  "workspace": "YOUR_WORKSPACE_ID"
}

$.ajax({
  url: "https://api.rebrandly.com/v1/links",
  type: "post",
  data: JSON.stringify(linkRequest),
  headers: requestHeaders,
  dataType: "json",
  success: (link) => {
    console.log(`Long URL was ${link.destination}, short URL is ${link.shortUrl}`);
  }
});
const axios = require('axios');

const headers = {
  "Content-Type": "application/json",
  "apikey": "YOUR_API_KEY",
  "workspace": "YOUR_WORKSPACE_ID"
}

async shorten = url => {
	let endpoint = "https://api.rebrandly.com/v1/links";
  let linkRequest = {
    destination: url,
    domain: { fullName: "rebrand.ly" }
    //, slashtag: "A_NEW_SLASHTAG"
    //, title: "Rebrandly YouTube channel"
  }
  const apiCall = {
      method: 'post',
      url: endpoint,
      data: linkRequest,
      headers: headers
  }
  let apiResponse = await axios(apiCall);
  let link = apiResponse.data;
  return link.shortUrl;
}

let shortUrl = await shorten("https://www.youtube.com/channel/UCHK4HD0ltu1-I212icLPt3g");
console.log(shortUrl);
<?php
  $client = new http\Client;
  $request = new http\Client\Request;
  $request->setRequestUrl('https://api.rebrandly.com/v1/links');
  $request->setRequestMethod('POST');
  $body = new http\Message\Body;
  $body->append('{
      "domain": {
          "fullName": "rebrand.ly"
      },
      "destination": "https://www.youtube.com/channel/UCHK4HD0ltu1-I212icLPt3g"
  }');
  $request->setBody($body);
  $request->setOptions(array());
  $request->setHeaders(array(
    'Content-Type' => 'application/json',
    'apikey' => 'YOUR_API_KEY',
    'workspace' => 'YOUR_WORKSPACE_ID'
  ));
  $client->enqueue($request)->send();
  $response = $client->getResponse();
  echo $response->getBody();
?>
import requests
import json

linkRequest = {
  "destination": "https://www.youtube.com/channel/UCHK4HD0ltu1-I212icLPt3g"
  , "domain": { "fullName": "rebrand.ly" }
# , "slashtag": "A_NEW_SLASHTAG"
# , "title": "Rebrandly YouTube channel"
}

requestHeaders = {
  "Content-type": "application/json",
  "apikey": "YOUR_API_KEY",
  "workspace": "YOUR_WORKSPACE_ID"
}

r = requests.post("https://api.rebrandly.com/v1/links", 
	data = json.dumps(linkRequest),
	headers=requestHeaders)

if (r.status_code == requests.codes.ok):
	link = r.json()
	print("Long URL was %s, short URL is %s" % (link["destination"], link["shortUrl"]))
POST https://api.rebrandly.com/v1/links
Content-Type: application/json
apikey: YOUR_API_KEY
workspace: YOUR_WORKSPACE_ID

{"destination": "https://www.youtube.com/channel/UCHK4HD0ltu1-I212icLPt3g", "domain": { "fullName": "rebrand.ly"}}
curl https://api.rebrandly.com/v1/links \
  -H 'Content-Type: application/json' \
  -H 'apikey: YOUR_API_KEY' \
  -H 'workspace: YOUR_WORKSPACE_ID' \
  -d '{"destination": "https://www.youtube.com/channel/UCHK4HD0ltu1-I212icLPt3g","domain":{"fullName":"rebrand.ly"}}'
//Install-Package Newtonsoft.Json
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
	
var payload = new
{
    destination = "https://www.youtube.com/channel/UCHK4HD0ltu1-I212icLPt3g",
    domain = new
    {
        fullName = "rebrand.ly"
    }
    //, slashtag = "A_NEW_SLASHTAG"
    //, title = "Rebrandly YouTube channel"
};

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.rebrandly.com") })
{
    httpClient.DefaultRequestHeaders.Add("apikey", "YOUR_API_KEY");
    httpClient.DefaultRequestHeaders.Add("workspace", "YOUR_WORKSPACE_ID");

    var body = new StringContent(
        JsonConvert.SerializeObject(payload), UnicodeEncoding.UTF8, "application/json");

    using (var response = await httpClient.PostAsync("/v1/links", body))
    {
        response.EnsureSuccessStatusCode();

        var link = JsonConvert.DeserializeObject<dynamic>(
            await response.Content.ReadAsStringAsync());

        Console.WriteLine($"Long URL was {payload.destination}, short URL is {link.shortUrl}");
    }
}
{% assign rebrandly_api_key = "YOUR_API_KEY" %}
{% assign rebrandly_workspace_id = "YOUR_WORKSPACE_ID" %}
{% assign branded_domain = "rebrand.ly" %}
{% assign search_example = "youtube" }

{% capture destination %}
https://www.google.com/search?q={{search_example | url_encode}}
{% endcapture %}

{% connected_content https://api.rebrandly.com/v1/links/new?apikey={{rebrandly_api_key}}&domain[fullName]={{branded_domain}}&workspace[id]={{rebrandly_workspace_id}}&destination={{destination | url_encode}} :save response %}

{% if response.__http_status_code__ == 200 %}
    {% assign short_url = response.shortUrl %}
{% else %}
    {% abort_message() %}
{% endif %}
set @rbAPIKey = "YOUR_API_KEY"
set @rbWorkspace = "YOUR_WORKSPACE_ID"
set @rbDomain = "YOUR_DOMAIN_NAME"

var @shortUrl, @rebrandlyUrl, @link
set @url = URLEncode("https://google.com")
set @rbAPIHost = "api.rebrandly.com"
set @rbAPIEndpoint = Concat("https://", @rbAPIHost, "/v1/links/new")
set @rbAPIParameters = Concat("apikey=", @rbAPIKey, "&workspace=", @rbWorkspace, "&domain[fullName]=", @rbDomain, "&destination=“, @url)
set @rbAPICall = Concat(@rbAPIEndpoint, "?", @rbAPIParameters)
set @rbResponseJSON = HTTPGet(rbAPICall)
<script runat="server">
    var rbResponse = eval("(" + Variable.GetValue("@rbResponseJSON") + ")");
    var shortUrl = "https://" + rbResponse.shortUrl
</script>
var payload = {
  destination: "https://www.youtube.com/channel/UCHK4HD0ltu1-I212icLPt3g",
  domain: { fullName: "rebrand.ly" }
  //, slashtag: "A_NEW_SLASHTAG"
  //, title: "Rebrandly YouTube channel"
}

var headers = {
  apikey: "YOUR_API_KEY",
  workspace: "YOUR_WORKSPACE_ID"
}

var params = {
  headers: headers,
  contentType: "application/json",
  method: 'post',
  payload: JSON.stringify(payload),
  muteHttpExceptions: true
}

var response = UrlFetchApp.fetch("https://api.rebrandly.com/v1/links", params);

if (response.getResponseCode() == 200){
  var link = JSON.parse(response.getContentText())
  console.log(`Long URL was ${payload.destination}, short URL is ${link.shortUrl}`);
}

Make sure you replace YOUR_API_KEY with your own API key and YOUR_WORKSPACE_ID with your workspace id:

  • refer to the Authentication overview section to understand how to create an API key (or, alternatively) how to equip your API calls with a Bearer token)
  • Rebrandly accounts come with the concept of Workspaces. If you only have one workspace, you can omit the workspace HTTP header so that your default/only workspace will be used. If you have multiple workspaces, please refer to the endpoint to list workspaces you are a member of, and use the property id of the workspace of choice.

What’s Next

First time with Rebrandly API?