Python SDK

A fully-typed client with sync and async support.

Install

$pip install usgm

Requires Python 3.8+.

Initialize

1import os
2from usgm import Usgm
3
4usgm = Usgm(token=os.environ["USGM_API_KEY"]) # never hard-code the key

Usage

1# Your account profile
2account = usgm.account.get()
3print(account.email, account.status)
4
5# List mail, one page at a time (cursor pagination)
6cursor = None
7while True:
8 page = usgm.mails.list(cursor=cursor)
9 for mail in page.data:
10 print(mail.id, mail.sender_name)
11 cursor = page.next_cursor
12 if cursor is None:
13 break
14
15# Fetch one item
16mail = usgm.mails.get(id="80421")
17
18# Create a folder
19folder = usgm.folders.create(name="Taxes")

Async

1import asyncio
2from usgm import AsyncUsgm
3
4usgm = AsyncUsgm(token=os.environ["USGM_API_KEY"])
5
6async def main():
7 page = await usgm.mails.list()
8 for mail in page.data:
9 print(mail.id)
10
11asyncio.run(main())

Error handling

1from usgm.core.api_error import ApiError
2
3try:
4 usgm.mails.get(id="does-not-exist")
5except ApiError as err:
6 # `body` is the RFC 9457 problem+json payload (code, request_id, detail, …)
7 print(err.status_code, err.body)
8 raise

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