PHP SDK

A fully-typed client for modern PHP.

Install

$composer require usgm/sdk

Requires PHP 8.1+.

Initialize

1<?php
2
3use Usgm\UsglobalmailClient;
4
5$usgm = new UsglobalmailClient(getenv('USGM_API_KEY')); // never hard-code the key

Usage

1<?php
2
3use Usgm\Mails\Requests\MailsListRequest;
4use Usgm\Folders\Requests\CreateFolderDto;
5
6// Your account profile
7$account = $usgm->account->get();
8echo $account->email . PHP_EOL;
9
10// List mail, one page at a time (cursor pagination)
11$cursor = null;
12do {
13 $page = $usgm->mails->list(new MailsListRequest(cursor: $cursor));
14 foreach ($page->data as $mail) {
15 echo $mail->id . ' ' . $mail->senderName . PHP_EOL;
16 }
17 $cursor = $page->nextCursor;
18} while ($cursor !== null);
19
20// Fetch one item
21$mail = $usgm->mails->get('80421');
22
23// Create a folder
24$folder = $usgm->folders->create(new CreateFolderDto(name: 'Taxes'));

Error handling

1<?php
2
3use Usgm\Exceptions\UsglobalmailApiException;
4
5try {
6 $usgm->mails->get('does-not-exist');
7} catch (UsglobalmailApiException $err) {
8 // getBody() is the RFC 9457 problem+json payload (code, request_id, detail, …)
9 error_log($err->getCode() . ' ' . json_encode($err->getBody()));
10 throw $err;
11}

Transient 429 and 5xx responses are retried automatically with exponential backoff. See Errors for the full problem+json contract.