Google URL Shortener API
Google lunched URL shortener site called goo.gl. Now they added API to that site, so we can integrate in ours applications.
We can shorten and expand URLs, as well as fetch history and analytics. First at all we have to create our API key through Google APIs Console.
Google API is for free for with certain limitations. Currently this is 1 million request per day with 1 request per second per user. We can tune the second limitation.
First at all we should setup Traffic Controls. We should choose if our source of request will be Browser-embeded scripts or Server-side applications. We should choose the former when we want to send requests from browsers, and the latter when we have application and many different users use API through it. If you still in doubt, I can simplify that:
- Browser-embeded scripts -> Google is using HTTP request to match the user.
- Server-side applications -> Google is using userip parameter value or machine IP address if userip parameter is not provided.
Additionally we may create white list of users which may connect with the service.
Depending on our needs we should change Per-User-Limit: by default it is 1.
So Let’s try it, as example we invoke revers mapping so we send shortUrl and google should answer with long url:
https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=
The answer is:
{ "kind": "urlshortener#url", "id": "http://goo.gl/fbsS", "longUrl": "http://www.google.com/", "status": "OK" }
The API is described here: Google URL Shortener API (currently in Labs). We have three functions: insert, get, and list. Insert and get operations are used to deliver shortening, list operation is for statistics.
Get and Insert operation may be accessed without the key parameter, the downside is not being count into statistics.
API is simple and docs provided by google is enough to play with it using curl and oacurl extension. Check Getting Started for details.
Conclusion
Simple but powerful API, 1 million request per day seems to be enough, nice feature is having nice statistics for free with partitioning by platform, browser, country, referrer for all time, month, week, day, two hours period.
Also I found performance measurements which show goo.gl is the fastest solution. See “Is goo.gl really the fastest URL shortener?” article for details.