# Source: https://dub.co/docs/sdks/ruby.md

> ## Documentation Index
> Fetch the complete documentation index at: https://dub.co/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Ruby SDK

> Learn how to integrate Dub with Ruby.

## Installation

```bash  theme={null}
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:

```ruby  theme={null}
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](/concepts/links/organization)
2. [Bulk link operations (create, update, delete)](/concepts/links/bulk-operations)
3. [Retrieving link analytics](/concepts/analytics)

## Frameworks

You can use the Dub Ruby SDK with any Ruby framework:

1. [Usage with Rails](/sdks/quickstart/rails)
2. [Usage with Sinatra](/sdks/quickstart/sinatra)

If you're using a different Ruby framework, you can refer to the [Ruby SDK quickstart](/sdks/quickstart/ruby) for a basic example.

## Additional Resources

<CardGroup cols={2}>
  <Card title="RubyGems Package" icon="gem" href="https://d.to/ruby/sdk">
    Download and install the Dub Ruby SDK on RubyGems
  </Card>

  <Card title="SDK Reference" icon="book" iconType="solid" href="https://github.com/dubinc/dub-ruby/blob/main/README.md">
    View the complete SDK reference documentation
  </Card>

  <Card title="Examples" icon="github" href="https://github.com/dubinc/examples/tree/main/ruby">
    Quickstart examples with the Ruby SDK
  </Card>

  <Card title="Source Code" icon="github" href="https://github.com/dubinc/dub-ruby">
    View the complete source code for the Dub Ruby SDK
  </Card>
</CardGroup>