DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building an Internal TLS and SSL Certificate Monitoring Agent: From Concept to Deployment
  • Beyond Manual Annotation: Engineering Self-Correcting Pseudo-Labeling Pipelines
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • Implementing Secure API Gateways for Microservices Architecture

Trending

  • Implementing Observability in Distributed Systems Using OpenTelemetry
  • 5 Common Security Pitfalls in Serverless Architectures
  • Getting Started With Agentic Workflows in Java and Quarkus
  • Chaos Engineering Has a Blind Spot. Agentic AI Lives in It.
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Use the Bitly API in Ruby

How to Use the Bitly API in Ruby

Bitly has an API so that you can shorten, expand and get metrics on links all in your app. This is how to use that Bitly API in Ruby.

By 
Phil Nash user avatar
Phil Nash
·
Nov. 19, 22 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
8.1K Views

Join the DZone community and get the full member experience.

Join For Free

Link shortening has been around for a long time, and Bitly is arguably the king of link shorteners. It has support for shortening long URLs as well as custom short links, custom domains, and metrics to track how each link is performing.

For those of us with the power of code at our fingertips, Bitly also has an API. With the Bitly API, you can build all of the functionality of Bitly into your own applications and expose it to your users. In this post, you’ll learn how to use the Bitly Ruby gem to use the Bitly API in your Ruby applications.

Getting Started

To start shortening or expanding links with the Bitly gem, you’ll need Ruby installed and a Bitly account.

To make API requests against the Bitly API you will need an access token. Log in to your Bitly account and head to the API settings. Here you can enter your account password and generate a new token. This token will only be shown once, so copy it now.

Using the Bitly API

Open a terminal and install the Bitly gem:

Shell
 
gem install bitly


Let’s explore the gem in the terminal. Open an irb session:

Shell
 
irb


Require the gem:

Ruby
 
require "bitly"


Create an authenticated API client using the token you created in your Bitly account:

Ruby
 
client = Bitly::API::Client.new(
  token: "Enter your access token here"
)


Shortening a URL

You can now use this client object to access all the Bitly APIs. For example, you can shorten a URL to a Bitlink like this:

Ruby
 
long_url = "https://twitter.com/philnash"
bitlink = client.shorten(long_url: long_url)
bitlink.link
# => "https://bit.ly/3zYdN21"


The shorten endpoint is a simplified method of shortening a URL. You can also use the create endpoint and set other attributes, like adding a title, tags, or deeplinks into native applications.

Ruby
 
long_url = "https://twitter.com/philnash"
bitlink = client.create_bitlink(
  long_url: long_url,
  title: "Phil Nash on Twitter",
  tags: ["social media", "worth following"]
)
bitlink.link
# => "https://bit.ly/3zYdN21"
bitlink.title
# => "Phil Nash on Twitter"


Expanding a URL

The API client can also be used to expand Bitlinks. You can use the expand method with any Bitlink, not just ones that you have shortened yourself. When you expand a URL you will get back publicly available information about the URL.

Ruby
 
bitlink = client.expand(bitlink: "bit.ly/3zYdN21")
bitlink.long_url
# => "https://twitter.com/philnash"
bitlink.title
# => nil
# (title is not public information)


If the URL was a Bitlink from your own account you get more detailed information when you use the bitlink method.

Ruby
 
bitlink = client.bitlink(bitlink: "bit.ly/3zYdN21")
bitlink.long_url
# => "https://twitter.com/philnash"
bitlink.title
# => "Phil Nash on Twitter"


Other Bitlink Methods

Once you have a bitlink object, you can call other methods on it. If you wanted to update the information about the link, for example, you can use the update method:

Ruby
 
bitlink.update(title: "Phil Nash on Twitter. Go follow him")
bitlink.title
# => "Phil Nash on Twitter. Go follow him"


You can also fetch metrics for your link, including the clicks_summary, link_clicks and click_metrics_by_country. For example:

Ruby
 
click_summary = bitlink.clicks_summary
click_summary.total_clicks
# => 1
# (not very popular yet)


Methods that return a list of metrics implement Enumerable so you can loop through them using each:

Ruby
 
country_clicks = bitlink.click_metrics_by_country
country_clicks.each do |metric|
  puts "#{metric.value}: #{metric.clicks}"
end
# => AU: 1
# (it was just me clicking it)


With these methods, you can create or fetch short links and then retrieve metrics about them. Your application can shorten links and measure their impact with a few lines of code.

There's More!

For advanced uses, you can also authorise other Bitly accounts to create and fetch short links via OAuth2 as well as manage your account’s users, groups, and organisations.

A Useful Little Tool

To find out more about using the Bitly API with Ruby, you can read the Bitly API documentation, the Bitly gem’s generated documentation, and the docs in the GitHub repo.

If you are interested, you can also read a bit more about the backstory of the Bitly Ruby gem. I’ve been working on this project since 2009, would you believe?

Are you using the Bitly API or do you have any feedback or feature requests? Let me know on Twitter at @philnash or open an issue in the repo.

API Ruby (programming language)

Published at DZone with permission of Phil Nash. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building an Internal TLS and SSL Certificate Monitoring Agent: From Concept to Deployment
  • Beyond Manual Annotation: Engineering Self-Correcting Pseudo-Labeling Pipelines
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • Implementing Secure API Gateways for Microservices Architecture

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook