Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add domains to Heroku app using Ruby code?

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.

like image 566
Galen King Avatar asked Oct 19 '10 06:10

Galen King


People also ask

How do I add a domain to Heroku app?

Summary of stepsAdd the custom domain to your app with the heroku domains:add command. Look up the Heroku-supplied DNS target for the custom domain using the heroku domains command. Configure your app's DNS provider to point to the Heroku-supplied DNS target. Confirm that your app is accessible via the custom domain.

How do I add a domain to Heroku without a credit card?

No, it is not possible to add a domain without adding a payment method.

How do I point my GoDaddy domain to Heroku?

Navigate to the project folder from the terminal then type "heroku domains:add www.yourdomain.com". The domain name is the one purchased from GoDaddy. Next, log into your GoDaddy account and find the domains area in the upper left navigation tab. The domains tab will allow you to choose a domain then launch.


3 Answers

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.

like image 142
edgerunner Avatar answered Oct 16 '22 19:10

edgerunner


The heroku gem has now been deprecated. You should use the heroku.rb gem instead. The commands have changed slightly, but it's essentially the same.

require 'heroku-api'

heroku = Heroku::API.new(:api_key => API_KEY)                           # use API Key
heroku = Heroku::API.new(:username => USERNAME, :password => PASSWORD)  # use username and password
heroku = Heroku::API.new(:headers => {'User-Agent' => 'custom'})        # use custom header


heroku.delete_domain('app', 'example.com') # remove the 'example.com' domain from the 'app' app
heroku.get_domains('app')                  # list configured domains for the 'app' app
heroku.post_domain('app', 'example.com')   # add 'example.com' domain to the 'app' app
like image 27
Simmo Avatar answered Oct 16 '22 19:10

Simmo


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.

like image 40
Simone Carletti Avatar answered Oct 16 '22 18:10

Simone Carletti