Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API subdomain for Heroku app, is it possible?

I am trying to build an API and I am concerned that all my resources will either not be accessible with the api.myapp.com domain or that they will "live" with the wrong uris.

I have added the CNAME for my domain name to point to my Heroku app. (ex: browsing to www.myapp.com takes you to https://myherokuapp.heroku.com)

I would like to set up an API subdomain, so that a GET to https://api.myapp.com takes you to https://myherokuapp.heroku.com/api/v1

The best scenario would be that a POST to https://api.myapp.com/accounts/12345 would create a new account. Is that even possible?

(I know that subdomains (eg: mysubdomain.myappname.heroku.com) are not possible with Heroku)

I believe the answer could be in three different places:

  1. Something to do with DNS provider forwarding configs (maybe something to do with "A" records).
  2. Something to config in Heroku, possibly a paid add-on to handle domains/subdomains.
  3. Handle all subdomains within my app.
like image 621
Scott Avatar asked Mar 01 '13 14:03

Scott


1 Answers

If you want to differentiate between api.mydomain.com and www.mydomain.com and have different controllers for your API requests then you could certainly use Rails routes constrained to your api subdomain to handle this

constraints :subdomain => "api" do
  scope :module => "api", :as => "api" do
   resources :posts
  end
end

which would then use the posts_controller.rb in the app/controllers/api folder of your application.

You'll then have both www.mydomain.com and api.mydomain.com added a custom domains for your application and then the routes will take care of the rest.

You might also want to look into the Grape Gem for helping build your api

like image 189
John Beynon Avatar answered Nov 15 '22 03:11

John Beynon