Back

Step by Step Guide to Building an Agile CRM API Integration in Ruby

Aug 17, 20245 minute read

Introduction

Hey there, fellow Ruby enthusiast! Ready to supercharge your CRM game? Let's dive into integrating the Agile CRM API into your Ruby project. This powerful combo will have you managing contacts like a pro in no time.

Prerequisites

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

  • A Ruby environment that's good to go
  • An Agile CRM account with API credentials in hand

Got those? Great! Let's roll.

Setting up the project

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

mkdir agile_crm_integration cd agile_crm_integration bundle init

Now, let's add some firepower to our Gemfile:

gem 'httparty' gem 'dotenv'

Run bundle install, and we're locked and loaded.

Authentication

Security first! Let's keep those API credentials safe:

  1. Create a .env file in your project root
  2. Add your credentials:
AGILE_CRM_API_KEY=your_api_key
AGILE_CRM_EMAIL=your_email
AGILE_CRM_DOMAIN=your_domain

Now, let's set up basic authentication:

require 'dotenv/load' require 'httparty' class AgileCRM include HTTParty base_uri "https://#{ENV['AGILE_CRM_DOMAIN']}.agilecrm.com/dev/api" basic_auth ENV['AGILE_CRM_EMAIL'], ENV['AGILE_CRM_API_KEY'] end

Making API requests

Time to flex those API muscles! Let's start with the basics:

GET request: Retrieving contacts

def get_contacts self.class.get('/contacts') end

POST request: Creating a new contact

def create_contact(contact_data) self.class.post('/contacts', body: contact_data.to_json, headers: { 'Content-Type' => 'application/json' }) end

PUT request: Updating an existing contact

def update_contact(id, contact_data) self.class.put("/contacts/#{id}", body: contact_data.to_json, headers: { 'Content-Type' => 'application/json' }) end

DELETE request: Removing a contact

def delete_contact(id) self.class.delete("/contacts/#{id}") end

Handling responses

Let's make sense of what the API's telling us:

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

Building a simple wrapper class

Let's wrap it all up in a neat little package:

class AgileCRM # ... previous code ... def get_contacts handle_response(self.class.get('/contacts')) end def create_contact(contact_data) handle_response(self.class.post('/contacts', body: contact_data.to_json, headers: { 'Content-Type' => 'application/json' })) end def update_contact(id, contact_data) handle_response(self.class.put("/contacts/#{id}", body: contact_data.to_json, headers: { 'Content-Type' => 'application/json' })) end def delete_contact(id) handle_response(self.class.delete("/contacts/#{id}")) end private def handle_response(response) # ... previous handle_response method ... end end

Testing the integration

Let's make sure everything's working as smooth as butter:

require 'minitest/autorun' require 'webmock/minitest' class AgileCRMTest < Minitest::Test def setup @crm = AgileCRM.new end def test_get_contacts stub_request(:get, "https://#{ENV['AGILE_CRM_DOMAIN']}.agilecrm.com/dev/api/contacts") .to_return(status: 200, body: '[{"id": 1, "name": "John Doe"}]') contacts = @crm.get_contacts assert_equal 1, contacts.first['id'] assert_equal 'John Doe', contacts.first['name'] end # Add more tests for create, update, and delete end

Best practices and optimization

Remember, play nice with the API:

  • Implement rate limiting to avoid hitting API limits
  • Cache responses when possible to reduce API calls

Conclusion

And there you have it! You've just built a sleek Agile CRM API integration in Ruby. Pretty cool, right? Now go forth and manage those contacts like a boss!

For more in-depth info, check out the Agile CRM API docs. Happy coding!