Create a gist now

Instantly share code, notes, and snippets.

Bitly shortening from the command line with node, motherfuckers.

This is for Mac OS X and requires npm.


  1. Open a terminal
  2. Type bitly

The shortened link is now in your clipboard. Paste the shortened link wherever you need to!


  1. Open a terminal
  2. Type npm install request (if you don't already have the request module).
  3. Type cd /usr/local/lib/node
  4. Type vim bitly.js
  5. Now paste in the following code:
var request = require('request'),
		sys = require('sys'),
		link = process.ARGV[2],
		bitly = '';
function urlencode(str) { 
return escape(str)
    .replace('+', '%2B')
    .replace('%20', '+')
    .replace('*', '%2A')
    .replace('/', '%2F').replace('@', '%40');

// prepend http if it does not exist.
if (!/^https?:\/\//i.test(link)) {
    link = 'http://' + link;

// urlencode the link and add to params.
var params = 'login=YOUR_LOGIN_ID&apiKey=YOUR_API_KEY&longUrl='+urlencode(link)+'%2F&format=json';

request({uri:bitly, body:params, method:'POST'},function(error,response,body){
	if(!error && response.statusCode === 200 && body)
		var json = JSON.parse(body);

NOTE: You will need to supply your own login and apiKey values.

  1. Type ESC:wq to save the file and close it.
  2. Back in your terminal type vim ~/.bash_profile
  3. Paste in the following block of code:
function bitly()
node $bitlyPath $1 | pbcopy
  1. Type ESC:wq to save the file and close it.
  2. Restart your terminal.
  3. Type bitly

The bitly link for "" is now in copied into your clipboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment