Rails API versioning strategies

Maya Patel Jan 2026
3 tabs
Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :posts do
        resources :comments, only: [:index, :create]
      end
      resources :users, only: [:show, :update]
    end

    namespace :v2 do
      resources :posts do
        resources :comments
        member do
          post :publish
          post :archive
        end
      end
      resources :users
      resources :profiles
    end

    # Default to latest version
    root to: redirect('/api/v2')
  end
end
3 files · ruby Explain with highlit

API versioning allows backward-incompatible changes without breaking existing clients. I version via URL path (/api/v1/posts) for simplicity and explicit version selection. Each version namespace has its own controllers and serializers. Routes use namespace blocks to organize versions. When creating v2, I duplicate v1 controllers and modify only what changes, sharing models between versions. For minor changes, I use optional params or conditional logic within a version. Deprecation headers warn clients about sunset timelines. I maintain at least two versions simultaneously during transitions. This strategy balances stability for existing clients with evolution for new features.