Skip to content

Ruby SDK for Sirv REST API

By , Head of Sirv StudioUpdated

Open this page in a new chat

Short answer

Sirv's official Ruby SDK wraps the Sirv REST API, so you can upload, download, search and manage files, metadata and account statistics from your code. Install it with gem install sirv_rest_api, then connect with the client ID and secret from your Sirv account's API settings. The SDK gets and refreshes the access token for you.

After reading this you will know

  • How to install the SDK and connect to your Sirv account
  • Which file, account, statistics and spin methods it offers
  • How to handle errors and limits

The official Ruby SDK provides a clean Ruby interface for interacting with the Sirv REST API.

It handles authentication automatically with token refresh and uses only Ruby’s standard library with no external dependencies.

Resources:

Install the SDK via RubyGems:

gem install sirv_rest_api

Or add to your Gemfile:

gem 'sirv_rest_api'
  • Ruby 2.7 or higher
  • No external dependencies (uses Ruby stdlib only)
require 'sirv_rest_api'
# Initialize the client
client = SirvRestApi::Client.new(
client_id: 'your-client-id',
client_secret: 'your-client-secret'
)
# Connect (authenticate)
token = client.connect
puts "Connected! Token expires in #{token.expires_in} seconds"
# Get account information
account = client.get_account_info
puts "Account: #{account.alias}"
puts "CDN URL: #{account.cdn_url}"
# List files in a directory
folder = client.read_folder_contents('/')
folder.contents.each do |file|
puts file.filename
end

The SDK handles authentication automatically. When you make your first API call, it obtains an access token using your client credentials.

The token is automatically refreshed when it expires.

client = SirvRestApi::Client.new(
client_id: 'your-client-id',
client_secret: 'your-client-secret'
)
# Connect and get token
token = client.connect
puts "Scopes: #{token.scope.join(', ')}"
# Request a token with custom expiry time (5-604800 seconds)
token = client.connect(expires_in: 3600) # Token valid for 1 hour
# Check if connected
client.connected? # => true
# Get current token
client.access_token
OptionTypeDefaultDescription
client_idStringrequiredYour Sirv API client ID
client_secretStringrequiredYour Sirv API client secret
base_urlStringhttps://api.sirv.comBase URL for API requests
auto_refresh_tokenBooleantrueAutomatically refresh token before expiry
token_refresh_bufferInteger60Seconds before expiry to trigger refresh
timeoutInteger30Request timeout in seconds
max_retriesInteger3Maximum retries for 5xx errors
client = SirvRestApi::Client.new(
client_id: 'your-client-id',
client_secret: 'your-client-secret',
base_url: 'https://api.sirv.com',
auto_refresh_token: true,
token_refresh_buffer: 60,
timeout: 30,
max_retries: 3
)

Complete file management including upload, download, copy, rename, delete, and metadata operations.

CategoryMethods
File Operationsread_folder_contents, get_file_info, upload_file, download_file, copy_file, rename_file, delete_file, create_folder, fetch_url
Metadataget_file_meta, set_file_meta, get_file_title, set_file_title, get_file_description, set_file_description, get_file_tags, add_file_tags, get_product_meta, set_product_meta, get_approval_flag, set_approval_flag
Batch Operationsbatch_zip, get_zip_status, batch_delete, get_batch_delete_status
Spins & Exportspin_to_video, video_to_spin, export_spin_to_amazon, export_spin_to_walmart, export_spin_to_home_depot, export_spin_to_lowes, export_spin_to_grainger
# List folder contents
folder = client.read_folder_contents('/images')
folder.contents.each { |f| puts f.filename }
# Iterate through all items (handles pagination)
client.each_folder_item('/images') do |file|
puts file.filename
end
# Upload a file
client.upload_file_from_path('/images/photo.jpg', './local-photo.jpg')
# Download a file
content = client.download_file('/images/photo.jpg')
File.binwrite('downloaded.jpg', content)
# Copy a file
client.copy_file(from: '/images/photo.jpg', to: '/backup/photo.jpg')
# Delete a file
client.delete_file('/images/photo.jpg')
# Search files
results = client.search_files(query: '*.jpg', size: 100)
puts "Found #{results.total} files"
# Iterate through all search results (handles pagination)
client.each_search_result(query: '*') do |file|
puts file.filename
end
# Set file metadata
client.set_file_meta('/images/photo.jpg', {
title: 'My Photo',
description: 'A beautiful sunset'
})
# Set tags
client.add_file_tags('/images/photo.jpg', ['nature', 'sunset'])
# Set product metadata
client.set_product_meta('/images/product.jpg', {
id: 'SKU123',
name: 'Product Name',
brand: 'Brand Name'
})
jwt = client.generate_jwt(
filename: '/protected/image.jpg',
expires_in: 3600 # 1 hour
)
puts "Protected URL: #{jwt.url}"

Manage your Sirv account settings and retrieve account information.

MethodDescription
get_account_infoGet account information including CDN URL and account name
update_account(options)Update account settings (minify, fetching)
get_account_limitsGet API request limits and usage
get_storage_infoGet storage usage information
get_account_usersList all account users
get_billing_planGet billing plan details
search_events(params)Search account events
mark_events_seen(event_ids)Mark events as seen
# Get account information
account = client.get_account_info
puts "CDN URL: #{account.cdn_url}"
# Get API limits
limits = client.get_account_limits
if limits.rest
puts "REST API: #{limits.rest.remaining}/#{limits.rest.limit}"
end
# Get storage information
storage = client.get_storage_info
puts "Storage used: #{storage.used} bytes"
# Search account events
events = client.search_events(level: 'error')

Retrieve usage statistics for your account.

MethodDescription
get_http_stats(from:, to:)Get HTTP transfer statistics
get_spin_views_stats(from:, to:)Get spin viewer statistics (max 5 days)
get_storage_stats(from:, to:)Get storage statistics
# HTTP transfer statistics
http_stats = client.get_http_stats(from: '2024-01-01', to: '2024-01-31')
# Storage statistics
storage_stats = client.get_storage_stats(from: '2024-01-01', to: '2024-01-31')
# Spin views (max 5-day range)
spin_stats = client.get_spin_views_stats(from: '2024-01-01', to: '2024-01-05')

The SDK provides specific exception types for different error scenarios.

ExceptionDescription
SirvRestApi::AuthenticationErrorInvalid credentials or authentication failed (401)
SirvRestApi::NotFoundErrorResource not found (404)
SirvRestApi::RateLimitErrorRate limit exceeded (429)
SirvRestApi::ValidationErrorInvalid request parameters (400)
SirvRestApi::ApiErrorGeneral API errors
begin
account = client.get_account_info
rescue SirvRestApi::AuthenticationError => e
puts "Auth error: #{e.message}"
rescue SirvRestApi::NotFoundError => e
puts "Not found: #{e.message}"
rescue SirvRestApi::RateLimitError => e
puts "Rate limited: #{e.message}"
rescue SirvRestApi::ApiError => e
puts "API error: #{e.message} (status #{e.status_code})"
end
PropertyDescription
status_codeHTTP status code
error_codeAPI error code (if any)
messageError message

Check the status code and message to see why a request failed.

Back to top