|
| 1 | +--- |
| 2 | +title: Python SDK |
| 3 | +description: Official UseSend Python SDK for sending emails and managing contacts. |
| 4 | +icon: python |
| 5 | +--- |
| 6 | + |
| 7 | +This guide shows how to install and use the official `usesend` Python SDK. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +Install from PyPI: |
| 12 | + |
| 13 | +```bash |
| 14 | +pip install usesend |
| 15 | +``` |
| 16 | + |
| 17 | +## Initialize |
| 18 | + |
| 19 | +```python |
| 20 | +from usesend import UseSend, types |
| 21 | + |
| 22 | + |
| 23 | +# Option A: pass values directly (helpful in scripts/tests) |
| 24 | +client = UseSend("us_xxx") |
| 25 | + |
| 26 | +# Option B: custom base URL (self-hosted) |
| 27 | +client = UseSend("us_xxx", url="https://your-domain.example") |
| 28 | +``` |
| 29 | + |
| 30 | +## Send an email |
| 31 | + |
| 32 | +`EmailCreate` is a TypedDict for editor hints; at runtime you pass a regular dict. The client accepts `from` or `from_` (it normalizes `from_` to `from`). |
| 33 | + |
| 34 | +```python |
| 35 | +from usesend import UseSend, types |
| 36 | + |
| 37 | +client = UseSend("us_xxx") |
| 38 | + |
| 39 | +payload: types.EmailCreate = { |
| 40 | + "to": "user@example.com", |
| 41 | + "from": "no-reply@yourdomain.com", |
| 42 | + "subject": "Welcome", |
| 43 | + "html": "<strong>Hello!</strong>", |
| 44 | +} |
| 45 | + |
| 46 | +data, err = client.emails.send(payload) |
| 47 | +print(data or err) |
| 48 | +``` |
| 49 | + |
| 50 | +Attachments and scheduling: |
| 51 | + |
| 52 | +```python |
| 53 | +from datetime import datetime, timedelta |
| 54 | + |
| 55 | +payload: types.EmailCreate = { |
| 56 | + "to": ["user1@example.com", "user2@example.com"], |
| 57 | + "from": "no-reply@yourdomain.com", |
| 58 | + "subject": "Report", |
| 59 | + "text": "See attached.", |
| 60 | + "attachments": [ |
| 61 | + {"filename": "report.txt", "content": "SGVsbG8gd29ybGQ="}, # base64 |
| 62 | + ], |
| 63 | + "scheduledAt": datetime.utcnow() + timedelta(minutes=10), |
| 64 | +} |
| 65 | +data, err = client.emails.create(payload) |
| 66 | +``` |
| 67 | + |
| 68 | +## Batch send |
| 69 | + |
| 70 | +```python |
| 71 | +items: list[types.EmailBatchItem] = [ |
| 72 | + {"to": "a@example.com", "from": "no-reply@yourdomain.com", "subject": "A", "html": "<p>A</p>"}, |
| 73 | + {"to": "b@example.com", "from": "no-reply@yourdomain.com", "subject": "B", "html": "<p>B</p>"}, |
| 74 | +] |
| 75 | +data, err = client.emails.batch(items) |
| 76 | +``` |
| 77 | + |
| 78 | +## Retrieve and manage emails |
| 79 | + |
| 80 | +Get an email: |
| 81 | + |
| 82 | +```python |
| 83 | +email, err = client.emails.get("email_123") |
| 84 | +``` |
| 85 | + |
| 86 | +Update schedule time: |
| 87 | + |
| 88 | +```python |
| 89 | +from datetime import datetime, timedelta |
| 90 | + |
| 91 | +update: types.EmailUpdate = {"scheduledAt": datetime.utcnow() + timedelta(hours=1)} |
| 92 | +data, err = client.emails.update("email_123", update) |
| 93 | +``` |
| 94 | + |
| 95 | +Cancel a scheduled email: |
| 96 | + |
| 97 | +```python |
| 98 | +data, err = client.emails.cancel("email_123") |
| 99 | +``` |
| 100 | + |
| 101 | +## Contacts |
| 102 | + |
| 103 | +All contact operations require a contact book ID (`book_id`). |
| 104 | + |
| 105 | +Create a contact: |
| 106 | + |
| 107 | +```python |
| 108 | +create: types.ContactCreate = { |
| 109 | + "email": "user@example.com", |
| 110 | + "firstName": "Jane", |
| 111 | + "properties": {"plan": "pro"}, |
| 112 | +} |
| 113 | +data, err = client.contacts.create("book_123", create) |
| 114 | +``` |
| 115 | + |
| 116 | +Get a contact: |
| 117 | + |
| 118 | +```python |
| 119 | +contact, err = client.contacts.get("book_123", "contact_456") |
| 120 | +``` |
| 121 | + |
| 122 | +Update a contact: |
| 123 | + |
| 124 | +```python |
| 125 | +update: types.ContactUpdate = {"subscribed": False} |
| 126 | +data, err = client.contacts.update("book_123", "contact_456", update) |
| 127 | +``` |
| 128 | + |
| 129 | +Upsert a contact: |
| 130 | + |
| 131 | +```python |
| 132 | +upsert: types.ContactUpsert = { |
| 133 | + "email": "user@example.com", |
| 134 | + "firstName": "Jane", |
| 135 | +} |
| 136 | +data, err = client.contacts.upsert("book_123", "contact_456", upsert) |
| 137 | +``` |
| 138 | + |
| 139 | +Delete a contact: |
| 140 | + |
| 141 | +```python |
| 142 | +data, err = client.contacts.delete(book_id="book_123", contact_id="contact_456") |
| 143 | +``` |
| 144 | + |
| 145 | +## Error handling |
| 146 | + |
| 147 | +By default the client raises `UseSendHTTPError` for non-2xx responses. To handle errors as return values, pass `raise_on_error=False`. |
| 148 | + |
| 149 | +```python |
| 150 | +from usesend import UseSend, UseSendHTTPError |
| 151 | + |
| 152 | +# Raises exceptions on errors (default) |
| 153 | +client = UseSend("us_xxx") |
| 154 | +try: |
| 155 | + data, _ = client.emails.get("email_123") |
| 156 | +except UseSendHTTPError as e: |
| 157 | + print("request failed:", e) |
| 158 | + |
| 159 | +# Returns (None, error) instead of raising |
| 160 | +client = UseSend("us_xxx", raise_on_error=False) |
| 161 | +data, err = client.emails.get("email_123") |
| 162 | +if err: |
| 163 | + print("error:", err) |
| 164 | +``` |
0 commit comments