---
title: "Use Vimeo API with Ruby on Rails"
author: "Yaroslav Shmarov"
date: "2024-10-24"
canonical_url: "https://blog.superails.com/vimeo-api"
markdown_url: "https://blog.superails.com/vimeo-api.md"
description: "I host SupeRails PRO videos on Vimeo. It is great because: no youtube branding & ads => more premium feeling I don't have to think about hosting my videos &…"
tags: ["ruby","rails","youtube","api","vimeo"]
license: "GNU AGPL v3. Credit Yaroslav Shmarov and link the canonical URL."
---

# Use Vimeo API with Ruby on Rails

I host SupeRails PRO videos on Vimeo.

It is great because:

- no youtube branding & ads => more premium feeling
- I don't have to think about hosting my videos & styling my player
- has basic security features, as "video can be viewed only on selected domains"
- fixed cost, not usage based like [MUX.com](https://www.mux.com/)

But having hundreds of videos in my library I want an easy way to import videos from Vimeo into my Ruby on Rails app.

I did not feel comfortable using an Vimeo API gems, but their own API is very good. **You don't need an api gem to use an API!**

### 1. Get a Vimeo API key

[Create a Vimeo app](https://developer.vimeo.com/apps/new)

Get an API key

![vimeo api authentication](https://blog.superails.com/assets/images/vimeo-api-authentication.png)

Great! Now you can interact vie the API.

### 2. Use the Vimeo API with Ruby:

- [General Vimeo API docs](https://developer.vimeo.com/api/reference)
- [GET videos/:id](https://developer.vimeo.com/api/reference/videos#get_video)

```ruby
# bundle add faraday

ACCESS_TOKEN = Rails.application.credentials.dig(:vimeo, :access_token)

conn = Faraday.new(url: 'https://api.vimeo.com') do |faraday|
  faraday.request :authorization, :Bearer, ACCESS_TOKEN
end

# ping the API
response = conn.get("/me")

body = JSON.parse(response.body)
user_path = body['uri']
# "/users/235423523"

# get a list of all your videos
response = conn.get("/me/videos")

# get list of all videos of a user (25 per page)
response = conn.get("#{user_path}/videos")

body = JSON.parse(response.body)
# get first video
video = body['data'].first
video['duration']
# 501 (seconds)

# get video by id
# https://vimeo.com/1021521307/832cd4eee7
response = conn.get("videos/45345435")

raise "Failed to fetch video data: #{response.message}" unless response.success?

body = JSON.parse(response.body)
body['duration'] # 501 (seconds)
```

That's it! You can now get all videos, or get a video payload based on an ID.
