Back

Step by Step Guide to Building a Seamless AI API Integration in Ruby

Aug 18, 20246 minute read

Introduction

Hey there, fellow Ruby enthusiast! Ready to supercharge your app with some AI goodness? Let's dive into integrating the Seamless AI API into your Ruby project. This powerful API will help you find contacts, enrich lead data, and gather company information like a pro. Buckle up, because we're about to make your Ruby app a whole lot smarter!

Prerequisites

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

  • Ruby 2.7+ installed (come on, you know you want to use the latest version!)
  • Bundler gem (for managing our dependencies like a boss)
  • A Seamless AI API key (grab one from their website if you haven't already)

Setting up the project

Let's kick things off by creating a new Ruby project:

mkdir seamless_ai_integration cd seamless_ai_integration bundle init

Now, let's add the gems we'll need. Open up your Gemfile and add:

gem 'faraday' gem 'json'

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

Configuring the API client

Time to set up our API client. Create a new file called seamless_ai_client.rb:

require 'faraday' require 'json' class SeamlessAIClient BASE_URL = 'https://api.seamless.ai/v1/' def initialize(api_key) @api_key = api_key @conn = Faraday.new(url: BASE_URL) do |faraday| faraday.headers['Authorization'] = "Bearer #{api_key}" faraday.headers['Content-Type'] = 'application/json' faraday.adapter Faraday.default_adapter end end # We'll add more methods here soon! end

Implementing core API functionalities

Now for the fun part! Let's add some methods to search for contacts, get company info, and enrich lead data:

class SeamlessAIClient # ... previous code ... def search_contacts(query) response = @conn.get('search/contacts', q: query) JSON.parse(response.body) end def get_company_info(domain) response = @conn.get('company', domain: domain) JSON.parse(response.body) end def enrich_lead(email) response = @conn.get('lead/enrich', email: email) JSON.parse(response.body) end end

Error handling and rate limiting

Let's add some error handling and respect those rate limits:

class SeamlessAIClient # ... previous code ... def make_request(method, endpoint, params = {}) retries = 0 begin response = @conn.send(method, endpoint, params) handle_rate_limit(response) JSON.parse(response.body) rescue Faraday::ClientError => e retries += 1 retry if retries < 3 raise e end end private def handle_rate_limit(response) if response.status == 429 sleep_time = response.headers['Retry-After'].to_i sleep(sleep_time) end end end

Creating a wrapper class

Let's wrap it all up in a nice, easy-to-use class:

class SeamlessAI def initialize(api_key) @client = SeamlessAIClient.new(api_key) end def find_contacts(query) @client.make_request(:get, 'search/contacts', q: query) end def company_info(domain) @client.make_request(:get, 'company', domain: domain) end def enrich_lead(email) @client.make_request(:get, 'lead/enrich', email: email) end end

Testing the integration

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

require 'rspec' require_relative 'seamless_ai' RSpec.describe SeamlessAI do let(:api_key) { 'your_api_key_here' } let(:seamless_ai) { SeamlessAI.new(api_key) } it 'finds contacts' do VCR.use_cassette('find_contacts') do results = seamless_ai.find_contacts('Ruby developer') expect(results).to be_an(Array) expect(results.first).to have_key('name') end end # Add more tests for other methods end

Best practices and optimization

To take your integration to the next level:

  1. Implement caching to reduce API calls
  2. Use batch processing for multiple requests
  3. Set up proper logging for easier debugging

Conclusion

And there you have it! You've just built a slick Seamless AI integration in Ruby. With this power at your fingertips, you can now find contacts, enrich leads, and gather company info like a champ.

Remember, with great power comes great responsibility. Use this integration wisely, and may your Ruby apps be forever awesome!

Now go forth and code, you magnificent Ruby developer!