Static whitelisted IP address with Rails, Heroku and HTTParty

Rémi Duvoux
Aug 29, 2018 · 2 min read

Some APIs require your IP to be whitelisted beforehand for security reasons. However, due to the way Heroku works, it is impossible to get a static IP address from Heroku, and you won’t be able to whitelist anything. Which means no access to the API. It was our case for some API at Okarito, and we had to figure out a solution.

Meet QuotaGuard, a Heroku addon that provides you with 2 static IP addresses that you can use for your whitelisted API calls.

Add the addon in the “Resources” tab in Heroku

As soon as you add the plugin to your current app, it automatically adds a config variable named QUOTAGUARDSTATIC_URL. Click on the QuotaGuard Static addon to arrive on your dashboard, and discover the 2 static IP addresses that have been created for you.

Follow the instructions on http://support.quotaguard.com/support/solutions/articles/5000013940-getting-started-with-the-httparty-gem-quotaguard-static and use these IP addresses for the first time!

A few seconds later, you see that your IP is indeed one of the 2 static IPs provided by QuotaGuard.

However, even though this setup works great for simple GET requests, I didn’t find anything working with HTTParty POST requests, and I had to find a small hack to make it work.

Create a file called httproxy.rb in your initializers folder.

In this file, create the HTTProxy class, that will use the QuotaGuard params when in production (you don’t want to waste money by using their service for development purposes).

The HTTProxy class :)

Restart your server.

You were using HTTParty to make API calls, you will now be able to use HTTProxy, which will use your QuotaGuard static IPs. It will behave exactly like HTTParty (it includes it), but will ensure your static IP will be used.

Comparing the output of HTTParty and HTTProxy in production.

Now, back to code 💪

(Disclaimer: I am not related to QuotaGuard, that I discovered today. I chose them because they were the only static IP Heroku addon available in Europe).

Rémi Duvoux

Written by

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade