Easiest Way to Shorten URLs?

I'm not a coder or a script kid.

What's the easiest way to convert a URL from the clipboard to a shortened version using a free service like bitly or tinyurl?

Thanks!

Hey @anamorph,

Use a browser extension for the purpose.

Bitly for Google Chrome

-Chris

I use this with TinyURL :

Hope this helps!

5 Likes

Thanks! This is exactly what I needed!

1 Like

If my clipboard has 10 web addresses, how can I achieve this?

Hi, @carycrusiau. Do you know if there is something similar for Bitly or Droplr?

Hi @alexcr!

I know Droplr but, apparently, they don't have an API that allows you to use their services from a command line.

Bit.ly has an API. So it's possible. Although a little more complex to implement than with TinyURL.

First, you need a Bit.ly account. Log in to your account and go to:

https://app.bitly.com/settings/api/

Enter your Bit.ly password and generate a token. Once you have your token, repeat the steps for my TinyURL solution and replace the shell script by:

Accesstoken=your_bitly_token
api=https://api-ssl.bitly.com/v4/shorten

curl -s -H Authorization:\ $Accesstoken -H Content-Type: -d '{"long_url": "'"$KMVAR_URL"\"} $api

If everything works correctly, you should receive a response similar to this one from Bit.ly:

As you can see, the response is formatted in JSON. We'll save this value in a variable called JSON (for example). The part we're interested in is "link". So we're going to use the %JSONValue% token to retrieve the short url generated by Bit.ly and send it to the clipboard:

And… voilà!

Bit.ly Link.kmmacros (23.6 KB)

1 Like

Thanks so much, @carycrusiau. I really appreciate it. Droplr, which is the primary service I use, actually does have an API...

https://droplr.github.io/docs/

Based on what you see here, would the steps be the same as those you shared for Bitly?

@carycrusiau on your tinyurl shell script, how do I add a requst for an alias?

Thanks!

Hi @alexcr, I've read the Droplr API documentation and, frankly, I don't understand it at all. Sorry I can't help.

1 Like

No worries, @carycrusiau. Thanks for taking the time to look it over. :pray:t3:

Hi @anamorph,

On your Tinyurl shell script, how do I add a reqeust for an alias?

As with Bit.ly, implementation is a little more complex because, in order to create an alias, you need to have a TinyURL account and obtain an access token.

Log in to your TinyURL account and go to:

https://tinyurl.com/app/settings/api

Enter a Token name (Keyboard Maestro for example), click Allow Create TinyURL. Copy your token.

Add a variable somewhere for the alias and change the shell script by:

curl -X 'POST' \
  'https://api.tinyurl.com/create?api_token=your_tinyurl_tokken' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d "{
  \"url\": \"$KMVAR_URL\",
  \"domain\": \"tinyurl.com\",
  \"alias\": \"$KMVAR_Alias\"
}"

If everything works correctly, you should receive a response similar to this one from TinyURL:

As with Bit.ly, the response is formatted in JSON. We'll save this value in a variable called JSON (for example). The part we're interested in is "tiny_url". So we're going to use the %JSONValue% token to retrieve the short alias generated by TinyURL and send it to the clipboard:

And… voilà!

But… we have to deal with some errors:

  • If the alias already exists, the response is:

{"data":[],"code":5,"errors":["Alias is not available."]}

  • If the alias is too long, the response is:

{"data":[],"code":5,"errors":["The Alias must not be greater than 30 characters."]}

  • If the alias contains invalid characters, the response is:

{"data":[],"code":5,"errors":["The Alias format is invalid."]}

So, I added an If/Then condition to check some potential errors and display the error message in a notification.

Here is the macro:

TinyURL Alias.kmmacros (19.4 KB)

1 Like

Thanks very much!

1 Like

Hey, @carycrusiau! I reached out to Droplr support, share your TinyURL script with them, and asked them if they could offer guidance on a similar script for Droplr. They shared the following, using https://www.nytimes.com as an example of the link being shortened...

Just tested this script and it works fine:
curl -XPOST -H 'Authorization: Bearer <your_token_here>' -H "Content-type: text/plain" -d 'https://www.nytimes.com' 'https://api.droplr.com/links'
You will receive a response in which the shortlink is the new link you need

Would this be something I can just plug into your TinyURL macro, replacing the TinyURL script you used?

Hi @alexcr,

It's a good step forward. It sounds interesting but they don't explain how you can get a token. I've searched my Droplr account, I've searched their API, I've searched Google… I can't find anything. Their documentation says:

All API operations must be properly authenticated. In order for you to access the API you must have a public/private key combination to access Droplr's API server.

Okay! But... how?!?!?

1 Like

I had to laugh at this, because I'm also running into the same issue.

Their customer support told me I can get a token on the website at "dev tools -> application -> cookies -> jwtToken", but I'm not finding a "dev tools" section anywhere on the website dashboard or in my settings.

I'm waiting to hear back from them with further clarification, and will let you know what I find out!

I couldn't find anything on my side either :melting_face:

Nothing new from Droplr?

Nothing yet, unfortunately. I'm planning to ping them again tonight if I still haven't heard from them.

I finally heard back from Droplr, @carycrusiau! Here's how you get your token, assuming you're using Chrome...

  1. Sign into your account on the Droplr website.

  2. Right click on any blank space on the website, and select "Inspect" from the Chrome contextual menu. This brings up Chrome's developer tools.

  3. In Chrome's developer tools, select the "Application" tab. (You might need to click on the ">>" button to find it.)

  4. In this tab, on the menu on the left, in the "Storage" section, expand the "Cookies" menu item. You should see "https://d.pr" in there. Click on that.

  5. Towards the bottom of developer tools, under "Cookie Value," you should now see your token.

Let me know if this works for you!