Back

Step by Step Guide to Building a Zoho Campaigns API Integration in Python

Aug 14, 20245 minute read

Introduction

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.

Prerequisites

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

  • A Python environment (3.7+ recommended)
  • A Zoho Campaigns account with API access
  • Your favorite code editor

Got all that? Great! Let's roll.

Authentication

First things first, we need to get cozy with Zoho's OAuth 2.0. Here's the quick rundown:

  1. Head to the Zoho Developer Console and create a new client
  2. Grab your client ID and secret
  3. Implement this nifty token refresh mechanism:
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"]

Setting Up the Project

Let's get our project off the ground:

mkdir zoho_campaigns_integration cd zoho_campaigns_integration pip install requests touch zoho_campaigns.py

Making API Requests

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()

Core Functionalities

Now for the fun part! Let's implement some key features:

Managing Mailing Lists

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)

Creating and Sending Campaigns

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)

Error Handling and Rate Limiting

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

Testing the Integration

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()

Best Practices and Optimization

To keep things running smoothly:

  • Cache your access tokens
  • Use asynchronous operations for bulk requests
  • Keep your code modular and well-documented

Conclusion

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!