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
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 #
Get an API key
Great! Now you can interact vie the API.
2. Use the Vimeo API with 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.
Did you like this article? Did it save you some time?