Skip to main content

Installation

gem install dub

Basic Usage

Here’s how you can use the Dub Ruby SDK to create a link and retrieve click analytics in timeseries format for it:
require 'dub'

# Initialize the Dub SDK with your API key
dub = ::OpenApiSDK::Dub.new
dub.config_security(
  ::OpenApiSDK::Shared::Security.new(
    token: ENV['DUB_API_KEY'], # optional, defaults to DUB_API_KEY
  )
)

# Create a new link
req = ::OpenApiSDK::Operations::CreateLinkRequest.new(
  request_body: ::OpenApiSDK::Operations::CreateLinkRequestBody.new(
    url: "https://google.com"
  )
)

res = dub.links.create(req)
puts res.raw_response.body # e.g. { "shortLink": "https://dub.sh/abc123" }

# Get analytics for the link
analytics_req = ::OpenApiSDK::Operations::RetrieveAnalyticsRequest.new(
  link_id: res.raw_response.body["id"],
  interval: ::OpenApiSDK::Operations::Interval::THIRTYD,
  group_by: ::OpenApiSDK::Operations::GroupBy::TIMESERIES
)

analytics_res = dub.analytics.retrieve(analytics_req)
puts analytics_res.raw_response.body # e.g. [{ "start": "2024-01-01", "clicks": 100 }]
For more usage examples:
  1. Organizing links by external ID, tenant ID, tags, etc
  2. Bulk link operations (create, update, delete)
  3. Retrieving link analytics

Frameworks

You can use the Dub Ruby SDK with any Ruby framework:
  1. Usage with Rails
  2. Usage with Sinatra
If you’re using a different Ruby framework, you can refer to the Ruby SDK quickstart for a basic example.

Additional Resources

RubyGems Package

Download and install the Dub Ruby SDK on RubyGems

SDK Reference

View the complete SDK reference documentation

Examples

Quickstart examples with the Ruby SDK

Source Code

View the complete source code for the Dub Ruby SDK