Back

Step by Step Guide to Building a Landingi API Integration in Ruby

Aug 18, 20244 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your Ruby project with Landingi's powerful API? You're in the right place. We're going to walk through building a sleek Landingi API integration that'll have you managing landing pages like a pro in no time.

Prerequisites

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

  • Ruby 2.7+ installed
  • The httparty gem (we'll use this for API requests)
  • Your Landingi API credentials (if you don't have these yet, hop over to your Landingi account and grab them)

Setting Up the Project

Let's kick things off by setting up our project:

mkdir landingi_integration cd landingi_integration bundle init

Now, open up your Gemfile and add:

gem 'httparty'

Run bundle install, and we're ready to rock!

Authentication

Landingi uses API key authentication. Here's how to implement it:

require 'httparty' class LandingiAPI include HTTParty base_uri 'https://api.landingi.com/v1' def initialize(api_key) @options = { headers: { 'X-Api-Key' => api_key } } end end

Making API Requests

Now that we're authenticated, let's make some requests:

def get_landing_pages self.class.get('/landing_pages', @options) end

Core API Functionalities

Let's implement some core functionalities:

def create_landing_page(name, template_id) self.class.post('/landing_pages', @options.merge(body: { name: name, template_id: template_id })) end def update_landing_page(id, params) self.class.put("/landing_pages/#{id}", @options.merge(body: params)) end def delete_landing_page(id) self.class.delete("/landing_pages/#{id}", @options) end

Working with Forms

Forms are crucial for landing pages. Here's how to interact with them:

def get_form_submissions(form_id) self.class.get("/forms/#{form_id}/submissions", @options) end def submit_form_entry(form_id, data) self.class.post("/forms/#{form_id}/submissions", @options.merge(body: data)) end

Error Handling and Best Practices

Always handle potential errors and respect rate limits:

def make_request response = yield raise "API Error: #{response.code}" unless response.success? response rescue => e puts "Request failed: #{e.message}" sleep 1 # Simple rate limiting retry end

Testing the Integration

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

RSpec.describe LandingiAPI do let(:api) { LandingiAPI.new('your_api_key') } it 'fetches landing pages' do VCR.use_cassette('landing_pages') do response = api.get_landing_pages expect(response).to be_success end end end

Conclusion

And there you have it! You've just built a robust Landingi API integration in Ruby. With this foundation, you can expand and customize to your heart's content. Remember, the API documentation is your best friend for exploring more endpoints and features.

Happy coding, and may your landing pages convert like crazy! 🚀