PHP code example of mcore-services / teamleader-sdk
1. Go to this page and download the library: Download mcore-services/teamleader-sdk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mcore-services / teamleader-sdk example snippets
// routes/web.php
use Illuminate\Http\Request;
use McoreServices\TeamleaderSDK\Facades\Teamleader;
Route::get('/teamleader/connect', function () {
return Teamleader::authorize();
});
Route::get('/teamleader/callback', function (Request $request) {
if (Teamleader::handleCallback($request->query('code'), $request->query('state'))) {
return response('Connected to Teamleader. You can close this tab and run: php artisan teamleader:status');
}
return response('Connecting to Teamleader failed. Check storage/logs/laravel.log.', 500);
});
use McoreServices\TeamleaderSDK\Facades\Teamleader;
// 1. Redirect the user to Teamleader — a state is generated and remembered
return Teamleader::authorize();
// 2. Handle the callback — the state is checked, tokens are encrypted and stored
Teamleader::handleCallback($code, $state);
// 3. Check authentication status
if (Teamleader::isAuthenticated()) {
// Ready to make API calls
}
// Requests the upload link and sends the file to it
Teamleader::files()->uploadFile(storage_path('contract.pdf'), 'deal', 'deal-uuid', 'Contracts');
$files = Teamleader::files()->forDeal('deal-uuid');
$offers = Teamleader::files()->forDeal('deal-uuid', ['filters' => ['term' => 'offerte']]);
$some = Teamleader::files()->byIds(['file-uuid-1', 'file-uuid-2']);
// Downloads: the bytes, or straight onto a Laravel disk
$pdf = Teamleader::invoices()->downloadContents('invoice-uuid');
Teamleader::invoices()->downloadTo('invoice-uuid', 's3', 'invoices/2026-0042.pdf');
$field = Teamleader::customFields()->create([
'label' => 'VAT Number',
'type' => 'single_line',
'context' => 'company',
]);
// Every definition, paging handled for you
$all = Teamleader::customFields()->all();
// A select field takes the option label, not the option id
$value = Teamleader::customFields()->selectValue('field-uuid', 'option-uuid-or-label');
Teamleader::companies()
->lazy(['status' => 'active'])
->each(function (array $company) {
// one record at a time
});
Teamleader::deals()->lazy(['status' => 'open'])->take(10)->all(); // one request
// Any list to a file
Teamleader::bulk()
->export('contacts', ['tags' => ['customer']])
->toCsv(storage_path('contacts.csv'), ['id', 'first_name', 'last_name', 'emails.0.email']);
// Many writes: every row validated before the first is sent
$result = Teamleader::bulk()
->create('companies', $rows)
->continueOnError()
->run();
$result->succeeded(); // [row key => API response]
$result->failed(); // [row key => BulkFailure]
// Or on the queue, in chunks
Teamleader::bulk()->update('deals', $rows)->dispatch(chunk: 50);
use Illuminate\Support\Facades\Event;
use McoreServices\TeamleaderSDK\Events\TokenRefreshFailed;
Event::listen(function (TokenRefreshFailed $event) {
if ($event->reauthorizationRequired) {
// tell someone to reconnect $event->connection
}
});
Teamleader::webhooks()->register('https://your-app.com/webhooks/teamleader', [
'invoice.booked',
'invoice.peppolSubmissionSucceeded',
'invoice.peppolSubmissionFailed',
'deal.won',
]);
// Or a whole category at once
$types = Teamleader::webhooks()->getInvoiceEventTypes();
Teamleader::webhooks()->register('https://your-app.com/webhooks/teamleader', $types);
use McoreServices\TeamleaderSDK\Exceptions\AuthenticationException;
use McoreServices\TeamleaderSDK\Exceptions\NotFoundException;
use McoreServices\TeamleaderSDK\Exceptions\RateLimitExceededException;
use McoreServices\TeamleaderSDK\Exceptions\TeamleaderException;
use McoreServices\TeamleaderSDK\Exceptions\ValidationException;
try {
$company = Teamleader::companies()->info('company-uuid');
} catch (NotFoundException $e) {
// 404 — the record does not exist
} catch (ValidationException $e) {
// 422 — the API rejected the payload
} catch (RateLimitExceededException $e) {
// 429 — always thrown, never swallowed
$this->release($e->getRetryAfter());
} catch (AuthenticationException $e) {
// Includes ConnectionNeedsReauthorizationException — connect again
return redirect('/teamleader/connect');
} catch (TeamleaderException $e) {
logger()->error('Teamleader API error', ['message' => $e->getMessage()]);
}