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(api_key=os.environ["USGM_API_KEY"]) # never hard-code the key

Usage

1# List mail (auto-paginates)
2for mail in usgm.mails.list(is_read=False):
3 print(mail.id, mail.sender)
4
5# Fetch one item
6mail = usgm.mails.get("80421")
7
8# Mark it read
9usgm.mails.update("80421", is_read=True)
10
11# Create a folder and file the mail into it
12folder = usgm.folders.create(name="Taxes", color="blue")
13usgm.mails.update("80421", folder_id=folder.id)

Async

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

Error handling

1from usgm import UsgmError
2
3try:
4 usgm.mails.get("does-not-exist")
5except UsgmError as err:
6 print(err.code, err.status_code, err.request_id)
7 raise

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