Hey there, fellow developer! Ready to dive into the world of Sage Business Cloud API integration? You're in for a treat. This guide will walk you through creating a robust Python integration that'll have you pulling data and pushing updates like a pro. Let's get cracking!
Before we jump in, make sure you've got these basics covered:
requests
library (pip install requests
)First things first, let's get you authenticated:
import requests def get_oauth_token(client_id, client_secret, username, password): url = "https://oauth.accounting.sage.com/token" data = { "grant_type": "password", "client_id": client_id, "client_secret": client_secret, "username": username, "password": password } response = requests.post(url, data=data) return response.json()["access_token"]
Pro tip: Implement token refresh to keep your integration running smoothly!
Let's create a base client to handle our API calls:
class SageClient: def __init__(self, access_token): self.base_url = "https://api.accounting.sage.com/v3.1" self.headers = { "Authorization": f"Bearer {access_token}", "Content-Type": "application/json" } def get(self, endpoint): return requests.get(f"{self.base_url}/{endpoint}", headers=self.headers) def post(self, endpoint, data): return requests.post(f"{self.base_url}/{endpoint}", headers=self.headers, json=data) # Implement put and delete methods similarly
Now, let's put our client to work:
def get_customers(client): response = client.get("contacts") return response.json()["$items"] def create_invoice(client, invoice_data): return client.post("sales_invoices", invoice_data)
Don't forget to catch those pesky errors:
import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) try: # Your API call here except requests.exceptions.RequestException as e: logger.error(f"API request failed: {e}")
Here's a quick example of how to process API data:
def process_customer_data(customers): return [{"name": customer["name"], "email": customer["email"]} for customer in customers]
Let's put it all together:
token = get_oauth_token(client_id, client_secret, username, password) client = SageClient(token) customers = get_customers(client) processed_customers = process_customer_data(customers) new_invoice = { "contact_id": processed_customers[0]["id"], "date": "2023-05-15", "due_date": "2023-06-15", "line_items": [ { "description": "Consulting services", "quantity": 1, "unit_price": 1000 } ] } create_invoice(client, new_invoice)
Don't skip testing! Here's a simple unit test to get you started:
import unittest from unittest.mock import Mock, patch class TestSageIntegration(unittest.TestCase): @patch('requests.get') def test_get_customers(self, mock_get): mock_get.return_value.json.return_value = {"$items": [{"name": "Test Corp"}]} client = SageClient("fake_token") customers = get_customers(client) self.assertEqual(len(customers), 1) self.assertEqual(customers[0]["name"], "Test Corp") if __name__ == '__main__': unittest.main()
And there you have it! You've just built a solid foundation for your Sage Business Cloud API integration. Remember, this is just the beginning. Keep exploring the API docs, and don't be afraid to push the boundaries of what you can do with this integration.
Happy coding, and may your API calls always return 200 OK!