Hey there, fellow developer! Ready to dive into the world of Givebutter API integration? You're in for a treat. We'll be walking through the process of building a robust integration that'll have you managing campaigns, donations, and supporters like a pro. Let's get cracking!
Before we jump in, make sure you've got:
requests
libraryFirst things first, let's get our environment ready:
pip install requests
Now, let's set up our API credentials:
import os API_KEY = os.environ.get('GIVEBUTTER_API_KEY') BASE_URL = 'https://api.givebutter.com/v1'
Time to test the waters:
import requests headers = { 'Authorization': f'Bearer {API_KEY}', 'Content-Type': 'application/json' } response = requests.get(f'{BASE_URL}/campaigns', headers=headers) print(response.json())
If you see campaign data, you're golden!
Let's tackle some key operations:
def get_campaign(campaign_id): response = requests.get(f'{BASE_URL}/campaigns/{campaign_id}', headers=headers) return response.json()
def create_donation(campaign_id, amount, supporter_info): payload = { 'amount': amount, 'supporter': supporter_info } response = requests.post(f'{BASE_URL}/campaigns/{campaign_id}/donations', json=payload, headers=headers) return response.json()
def get_supporter(supporter_id): response = requests.get(f'{BASE_URL}/supporters/{supporter_id}', headers=headers) return response.json()
Webhooks are your friends. Here's a quick Flask endpoint to handle them:
from flask import Flask, request app = Flask(__name__) @app.route('/webhook', methods=['POST']) def handle_webhook(): event = request.json # Process the event return '', 200
Always be prepared:
def make_request(url, method='GET', payload=None): try: response = requests.request(method, url, headers=headers, json=payload) response.raise_for_status() return response.json() except requests.exceptions.HTTPError as e: if e.response.status_code == 429: # Handle rate limiting return None # Handle other errors return None
Keep it simple:
import json def save_to_file(data, filename): with open(filename, 'w') as f: json.dump(data, f) def load_from_file(filename): with open(filename, 'r') as f: return json.load(f)
Let's create a donation summary report:
def generate_donation_report(campaign_id): campaign = get_campaign(campaign_id) donations = make_request(f'{BASE_URL}/campaigns/{campaign_id}/donations') total_amount = sum(d['amount'] for d in donations) donor_count = len(set(d['supporter']['id'] for d in donations)) report = { 'campaign_name': campaign['name'], 'total_donations': total_amount, 'donor_count': donor_count } save_to_file(report, f'report_{campaign_id}.json') return report
Don't forget to test your code:
import unittest class TestGivebutterIntegration(unittest.TestCase): def test_get_campaign(self): campaign = get_campaign('test_campaign_id') self.assertIsNotNone(campaign) self.assertIn('name', campaign) if __name__ == '__main__': unittest.main()
And there you have it! You've just built a solid Givebutter API integration. Remember, this is just the beginning - there's so much more you can do. Keep exploring the API docs, and happy coding!
For more info, check out the Givebutter API documentation. Now go forth and create something awesome!