Bitly (formerly Bit.ly) is a URL shortening and bookmarking service owned by Bitly, Inc. (https://bitly.com/)
Sign up for a Bitly account at https://bitly.com/a/sign_up
There are two methods of authenticating with Bitly's API
1- OAuth access token which can be generated by visiting: https://bitly.com/a/oauth_apps
OR
2- LoginName and ApiKey (deprecated) which can be generated by visiting: https://bitly.com/a/your_api_key/
var reallyLongLink = "http://www.thisisareallylonglink.com/";
var bitly = require('cloud/bitly.js'); //bitly.js must exist in your cloud directory
var authToken = "YOUR_AUTH_TOKEN";
bitly.initializeWithOAuthToken(authToken);
//or you could have initialized with Login/ApiKey (deprecated)
var login = "YOUR_LOGIN";
var api_key = "YOUR_API_KEY";
bitly.initializeWithLoginAndApiKey(login, api_key);
//call shortenUrl, and pass the url you'd like to shorten as 'longUrl'
bitly.shortenUrl({ longUrl : longUrl }).then(handleSuccess, handleError);
Original: https://parse.com/questions/bitlyjs-a-cloud-code-module-for-shortening-long-urls
Revised to use Parse.Promise by SmallSharpTools (http://sstools.co/)
This revision allows for the returned process to be used asynchronously. Below is an example of a Cloud Code function which uses this module asynchronously.
var shortenUrl = function(request, response) {
var handleSuccess = function(shortenedUrl) {
response.success({ shortenedUrl : shortenedUrl });
};
var handleError = function(error) {
response.error(error);
};
bitly.shortenUrl({
longUrl: request.params.longUrl
}).then(handleSuccess, handleError);
};
Parse.Cloud.define("shortenUrl", shortenUrl);