API Rails routes where token is part of the route

54 Views Asked by At

I need to create a Ruby on Rails API where routes will look like

mydomain.com/api/v1/xxxxxxxx/
mydomain.com/api/v1/xxxxxxxx/messages
mydomain.com/api/v1/xxxxxxxx/authors/123/books

xxxxxxxx represent the API key and it should be picked up as a params[:key] or something simmillar likeparams[:api_connection_key]

e.g mydomain.com/api/v1/xxxxxxxx/messages point to app/controllers/api/v1/messages_controller.rb

I'm wondering how would you design the config/routes.rb to achive this ? (controllers and rest of logic is fine, just routes)

no mydomain.com/api/v1/messages?key=xxxxxxxx is not an option and Header authentication is out of the question

1

There are 1 best solutions below

1
Arctodus On BEST ANSWER

scope supports dynamic segments

Rails.application.routes.draw

  namespace :api do
    namespace :v1 do
      scope "/:key" do
        resources :messages
      end
    end
  end
  
end