Official Python client for the SendPulse REST API.
- Python ≥ 3.11, single runtime dependency (
httpx) - Thin transport: OAuth / API-key auth, automatic token refresh, typed exceptions
- Chained endpoint surface with IDE autocomplete —
client.crm.deals.getDeal({...}) - Raw
get/post/...fallback for any endpoint - Swappable HTTP client and token storage
- Ships
llms.txtfor AI coding assistants · mypy strict · ruff
pip install pysendpulsefrom sendpulse import Clientclient = Client(api_key="YOUR_API_KEY")client = Client(client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET")Tokens are fetched automatically and stored on disk between requests. A 401 is
retried once with a freshly minted token.
Mapped endpoints are reachable as a chain — service → resource → operation —
with IDE autocomplete for every segment:
campaigns = client.email.campaigns.getCampaigns({"limit": 10})
campaign = client.email.campaigns.getCampaignById({"id": 12345})
deal = client.crm.deals.getDeal({"dealId": 42})
client.smtp.emails.sendSmtpEmail({
"email": {
"subject": "Hello",
"from": {"name": "Me", "email": "[email protected]"},
"to": [{"name": "You", "email": "[email protected]"}],
"html": "<p>Hi!</p>",
},
})You pass one dict. Path params ({id}) and declared query params are routed
automatically; any other key goes to the JSON body (for a GET it becomes a
query param). Every call returns the decoded JSON response (a dict or list;
empty body → {}), or raises one of the exceptions below.
Hyphenated services (
file-manager,live-chat,viber-chatbot,web-push) are not valid attribute names — reach them viagetattr:getattr(client, "file-manager").directory.getDirectoryTree({}).
The full list of chains, paths and parameters lives in
sendpulse/resources/operations/ and is
mirrored in llms.txt.
For any endpoint not in the map — or when you want full control — call a path directly:
client.get("/campaigns", {"limit": 10}) # also query params
client.post("/smtp/emails", {"email": {}}) # put / patch / delete too
# lowest level:
client.request("GET", "/campaigns")
from sendpulse.http.request import Request
client.send(Request(method="GET", uri="/campaigns"))The chained surface is powered by generated stub definitions in
sendpulse/stubs.pyi:
- PyCharm / VS Code (Pylance) index the
.pyiautomatically — no setup. - mypy resolves the chain and flags unknown resources/methods and missing required params. Each operation's HTTP route and parameter list is in its docstring (shown on hover).
from sendpulse import (
Client,
AuthException, # 401 / 403 — wrong credentials or insufficient permissions
RateLimitException, # 429 — too many requests
ApiException, # any other 4xx / 5xx from the API
NetworkException, # connection timeout, DNS failure, etc.
ProtocolException, # response received but could not be parsed (malformed JSON)
)
try:
campaigns = client.email.campaigns.getCampaigns()
except AuthException as e:
print(e.http_status, e.raw_body) # 401 / 403 — credentials problem
except RateLimitException:
... # 429 — back off and retry
except ApiException as e:
print(e.http_status, e.raw_body) # other 4xx / 5xx
except ProtocolException as e:
... # unexpected response format
except NetworkException as e:
... # transport errorAll SDK exceptions subclass sendpulse.SendPulseError, so a single
except SendPulseError covers every SDK error.
client = Client(
api_key="key",
connect_timeout=10.0, # seconds, default
request_timeout=30.0, # seconds, default
)Any object that implements send(request: Request) -> Response is accepted:
from sendpulse.http.request import Request
from sendpulse.http.response import Response
class MyHttpClient:
def send(self, request: Request) -> Response:
...
client = Client(api_key="key", http_client=MyHttpClient())from sendpulse.auth.token_storage import InMemoryTokenStorage
# In-memory (tokens lost on process exit — useful for tests)
client = Client(
client_id="id",
client_secret="secret",
token_storage=InMemoryTokenStorage(),
)Default is FileTokenStorage (system temp directory or custom cache_dir).
Use Client as a context manager to release the underlying HTTP connection:
with Client(api_key="YOUR_API_KEY") as client:
lists = client.email.mailingLists.getMailingLists()| Topic | |
|---|---|
| Authentication & token storage | docs/authentication.md |
| Exception reference | docs/exceptions.md |
| Testing | docs/testing.md |
| FastAPI integration | docs/fastapi.md |
| Architecture & decisions | docs/architecture.md |
| AI assistants | llms.txt |
MIT