Hey there, fellow developer! Ready to dive into the world of WhatConverts API integration? You're in for a treat. This guide will walk you through the process of building a robust integration using Python. WhatConverts API is a powerful tool for tracking and analyzing lead data, and with this integration, you'll be able to harness its full potential in your projects.
Before we jump in, make sure you've got these basics covered:
requests
library installed (pip install requests
)Let's kick things off by setting up our connection:
import requests import json API_KEY = 'your_api_key_here' BASE_URL = 'https://app.whatconverts.com/api/v1' headers = { 'Authorization': f'Bearer {API_KEY}', 'Content-Type': 'application/json' }
Now, let's make our first API call:
def get_leads(): endpoint = f'{BASE_URL}/leads' response = requests.get(endpoint, headers=headers) return response.json() leads = get_leads() print(json.dumps(leads, indent=2))
Easy peasy, right? This will fetch your leads and print them out nicely.
Let's step it up a notch. Here's how you can filter and paginate your results:
def get_filtered_leads(start_date, end_date, page=1, per_page=100): endpoint = f'{BASE_URL}/leads' params = { 'start_date': start_date, 'end_date': end_date, 'page': page, 'per_page': per_page } response = requests.get(endpoint, headers=headers, params=params) return response.json() filtered_leads = get_filtered_leads('2023-01-01', '2023-12-31')
Always be prepared for things to go sideways. Here's a simple way to handle errors:
def make_api_request(endpoint, params=None): try: response = requests.get(f'{BASE_URL}/{endpoint}', headers=headers, params=params) response.raise_for_status() return response.json() except requests.exceptions.RequestException as e: print(f"An error occurred: {e}") return None
Ready for some advanced stuff? Let's set up a webhook listener:
from flask import Flask, request app = Flask(__name__) @app.route('/webhook', methods=['POST']) def webhook(): data = request.json # Process the webhook data here print(f"Received webhook: {data}") return '', 200 if __name__ == '__main__': app.run(port=5000)
Got your data? Great! Let's do something cool with it:
import pandas as pd def analyze_leads(leads): df = pd.DataFrame(leads) # Perform your analysis here return df.groupby('source').agg({'id': 'count', 'value': 'sum'}) lead_analysis = analyze_leads(filtered_leads['leads']) print(lead_analysis)
Here's a script that ties everything together:
import requests import json from datetime import datetime, timedelta API_KEY = 'your_api_key_here' BASE_URL = 'https://app.whatconverts.com/api/v1' headers = { 'Authorization': f'Bearer {API_KEY}', 'Content-Type': 'application/json' } def get_leads(start_date, end_date): endpoint = f'{BASE_URL}/leads' params = { 'start_date': start_date, 'end_date': end_date, 'per_page': 1000 } all_leads = [] page = 1 while True: params['page'] = page response = requests.get(endpoint, headers=headers, params=params) data = response.json() if not data['leads']: break all_leads.extend(data['leads']) page += 1 return all_leads # Get leads for the last 30 days end_date = datetime.now().strftime('%Y-%m-%d') start_date = (datetime.now() - timedelta(days=30)).strftime('%Y-%m-%d') leads = get_leads(start_date, end_date) print(f"Total leads: {len(leads)}") # Do something with the leads...
And there you have it! You've just built a solid WhatConverts API integration in Python. From basic requests to advanced data analysis, you're now equipped to make the most of your lead data.
Remember, this is just the beginning. The WhatConverts API has a lot more to offer, so don't be afraid to explore and experiment. Happy coding!