API subdomain for Heroku application, is this possible?

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

I added a CNAME for my domain name to point to my Heroku application. (for example: browsing at www.myapp.com will lead you to https://myherokuapp.heroku.com )

I would like to configure the API subdomain so that GET https://api.myapp.com will take you to https://myherokuapp.heroku.com/api/v1

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

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

I believe the answer can be in three different places:

  • Something to do with the DNS provider forwarding settings (maybe something related to the "A" records).
  • Something to configure in Heroku, perhaps a paid add-on for handling domains / subdomains.
  • Handle all subdomains in my application.
+7
source share
1 answer

If you want to distinguish between api.mydomain.com and www.mydomain.com and have different controllers for your API requests, then you can certainly use the Rails routes limited to your api subdomain to handle this

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

which will then use posts_controller.rb in the app / controller / api folder of your application.

Then you have both www.mydomain.com and api.mydomain.com added custom domains for your application, and then the routes will take care of the rest.

You can also look in Grape Gem to help you build api

+2
source

All Articles