<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
chrisjohnleah / sage-business-cloud-accounting-api example snippets
use ChrisJohnLeah\SageAccounting\Auth\ArrayTokenStore;
use ChrisJohnLeah\SageAccounting\Sage;
use ChrisJohnLeah\SageAccounting\SageConnector;
$connector = new SageConnector(
clientId: getenv('SAGE_CLIENT_ID'),
clientSecret: getenv('SAGE_CLIENT_SECRET'),
redirectUri: 'https://your-app.test/oauth/sage/callback',
scopes: ['readonly'], // or ['full_access']
);
// Bring your own persistence (DB, cache…) by implementing TokenStore.
// ArrayTokenStore keeps tokens in memory — handy for scripts and tests.
$sage = new Sage($connector, new ArrayTokenStore);
$url = $sage->authorizationUrl(); // redirect the user here
$state = $sage->generatedState(); // persist this (session/cache) for the callback
$sage->exchangeCode($_GET['code'], $_GET['state'], $expectedState: $state);
$sage->resolveBusiness(); // selects + remembers the business to target
// Every supplier bill updated since a timestamp — typed, across all pages.
$bills = $sage->purchaseInvoices()->list([
'updated_or_created_since' => '2026-05-01T00:00:00Z',
]);
foreach ($bills as $invoice) {
printf(
"%s owes %.2f, due %s [%s]\n",
$invoice->contactName,
$invoice->outstandingAmount ?? 0.0,
$invoice->dueDate?->format('Y-m-d') ?? 'n/a',
$invoice->status?->displayedAs ?? 'unknown',
);
}
// Suppliers
foreach ($sage->contacts()->list(['contact_type_id' => 'VENDOR']) as $contact) {
echo $contact->name, ' <', $contact->email, ">\n";
}
use ChrisJohnLeah\SageAccounting\Auth\StoredToken;
use ChrisJohnLeah\SageAccounting\Contracts\TokenStore;
final class MyTokenStore implements TokenStore
{
public function get(): ?StoredToken { /* load access/refresh/expiresAt/businessId */ }
public function put(StoredToken $token): void { /* overwrite */ }
public function forget(): void { /* delete */ }
}
use ChrisJohnLeah\SageAccounting\Requests\LedgerAccounts\GetLedgerAccounts;
use ChrisJohnLeah\SageAccounting\Requests\Contacts\PostContacts;
$ledgers = $sage->connector()->send(new GetLedgerAccounts(['attributes' => 'all']))->dto();
$created = $sage->connector()->send(new PostContacts(['contact' => [/* ... */]]))->dto();