Back

Step by Step Guide to Building a Givebutter API Integration in PHP

Aug 15, 20246 minute read

Hey there, fellow developer! Ready to dive into the world of Givebutter API integration? Let's roll up our sleeves and get coding!

Introduction

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.

Prerequisites

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

  • A PHP environment up and running (PHP 7.4+ recommended)
  • A Givebutter account with API access (if you don't have one, go grab it!)

Setting Up the Project

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.

Authentication

Alright, time to get that API key:

  1. Log into your Givebutter account
  2. Head to Settings > API
  3. Generate a new 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' ] ]); } }

Making API Requests

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); }

Implementing Key Features

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

Webhooks

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 }

Error Handling and Logging

Always expect the unexpected:

try { $campaigns = $api->getCampaigns(); } catch (\GuzzleHttp\Exception\RequestException $e) { error_log('API request failed: ' . $e->getMessage()); }

Testing

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); }

Best Practices

  • Respect rate limits (check the X-RateLimit-* headers)
  • Store the API key securely (use environment variables!)
  • Validate input data before sending to the API

Conclusion

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!

Resources

Happy coding, and may your donations be plentiful! 🎉💻🚀