Hey there, fellow developer! Ready to dive into the world of Givebutter API integration? Let's roll up our sleeves and get coding!
Givebutter's API is a powerful tool that lets you tap into their fundraising platform. Whether you're looking to create custom donation experiences or automate your fundraising processes, this guide will walk you through building a solid integration in PHP.
Before we jump in, make sure you've got:
First things first, let's get our project structure in place:
composer require guzzlehttp/guzzle
This will install Guzzle, our HTTP client of choice. Now, create a GivebuttonAPI.php
file - this'll be our main class.
Alright, time to get that API key:
Now, let's use it in our code:
class GivebuttonAPI { private $apiKey; private $client; public function __construct($apiKey) { $this->apiKey = $apiKey; $this->client = new \GuzzleHttp\Client([ 'base_uri' => 'https://api.givebutter.com/v1/', 'headers' => [ 'Authorization' => 'Bearer ' . $this->apiKey, 'Content-Type' => 'application/json' ] ]); } }
Let's add some methods to our class:
public function getCampaigns() { $response = $this->client->get('campaigns'); return json_decode($response->getBody(), true); } public function createDonation($campaignId, $data) { $response = $this->client->post("campaigns/{$campaignId}/donations", [ 'json' => $data ]); return json_decode($response->getBody(), true); }
Now you can use these methods like so:
$api = new GivebuttonAPI('your-api-key'); // List campaigns $campaigns = $api->getCampaigns(); // Create a donation $donationData = [ 'amount' => 1000, // $10.00 'donor' => [ 'first_name' => 'John', 'last_name' => 'Doe', 'email' => '[email protected]' ] ]; $donation = $api->createDonation('campaign-id', $donationData);
Givebutter can send you real-time updates. Set up an endpoint in your application:
// webhook.php $payload = file_get_contents('php://input'); $event = json_decode($payload, true); switch($event['type']) { case 'donation.succeeded': // Handle successful donation break; // Handle other event types }
Always expect the unexpected:
try { $campaigns = $api->getCampaigns(); } catch (\GuzzleHttp\Exception\RequestException $e) { error_log('API request failed: ' . $e->getMessage()); }
Don't forget to test! Here's a quick PHPUnit example:
public function testGetCampaigns() { $api = new GivebuttonAPI('test-api-key'); $campaigns = $api->getCampaigns(); $this->assertIsArray($campaigns); }
X-RateLimit-*
headers)And there you have it! You've just built a solid foundation for your Givebutter API integration. Remember, this is just the beginning - there's so much more you can do with the API. Keep exploring, keep coding, and most importantly, keep making a difference with your fundraising efforts!
Happy coding, and may your donations be plentiful! 🎉💻🚀