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

# Ruby/Rails SDK


[changelog-link]: https://github.com/cloudinary/cloudinary_gem/blob/master/CHANGELOG.md

The Cloudinary Ruby/Rails SDK provides simple, yet comprehensive image and video upload, transformation, optimization, and delivery capabilities through the [Cloudinary APIs](cloudinary_references#url_and_rest_apis), that you can implement using code that integrates seamlessly with your existing Ruby or Ruby on Rails application.
> **INFO**: :title=SDK security upgrade, June 2025

We recently released an enhanced security version of this SDK that improves the validation and handling of input parameters. We recommend upgrading to the [latest version][changelog-link] of the SDK to benefit from these security improvements.

## How would you like to learn?

{table:class=no-borders overview}Resource | Description 
--|--
[Ruby/Rails quick start](ruby_rails_quickstart) | Get up and running in five minutes with a walk through of installation, configuration, upload, management and transformations.
[Cloudinary Ruby/Rails SDK GitHub repo](https://github.com/cloudinary/cloudinary_gem) | Explore the source code and see the [CHANGELOG][changelog-link] for details on all new features and fixes from previous versions. 
[Video tutorials](tutorial_screencasts) | Watch tutorials relevant to your use cases to learn how to use Cloudinary features. 
 | Try the free [Introduction to Cloudinary for Ruby Developers](https://training.cloudinary.com/courses/introduction-for-api-users-and-ruby-developers) online course, where you can learn how to upload, manage, transform and optimize your digital assets.

Other helpful resources...

This guide focuses on how to set up and implement popular Cloudinary capabilities using the Ruby/Rails SDK, but it doesn't cover every feature or option. Check out these other resources to learn about additional concepts and functionality in general. 

{table:class=no-borders overview}Resource | Description 
--|--
[Developer kickstart](dev_kickstart) |A hands-on, step-by-step introduction to Cloudinary features.
[Glossary](cloudinary_glossary) | A helpful resource to understand Cloudinary-specific terminology.
[Guides](programmable_media_guides) | In depth guides to help you understand the many, varied capabilities provided by the product. 
[References](cloudinary_references) | Comprehensive references for all APIs, including Ruby/Rails code examples.

## Install

Cloudinary's Ruby library is available as an open-source Ruby gem. 
  
To install the Cloudinary Ruby gem, run:

```ruby
gem install cloudinary
```

If you use Rails, edit your *Gemfile*, add the following line and run *bundle*.
  
```ruby
gem 'cloudinary'
```

> **NOTE**: Check that you're using a [supported version](https://github.com/cloudinary/cloudinary_gem?tab=readme-ov-file#Version-Support) of Ruby and/or Rails.

## Configure

### Load the Cloudinary Ruby library

```ruby
require 'cloudinary'
```

### Set required configuration parameters

You can set the required configuration parameters, `cloud_name`, `api_key` and `api_secret` in one of a few ways: 

* Using the `CLOUDINARY_URL` [environment variable](#environment_variable_configuration)
* Setting them in a [cloudinary.yml file](#cloudinary_yml_file_configuration) 
* Setting them in an [initializer file](#initializer_file_configuration)
> **TIP**: To generate transformation URLs, you only need to configure the cloud name. The API key and API secret aren't required for URL generation.
#### Environment variable configuration
To define the `CLOUDINARY_URL` environment variable:

1. Copy the **API environment variable** format from the [API Keys](https://console.cloudinary.com/app/settings/api-keys) page of the Cloudinary Console Settings. 
2. Replace `<your_api_key>` and `<your_api_secret>` with your actual values. Your cloud name is already correctly included in the format. 
 
For example:

```
CLOUDINARY_URL=cloudinary://my_key:my_secret@my_cloud_name
```
> **NOTES**:
>
> * When writing your own applications, follow your organization's policy on storing secrets and don't expose your API secret. * If you use a method that involves writing your environment variable to a file (e.g. `dotenv`), exclude the file from your version control system, so as not to expose it publicly. 

> * You may also want to check out the [API Keys and Credentials](finding_your_credentials_tutorial) video tutorial. 

> * When using Cloudinary through a PaaS add-on, such as Heroku, the environment variable is automatically defined in your deployment environment. 

> * You can always override the global configuration values specified by passing different values in specific Cloudinary calls.

> * If you use more than one global method of setting configuration options, the order of precedence is:
>    `CLOUDINARY_URL`   **->**   `cloud_name`  **->**   `cloudinary.yml`/`cloudinary.rb`

#### cloudinary.yml file configuration

Configure the required Cloudinary credentials in your **.yml** file as follows:
 
```ruby
production:
  cloud_name: "my_cloud_name"
  api_key: "874837483274837"
  api_secret: "a676b67565c6767a6767d6767f676fe1"
```

> **NOTE**:
>
> If using Rails, place the above in a `cloudinary.yml` file in the **config** folder of your Rails project.

#### initializer file configuration

Configure the required Cloudinary credentials in your initializer file as follows:

```ruby
Cloudinary.config do |config|
  config.cloud_name = 'my_cloud_name'
  config.api_key = '874837483274837'
  config.api_secret = 'a676b67565c6767a6767d6767f676fe1'
end
```

Or alternatively

```ruby
Cloudinary.config_from_url("cloudinary://my_key:my_secret@my_cloud_name")
```
> **NOTE**:
>
> If using Rails, place the above in a file named `cloudinary.rb` in the **/config/initializers** folder of your Rails project.

### Set additional configuration parameters

In addition to the required configuration parameters, you can define relevant [optional configuration parameters](cloudinary_sdks#configuration_parameters) as shown in the examples below:

* **Environment variable**:

    If you're using the CLOUDINARY_URL [environment variable](#environment_variable_configuration), you can append optional configuration parameters to the environment variable. For example, adding the optional `upload_prefix` and `secure_distribution` parameters:

    ```
    CLOUDINARY_URL=cloudinary://my_key:my_secret@my_cloud_name?secure_distribution=mydomain.com&upload_prefix=https://api-eu.cloudinary.com
    ```
* **cloudinary.yml**:

    ```ruby
    production:
      cloud_name: "my_cloud_name"
      api_key: "874837483274837"
      api_secret: "a676b67565c6767a6767d6767f676fe1"
      secure_distribution: 'mydomain.com',
      upload_prefix: 'https://api-eu.cloudinary.com'
    ``` 
* **initializer file**:     

    ```ruby
    Cloudinary.config do |config|
      config.cloud_name = 'my_cloud_name'
      config.api_key = '874837483274837'
      config.api_secret = 'a676b67565c6767a6767d6767f676fe1'
      secure_distribution = 'mydomain.com',
      upload_prefix = 'https://api-eu.cloudinary.com'
    end
    ```

      Or 

    ```ruby
    Cloudinary.config_from_url("cloudinary://my_key:my_secret@my_cloud_name?secure_distribution=mydomain.com&upload_prefix=https://api-eu.cloudinary.com")
    ```

> **NOTE**: By default, URLs generated with this SDK include an appended SDK-usage query parameter. Cloudinary tracks aggregated data from this parameter to improve future SDK versions. We don't collect any individual data. If needed, you can disable the `urlAnalytics` configuration option. [Learn more](cloudinary_sdks#analytics_config).

## Use

Once you've installed and configured the Ruby SDK, you can use it for:
* **Uploading files to your product environment**: You can upload any files, not only images and videos, set your own naming conventions and overwrite policies, moderate and tag your assets on upload, and much more. [See&nbsp;example](#quick_example_file_upload)
* **Transforming and optimizing images and videos**: Keeping your original assets intact in your product environment, you can deliver different versions of your media - different sizes, formats, with effects and overlays, customized for your needs. [See&nbsp;example](#quick_example_transform_and_optimize)
* **Managing assets**: Using methods from the Admin and Upload APIs, you can organize your assets, for example, list, rename and delete them, add tags and metadata and use advanced search capabilities. [See&nbsp;example](#quick_example_get_details_of_a_single_asset)

Capitalization and data type guidelines...

When using the Ruby/Rails SDK, keep these guidelines in mind:  

* Parameter names: `snake_case`. For example: **public\_id**
* Classes: `PascalCase`. For example: **PictureUploader**
* Methods: `snake_case`. For example: **cl\_image\_upload\_tag**
* Pass parameter data as: `Hash`

### Quick example: File upload

The following Ruby code uploads the `dog.mp4` video using the public\_id, `my_dog`. The video overwrites the existing `my_dog` video if it exists. When the video upload finishes, the specified notification URL receives details about the uploaded media asset.

```ruby
Cloudinary::Uploader.upload("dog.mp4", 
  asset_folder: "pets", public_id: "my_dog", overwrite: true, 
  notification_url: "https://mysite.example.com/notify_endpoint", resource_type: "video")
```

> **Learn more about upload**:
>
> * Read the [Upload guide](upload_images) to learn more about customizing uploads, using upload presets and more.

> * See more examples of [image and video upload](rails_image_and_video_upload) using the Cloudinary Ruby/Rails library.  

> * Explore the [Upload API reference](image_upload_api_reference) to see all available methods and options.

### Quick example: Transform and optimize

Take a look at the following transformation code and the image it delivers:

![sample transformation](https://res.cloudinary.com/demo/image/upload/c_thumb,g_face,h_150,w_150/r_20/e_sepia/l_cloudinary_icon/e_brightness:90/o_60/c_scale,w_50/fl_layer_apply,g_south_east,x_5,y_5/a_10/front_face.png "disable_all_tab: true, with_url: false, frameworks:ruby")

```ruby
cl_image_tag("front_face.png", transformation: [
  {gravity: "face", height: 150, width: 150, crop: "thumb"},
  {radius: 20},
  {effect: "sepia"},
  {overlay: "cloudinary_icon"},
  {effect: "brightness:90"},
  {opacity: 60},
  {width: 50, crop: "scale"},
  {flags: "layer_apply", gravity: "south_east", x: 5, y: 5},
  {angle: 10}
  ])
```

This relatively simple code performs all of the following on the original front_face.jpg image before delivering it:

* **Crop** to a 150x150 thumbnail using face-detection gravity to automatically determine the location for the crop
* **Round the corners** with a 20 pixel radius
* Apply a **sepia effect**
* **Overlay the Cloudinary logo** on the southeast corner of the image (with a slight offset). Scale the logo overlay down to a 50 pixel width, with increased brightness and partial transparency (opacity = 60%).
* **Rotate** the resulting image (including the overlay) by 10 degrees
* **Convert** and deliver the image in PNG format (the originally uploaded image was a JPG)

And here's the URL that's automatically generated and included in an image tag from the above code:

![sample transformation](https://res.cloudinary.com/demo/image/upload/c_thumb,g_face,h_150,w_150/r_20/e_sepia/l_cloudinary_icon/e_brightness:90/o_60/c_scale,w_50/fl_layer_apply,g_south_east,x_5,y_5/a_10/front_face.png "disable_all_tab: true, with_code:false, with_image:false")

In a similar way, you can [transform a video](rails_video_manipulation#video_transformation_examples).

> **Learn more about transformations**:
>
> * Read the [image](image_transformations) and [video](video_manipulation_and_delivery) transformation guides to learn about the different ways to transform your assets.

> * See more examples of [image](rails_image_manipulation) and [video](rails_video_manipulation) transformations using the Cloudinary Ruby/Rails library.  

> * See all possible transformations in the [Transformation URL API reference](transformation_reference).

### Quick example: Get details of a single asset  

The following example uses the Admin API [resource](admin_api#get_details_of_a_single_resource_by_public_id) method to return details of the image with public ID `cld-sample`:

```ruby
result = Cloudinary::Api
.resource('cld-sample')
```

#### Sample response

```json
{
  "asset_id": "bf98540caf22ed65775ee0951f4746c9",
  "public_id": "cld-sample",
  "format": "jpg",
  "version": 1719304891,
  "resource_type": "image",
  "type": "upload",
  "created_at": "2024-06-25T08:41:31Z",
  "bytes": 476846,
  "width": 1870,
  "height": 1250,
  "backup": true,
  "asset_folder": "",
  "display_name": "cld-sample",
  "url": "http://res.cloudinary.com/cld-docs/image/upload/v1719304891/cld-sample.jpg",
  "secure_url": "https://res.cloudinary.com/cld-docs/image/upload/v1719304891/cld-sample.jpg",
  "next_cursor": "497b323dcb9883a15a5e6a7cfb75d439e4de1ca882f5cbe8de6a8b322c37bdad",
  "derived": [
    {
      "transformation": "c_scale,w_500",
      "format": "jpg",
      "bytes": 22871,
      "id": "ce3d7bf3068809656dc5aa76572535da",
      "url": "http://res.cloudinary.com/cld-docs/image/upload/c_scale,w_500/v1719304891/cld-sample.jpg",
      "secure_url": "https://res.cloudinary.com/cld-docs/image/upload/c_scale,w_500/v1719304891/cld-sample.jpg"
    }
  ]
}

 ```

> **Learn more about managing assets**:
>
> * Check out the [Manage and analyze assets](asset_management) guide for all the different capabilities.

> * Get an overview of [asset management](rails_asset_administration) using the Ruby/Rails SDK.

> * Select the **Ruby** tab in the [Admin API](admin_api) and [Upload API](image_upload_api_reference) references to see example code snippets.

## Rails-specific features

You can use Cloudinary's Ruby gem for any Ruby application. However, this SDK also provides some extra rails-specific functionality for those developing Ruby on Rails applications:

* Rails view [image](rails_image_manipulation#the_cl_image_tag_method) and [video](rails_video_manipulation#video_tag_helper_method) helper tags for embedding and transforming images and videos
* [Migration tool](rails_image_and_video_upload#migrating_assets_to_cloudinary)
* [Active Storage integration](rails_activestorage)
* [CarrierWave plugin](rails_carrierwave)
* [Static image syncing](rails_image_manipulation#static_files) for CDN delivery
    

## Sample projects

For additional useful code samples and to learn how to integrate Cloudinary with your Rails applications, take a look at our [Sample Projects](https://github.com/cloudinary/cloudinary_gem/tree/master/samples). 
      
* **Basic Ruby sample**: Uploading local and remote images to Cloudinary and generating various transformation URLs.
* **Basic Rails sample**: Uploading local and remote images in a Rails project while embedding various transformed images in a Rails web view.
* **Rails Photo Album**: A fully working web application. It uses CarrierWave to manage images of an album model (database). Performs image uploading both from the server side and directly from the browser using a jQuery plugin.
* **Active Storage sample**: Demonstrates the setup for an Active Storage integration.

> **READING**:
>
> * Get your first Ruby/Rails SDK project up and running in ~5 minutes with the Ruby/Rails SDK [quick start](ruby_rails_quickstart).

> * Learn more about [uploading images and videos](rails_image_and_video_upload).    

> * See examples of powerful [image](rails_image_manipulation) and [video](rails_video_manipulation) transformations using Ruby code and see our [image transformations](image_transformations) and [video transformation](video_manipulation_and_delivery) docs.   

> * Check out Cloudinary's [asset administration](rails_asset_administration) capabilities, for example, renaming and deleting assets, adding tags and metadata to assets, and searching for assets.

> * Make sure to read about [Rails CarrierWave](rails_carrierwave), [Attachinary](rails_attachinary), and [ActiveStorage](rails_activestorage) integrations. 

> * Stay tuned for updates by following the [Release Notes](programmable_media_release_notes) and the [Cloudinary Blog](https://cloudinary.com/blog).

