Back

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

Aug 15, 20246 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your Ruby app with Insightly's powerful CRM capabilities? You're in the right place. We're going to walk through building an Insightly API integration that'll have you managing contacts, leads, and opportunities like a pro. Let's dive in!

Prerequisites

Before we get our hands dirty, make sure you've got:

  • A Ruby environment set up (I know you've got this!)
  • An Insightly API key (grab one from your Insightly account settings)

Setting up the project

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

mkdir insightly_integration cd insightly_integration bundle init

Now, crack open that Gemfile and add:

gem 'httparty' gem 'json'

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

Authentication

Insightly uses basic authentication. Let's set it up:

require 'httparty' require 'json' API_KEY = 'your_api_key_here' BASE_URL = 'https://api.insightly.com/v3.1' def api_headers { 'Authorization' => "Basic #{Base64.encode64(API_KEY)}", 'Content-Type' => 'application/json' } end

Making API requests

Now for the fun part - let's start making some requests!

GET request

def get_contacts response = HTTParty.get("#{BASE_URL}/Contacts", headers: api_headers) JSON.parse(response.body) end

POST request

def create_lead(lead_data) response = HTTParty.post("#{BASE_URL}/Leads", headers: api_headers, body: lead_data.to_json ) JSON.parse(response.body) end

PUT request

def update_opportunity(id, opportunity_data) response = HTTParty.put("#{BASE_URL}/Opportunities/#{id}", headers: api_headers, body: opportunity_data.to_json ) JSON.parse(response.body) end

DELETE request

def delete_task(id) response = HTTParty.delete("#{BASE_URL}/Tasks/#{id}", headers: api_headers) response.code == 202 end

Handling responses

Always expect the unexpected! Let's add some error handling:

def handle_response(response) case response.code when 200..299 JSON.parse(response.body) else raise "API request failed: #{response.code} - #{response.body}" end end

Building reusable methods

Let's wrap things up nicely with a class:

class InsightlyClient # ... include all the methods we've created so far ... def get(endpoint) handle_response(HTTParty.get("#{BASE_URL}/#{endpoint}", headers: api_headers)) end def post(endpoint, data) handle_response(HTTParty.post("#{BASE_URL}/#{endpoint}", headers: api_headers, body: data.to_json )) end # ... add put and delete methods similarly ... end

Implementing specific use cases

Now you can easily implement specific features:

client = InsightlyClient.new # Sync contacts def sync_contacts(external_contacts) insightly_contacts = client.get_contacts # ... implement syncing logic ... end # Automate lead creation def create_lead_from_form(form_data) lead_data = { FIRST_NAME: form_data[:name], EMAIL: form_data[:email], # ... map other fields ... } client.create_lead(lead_data) end

Best practices

Remember to:

  • Respect Insightly's rate limits
  • Batch your requests when dealing with large datasets
  • Cache responses when appropriate to reduce API calls

Testing the integration

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

RSpec.describe InsightlyClient do let(:client) { InsightlyClient.new } describe '#get_contacts' do it 'returns a list of contacts' do VCR.use_cassette('get_contacts') do contacts = client.get_contacts expect(contacts).to be_an(Array) expect(contacts.first).to have_key('CONTACT_ID') end end end end

Conclusion

And there you have it! You've just built a solid foundation for your Insightly API integration in Ruby. Remember, this is just the beginning - there's so much more you can do with Insightly's API. Keep exploring, keep coding, and most importantly, have fun with it!

For more details, check out the Insightly API documentation. Now go forth and integrate!