8

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.
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Scott
  • 327
  • 1
  • 5
  • 13
  • hey @Scott i'm trying to do this exact same thing but Heroku and GoDaddy - can't seem to get them to work. would you be able to post your answer to the above solution: i.e. your routes file? that would be very handy to the 1500 people who have thus viewed your answer. – BenKoshy Jan 14 '17 at 03:49
  • @BKSpurgeon It was almost 4 years ago since I asked this question ;) But yes, the answer is in the comments below: 1. Add the CNAME(Alias) api.myapp.com to my DNS, and have it point to myherokuapp.heroku.com 2. Add the domain api.myapp.com to Heroku. 3. Use a module in your node app to handle subdomain routing. I used https://www.npmjs.com/package/express-subdomain to route all api subdomain traffic to the API endpoint at myherokuapp.heroku.com/api/v1. Since then, I have a DIY server at Digital Ocean ($5/mo) and do the routing with Nginx before even hitting the app. – Scott Jan 15 '17 at 19:04

1 Answers1

2

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

John Beynon
  • 37,398
  • 8
  • 88
  • 97
  • interesting approach, although I am using Node.js the idea is the same. So you suggest that my app can handle all incoming subdomain requests. I can use DNS forwarding from my DNS provider, and forward something like "https://myapp.com" to "https://myherokuapp.heroku.com/api/v1", but I don't see how to make it forward "https://api.myapp.com" to "https://api.myherokuapp.heroku.com" I don't think this is possible, and any hit to api.myapp.com will result in the address bar change...and my URIs will be tied to heroku's app name. Any ideas how to get around this? – Scott Mar 04 '13 at 03:15
  • 2
    you can have multiple domains on a heroku app eg api.mydomain.com and www.mydomain.com - you would CNAME both of these to myapp.herokuapp.com and then add them as custom domains via the heroku control panel. You would not see the address bar change, ie if a visitor hits api.myapp.com it would stay as that and www.myapp.com would stay as that. Once in your application you simply handle the request based on the subdomain being requested in what ever language you are coding in. – John Beynon Mar 04 '13 at 08:34
  • 1
    Your advice on the CNAME was what I needed. The URL can now be api.myapp.com and it redirects to the myherokuapp.heroku.com without changing the URL. So in summary I had to: 1. Add the CNAME(Alias) api.myapp.com to my DNS, and have it point to myherokuapp.heroku.com 2. Add the domain api.myapp.com to Heroku. 3. Use a module in my app to handle subdomains. – Scott Mar 18 '13 at 15:37
  • @praveena_kd yes I had to add the CNAME(Alias) to my domain provider and to Heroku. Anyone going to api.myapp.com would be redirected (without the URL changing in the address bar) to myherokuapp.heroku.com/api/ So when I go to "http://api.myapp.com/myRESTresource" it will actually be directed to myherokuapp.heroku.com/api/myRESTresource – Scott Jul 08 '13 at 17:44
  • @Scott Hi Scott, which module did you use to handle the subdomains? – AmpT Feb 26 '14 at 19:51
  • @AmpT I believe it was this approach. I am using Node/Express. http://stackoverflow.com/questions/5791260/how-can-i-configure-multiple-sub-domains-in-express-js-or-connect-js – Scott Apr 03 '14 at 12:14
  • @JohnBeynon thank you but will this work for Heroku? If one points api.mydomainName.com to heroku without an .api subdomain - how will Heroku figure out to serve the api controllers and views? – BenKoshy Jan 16 '17 at 22:01