Hey there, fellow developer! Ready to supercharge your marketing efforts with Zoho Campaigns? Let's dive into building a Python integration that'll have you managing campaigns like a pro in no time.
Before we jump in, make sure you've got:
Got all that? Great! Let's roll.
First things first, we need to get cozy with Zoho's OAuth 2.0. Here's the quick rundown:
import requests def refresh_token(client_id, client_secret, refresh_token): url = "https://accounts.zoho.com/oauth/v2/token" data = { "grant_type": "refresh_token", "client_id": client_id, "client_secret": client_secret, "refresh_token": refresh_token } response = requests.post(url, data=data) return response.json()["access_token"]
Let's get our project off the ground:
mkdir zoho_campaigns_integration cd zoho_campaigns_integration pip install requests touch zoho_campaigns.py
Time to start talking to Zoho! Here's a handy function to get you started:
import requests BASE_URL = "https://campaigns.zoho.com/api/v1.1" def make_request(endpoint, method="GET", data=None, headers=None): url = f"{BASE_URL}/{endpoint}" response = requests.request(method, url, json=data, headers=headers) response.raise_for_status() return response.json()
Now for the fun part! Let's implement some key features:
def get_mailing_lists(access_token): headers = {"Authorization": f"Zoho-oauthtoken {access_token}"} return make_request("getmailinglists", headers=headers) def add_subscriber(access_token, list_key, email): headers = {"Authorization": f"Zoho-oauthtoken {access_token}"} data = {"email": email} return make_request(f"listsubscribe&resfmt=JSON&listkey={list_key}", method="POST", data=data, headers=headers)
def create_campaign(access_token, campaign_data): headers = {"Authorization": f"Zoho-oauthtoken {access_token}"} return make_request("createcampaign", method="POST", data=campaign_data, headers=headers) def send_campaign(access_token, campaign_key): headers = {"Authorization": f"Zoho-oauthtoken {access_token}"} return make_request(f"sendcampaign&campaignkey={campaign_key}", method="POST", headers=headers)
Don't forget to play nice with the API:
import time def rate_limited_request(func): def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except requests.exceptions.HTTPError as e: if e.response.status_code == 429: retry_after = int(e.response.headers.get("Retry-After", 60)) print(f"Rate limit hit. Retrying after {retry_after} seconds.") time.sleep(retry_after) return func(*args, **kwargs) raise return wrapper
Always test your code! Here's a quick example:
import unittest class TestZohoCampaignsIntegration(unittest.TestCase): def setUp(self): self.access_token = "your_access_token_here" def test_get_mailing_lists(self): result = get_mailing_lists(self.access_token) self.assertIn("list_of_lists", result) if __name__ == "__main__": unittest.main()
To keep things running smoothly:
And there you have it! You're now equipped to harness the power of Zoho Campaigns through Python. Remember, this is just the beginning – there's a whole world of possibilities to explore in the Zoho Campaigns API.
Happy coding, and may your open rates be ever in your favor!