Hey there, fellow developer! Ready to supercharge your Python project with some AI goodness? Let's dive into integrating the Seamless AI API into your workflow. This powerful tool will help you access a wealth of contact and company information, making your data-driven tasks a breeze.
Before we jump in, make sure you've got:
Let's kick things off by creating a new Python file and importing the essentials:
import requests import json from dotenv import load_dotenv import os load_dotenv() # This will load our API key from a .env file
Security first! We'll store our API key in a .env
file and load it like this:
API_KEY = os.getenv('SEAMLESS_AI_API_KEY') headers = { 'Authorization': f'Bearer {API_KEY}', 'Content-Type': 'application/json' }
Now for the fun part! Let's write a function to make API calls:
def make_api_request(endpoint, params=None): base_url = 'https://api.seamless.ai/v1/' url = base_url + endpoint try: response = requests.get(url, headers=headers, params=params) response.raise_for_status() return response.json() except requests.exceptions.RequestException as e: print(f"Oops! API request failed: {e}") return None
The API returns JSON, so let's parse it and extract what we need:
def parse_contact_data(response): if not response or 'data' not in response: return [] contacts = response['data'] return [{'name': c['name'], 'email': c['email'], 'company': c['company']} for c in contacts]
Let's create functions for searching contacts and getting company info:
def search_contacts(query, limit=10): endpoint = 'search' params = {'q': query, 'limit': limit} response = make_api_request(endpoint, params) return parse_contact_data(response) def get_company_info(company_name): endpoint = 'company' params = {'name': company_name} return make_api_request(endpoint, params)
To play nice with the API, let's add some basic rate limiting:
import time def rate_limited_api_call(func): last_called = 0 def wrapper(*args, **kwargs): nonlocal last_called elapsed = time.time() - last_called if elapsed < 1: # Ensure at least 1 second between calls time.sleep(1 - elapsed) result = func(*args, **kwargs) last_called = time.time() return result return wrapper search_contacts = rate_limited_api_call(search_contacts) get_company_info = rate_limited_api_call(get_company_info)
Let's add some logging to keep track of what's happening:
import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def safe_api_call(func): def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except Exception as e: logger.error(f"Error in {func.__name__}: {str(e)}") return None return wrapper search_contacts = safe_api_call(search_contacts) get_company_info = safe_api_call(get_company_info)
Time to put our code to the test:
if __name__ == "__main__": # Test contact search results = search_contacts("Python developer", limit=5) print("Search Results:", json.dumps(results, indent=2)) # Test company info retrieval company_info = get_company_info("Seamless AI") print("Company Info:", json.dumps(company_info, indent=2))
And there you have it! You've just built a robust Seamless AI API integration in Python. With this foundation, you can now expand on these functionalities to suit your specific needs. Remember to check the official Seamless AI documentation for more advanced features and always keep an eye on your API usage.
Happy coding, and may your data always flow seamlessly!