Back

Step by Step Guide to Building an Unbounce API Integration in Ruby

Aug 11, 2024 • 5 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your marketing efforts with Unbounce? Let's dive into building a Ruby integration for the Unbounce API. This powerhouse combo will let you programmatically manage your landing pages and leads like a pro.

Prerequisites

Before we jump in, make sure you've got:

  • A Ruby environment (2.5+ recommended)
  • Unbounce API credentials (if you don't have these, hop over to your Unbounce account settings)

Setting up the project

First things first, let's get our project off the ground:

mkdir unbounce_integration cd unbounce_integration bundle init

Now, crack open that Gemfile and add:

gem 'httparty' gem 'dotenv'

Run bundle install, and we're cooking with gas!

Authentication

Unbounce uses OAuth 2.0, so let's set up our authentication:

require 'httparty' require 'dotenv/load' class UnbounceAPI include HTTParty base_uri 'https://api.unbounce.com/v1' def initialize @options = { headers: { 'Authorization' => "Bearer #{ENV['UNBOUNCE_ACCESS_TOKEN']}", 'Content-Type' => 'application/json' } } end end

Don't forget to create a .env file with your UNBOUNCE_ACCESS_TOKEN!

Making API requests

Now for the fun part - let's make some requests:

def get_pages self.class.get('/pages', @options) end

Easy peasy, right? This method will fetch all your Unbounce pages.

Core API operations

Let's add some more muscle to our integration:

def create_page(data) self.class.post('/pages', @options.merge(body: data.to_json)) end def update_page(page_id, data) self.class.put("/pages/#{page_id}", @options.merge(body: data.to_json)) end def get_leads(page_id) self.class.get("/pages/#{page_id}/leads", @options) end

Error handling and rate limiting

Let's be good API citizens and handle errors gracefully:

def make_request(method, endpoint, options = {}) response = self.class.send(method, endpoint, @options.merge(options)) case response.code when 200..299 response when 429 sleep(60) make_request(method, endpoint, options) else raise "API request failed: #{response.code} #{response.message}" end end

This method includes basic retry logic for rate limiting.

Best practices

To keep things speedy, let's implement some basic caching:

def get_pages @pages ||= make_request(:get, '/pages') end

This caches the pages in memory for the lifetime of the object.

Testing the integration

Don't forget to test! Here's a simple RSpec example:

RSpec.describe UnbounceAPI do let(:api) { UnbounceAPI.new } it "fetches pages successfully" do VCR.use_cassette("unbounce_pages") do response = api.get_pages expect(response.code).to eq(200) expect(response.parsed_response).to be_an(Array) end end end

Conclusion

And there you have it! You've just built a solid foundation for integrating with the Unbounce API in Ruby. Remember, this is just the beginning - there's a whole world of possibilities to explore with this integration. Happy coding, and may your conversion rates be ever in your favor!