Tuesday, March 18, 2014

Use bitly python to shorten the url

We are using bitly to provide short url to users, which is little easy to look at visually and also (probably) motivate user to share instead of long long urls.  You might have seen those on while trying to tweet the link or share it on Facebook.

We are using Django for our web app, and getting short url from bitly while sending certain links in the email to user and provide it on the app to enable sharing.

- Get your code from bitly account, you can get it from settings > Advanced > Legacy API Key



- Install bitly-python-api - Its official api provided by bitly for python.

- Now in following quick steps you can get the short URL:

import bitly_api
con = bitly_api.Connection(
                    'myapp',
                    'R_12345....'
                )
shorten = con.shorten('https://myapp.com/abc/?item=55678')
shorten_url = shorten['url']
//output - https://bit.ly/xyz123

bitly_api takes care of underlying connection complexity, if you using any other platform you can find related packages and instruction over here - http://dev.bitly.com/code_libraries.html


No comments:

Post a Comment