Hey there, fellow Ruby enthusiast! Ready to supercharge your outreach game with lemlist? Let's dive into building a slick API integration that'll have you managing campaigns and leads like a pro. We'll keep things snappy and focus on the good stuff – no fluff, just pure Ruby goodness.
Before we jump in, make sure you've got:
First things first, let's get our project off the ground:
mkdir lemlist_integration && cd lemlist_integration bundle init
Now, crack open that Gemfile and add these gems:
gem 'httparty' gem 'dotenv'
Run bundle install
, and we're cooking with gas!
Time to create our lemlist API client. Create a new file called lemlist_client.rb
:
require 'httparty' require 'dotenv/load' class LemlistClient include HTTParty base_uri 'https://api.lemlist.com/api' def initialize @options = { headers: { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{ENV['LEMLIST_API_KEY']}" } } end # We'll add more methods here soon! end
Don't forget to create a .env
file in your project root and add your API key:
LEMLIST_API_KEY=your_api_key_here
Let's add some campaign magic to our client:
def list_campaigns self.class.get('/campaigns', @options) end def create_campaign(name, team_id) body = { name: name, teamId: team_id } self.class.post('/campaigns', @options.merge(body: body.to_json)) end def update_campaign(campaign_id, params) self.class.put("/campaigns/#{campaign_id}", @options.merge(body: params.to_json)) end
Now for some lead management goodness:
def add_lead(campaign_id, lead_data) self.class.post("/campaigns/#{campaign_id}/leads", @options.merge(body: lead_data.to_json)) end def get_lead(campaign_id, lead_id) self.class.get("/campaigns/#{campaign_id}/leads/#{lead_id}", @options) end def update_lead_status(campaign_id, lead_id, status) body = { status: status } self.class.put("/campaigns/#{campaign_id}/leads/#{lead_id}", @options.merge(body: body.to_json)) end
Let's not forget about sequences:
def create_sequence(campaign_id, sequence_data) self.class.post("/campaigns/#{campaign_id}/sequences", @options.merge(body: sequence_data.to_json)) end def add_sequence_step(campaign_id, sequence_id, step_data) self.class.post("/campaigns/#{campaign_id}/sequences/#{sequence_id}/steps", @options.merge(body: step_data.to_json)) end
Let's add some resilience to our client:
def with_retry(max_retries = 3) retries = 0 begin yield rescue StandardError => e retries += 1 if retries <= max_retries sleep(2 ** retries) retry else raise e end end end
Now wrap your API calls with this method:
def list_campaigns with_retry { self.class.get('/campaigns', @options) } end
Time to put our client through its paces. Create a test_client.rb
file:
require_relative 'lemlist_client' require 'minitest/autorun' class TestLemlistClient < Minitest::Test def setup @client = LemlistClient.new end def test_list_campaigns response = @client.list_campaigns assert_equal 200, response.code end # Add more tests for other methods end
Run your tests with ruby test_client.rb
. Watch those green dots roll in!
To keep things speedy, consider implementing some caching:
require 'redis' class LemlistClient # ... existing code ... def initialize # ... existing code ... @cache = Redis.new end def list_campaigns cached = @cache.get('campaigns') return JSON.parse(cached) if cached response = with_retry { self.class.get('/campaigns', @options) } @cache.setex('campaigns', 300, response.body) # Cache for 5 minutes JSON.parse(response.body) end end
And there you have it, folks! A lean, mean lemlist API integration machine. You've got the tools to manage campaigns, leads, and sequences like a boss. Now go forth and conquer those outreach campaigns!
Remember, this is just the beginning. Feel free to expand on this integration, add more features, and make it your own. Happy coding, and may your open rates be ever in your favor!