views:

23

answers:

3

I'm creating an app that allows users to use their own domain. What method do I use in my Rails app to automatically register their chosen domain with Heroku? I'll also need to deregister it if they change it.

+1  A: 

The way you usually add domains in Heroku is using the Heroku API through the Heroku gem. There's a command called heroku domains:add you can invoke

$ heroku domains:add example.com

As I said before, the client calls the Heroku API. You can extract the Heroku Domain API information from the library and create a custom script that calls the Heroku API to add and remove a domain from your app.

Here's the client source code.

Note. Because you are "reverse engineering" and API which appears to be not documented, you should ask Heroku permission to do that, just to be sure you are not creating something against their TOS.

Simone Carletti
Thanks Simone. So there's no documented way to do this legitimately then?
Galen King
Honestly, I don't believe this way is "illegal" because you are not de-compiling anything. BTW, you should contact Heroku to make sure it can be done.
Simone Carletti
No need to worry, Heroku supports using the API, for instance, they mention using it to [change your number of dynos in their docs](http://docs.heroku.com/faq-product-features#how-does-dyno-amp-worker-scaling-work). However, the API may be subject to change in the future.
wuputah
Use of the api isn't illegal, the heroku people are fine with that. They don't advertise the api because it isn't finalized or well documented yet
edgerunner
A: 

The gem is from heroku and is the recommended approach. I just contacted Heroku with the same question and this was there response:

You need to tell Heroku about the domain and where to route it as well. The CNAME tells DNS how to get the request to Heroku. Now you must tell Heroku which app to send it to. You do this by adding the domain to your app. In your case you would need to run "heroku domains:add my.domain.com" to your app. You can also do this programmatically from inside your application over our API. See the Heroku gem (http://github.com/heroku/heroku) for an example of how to connect and use the API.

Nader
A: 

I have contacted Heroku for the same thing, and they just pointed me at their api, and said it is fine to use it that way.

I'm afraid there isn't. Our API is "documented" only by the code of the client.

You may find our google group helpful for getting advice from community members as well: http://groups.google.com/group/heroku/

Oren

Here's the simple how-to:

require 'heroku'

heroku = Heroku::Client.new('heroku_username', 'heroku_password')
heroku.add_domain('heroku_app_name', 'example.com')
heroku.remove_domain('heroku_app_name','example.com')

See the api for more.

Of course I'd recommend against putting a plaintext password into your code. A nice thing you can do is use the heroku environment variables to get your passwords out of the code.

heroku = Heroku::Client.new(ENV['HEROKU_USER'], ENV['HEROKU_PASSWORD'])

and then you can set the environment variables on your app with

$> heroku config:add HEROKU_USER='heroku_username'
$> heroku config:add HEROKU_PASSWORD='heroku_password'

from the command line.

edgerunner