Hey there, fellow developer! Ready to dive into the world of document automation with PandaDoc? Let's roll up our sleeves and build a slick PHP integration that'll have you creating, sending, and tracking documents like a pro.
PandaDoc's API is a powerhouse for document workflows. Whether you're looking to generate contracts, send proposals, or manage e-signatures, this integration will be your new best friend. We'll walk through the process step-by-step, so you can get up and running in no time.
Before we jump in, make sure you've got:
Got all that? Great! Let's get cooking.
First things first, let's set up our project:
mkdir pandadoc-integration cd pandadoc-integration composer init composer require guzzlehttp/guzzle
PandaDoc uses API key authentication. Let's create a base client class to handle this:
<?php use GuzzleHttp\Client; class PandaDocClient { private $client; private $apiKey; public function __construct($apiKey) { $this->apiKey = $apiKey; $this->client = new Client([ 'base_uri' => 'https://api.pandadoc.com/public/v1/', 'headers' => [ 'Authorization' => 'API-Key ' . $this->apiKey, 'Content-Type' => 'application/json' ] ]); } // We'll add more methods here soon! }
Let's add a method to create a document:
public function createDocument($name, $templateId) { $response = $this->client->post('documents', [ 'json' => [ 'name' => $name, 'template_uuid' => $templateId ] ]); return json_decode($response->getBody(), true); }
Now, let's add those recipients:
public function addRecipients($documentId, $recipients) { $response = $this->client->put("documents/$documentId/recipients", [ 'json' => ['recipients' => $recipients] ]); return json_decode($response->getBody(), true); }
If you're not using a template, you'll need to upload content:
public function uploadContent($documentId, $content) { $response = $this->client->post("documents/$documentId/content", [ 'json' => ['content' => $content] ]); return json_decode($response->getBody(), true); }
Finally, let's send that document:
public function sendDocument($documentId) { $response = $this->client->post("documents/$documentId/send", [ 'json' => ['message' => 'Please review and sign this document.'] ]); return json_decode($response->getBody(), true); }
Always check for errors in the API responses. Here's a quick helper method:
private function handleResponse($response) { $body = json_decode($response->getBody(), true); if ($response->getStatusCode() >= 400) { throw new Exception($body['message'] ?? 'An error occurred'); } return $body; }
Want to know when your document is viewed or signed? Set up a webhook endpoint:
public function handleWebhook($payload) { $event = $payload['event']; $documentId = $payload['data']['id']; switch ($event) { case 'document_state_changed': // Handle document state changes break; case 'recipient_completed': // Handle recipient completion break; // Add more cases as needed } }
Don't forget to test! Here's a quick example:
$client = new PandaDocClient('your-api-key'); $document = $client->createDocument('My Contract', 'template-id'); $client->addRecipients($document['id'], [ ['email' => '[email protected]', 'first_name' => 'John', 'last_name' => 'Doe'] ]); $client->sendDocument($document['id']); echo "Document sent successfully!";
And there you have it! You've just built a robust PandaDoc integration in PHP. With these building blocks, you can create powerful document workflows tailored to your needs. Remember, this is just scratching the surface β PandaDoc's API offers even more features like custom pricing tables, form fields, and more.
Happy coding, and may your documents always be signed on time! πΌπβοΈ
For more details, check out the PandaDoc API documentation. Now go forth and automate those documents!