Skip to main content

Python SDK for the unsent API

Project description

unsent Python SDK

Prerequisites

Installation

pip

pip install unsent

poetry

poetry add unsent

Usage

Basic Setup

from unsent import unsent

client = unsent("us_12345")

Environment Variables

You can also set your API key using environment variables:

# Set UNSENT_API_KEY or UNSENT_API_KEY in your environment
# Then initialize without passing the key
client = unsent()

Sending Emails

Simple Email

data, error = client.emails.send({
    "to": "hello@acme.com",
    "from": "hello@company.com",
    "subject": "unsent email",
    "html": "<p>unsent is the best open source product to send emails</p>",
    "text": "unsent is the best open source product to send emails",
})

if error:
    print(f"Error: {error}")
else:
    print(f"Email sent! ID: {data['id']}")

Email with Attachments

data, error = client.emails.send({
    "to": "hello@acme.com",
    "from": "hello@company.com",
    "subject": "Email with attachment",
    "html": "<p>Please find the attachment below</p>",
    "attachments": [
        {
            "filename": "document.pdf",
            "content": "base64-encoded-content-here",
        }
    ],
})

Scheduled Email

from datetime import datetime, timedelta

# Schedule email for 1 hour from now
scheduled_time = datetime.now() + timedelta(hours=1)

data, error = client.emails.send({
    "to": "hello@acme.com",
    "from": "hello@company.com",
    "subject": "Scheduled email",
    "html": "<p>This email was scheduled</p>",
    "scheduledAt": scheduled_time,
})

Batch Emails

emails = [
    {
        "to": "user1@example.com",
        "from": "hello@company.com",
        "subject": "Hello User 1",
        "html": "<p>Welcome User 1</p>",
    },
    {
        "to": "user2@example.com",
        "from": "hello@company.com",
        "subject": "Hello User 2",
        "html": "<p>Welcome User 2</p>",
    },
]

data, error = client.emails.batch(emails)

if error:
    print(f"Error: {error}")
else:
    print(f"Sent {len(data['emails'])} emails")

Managing Emails

Get Email Details

data, error = client.emails.get("email_id")

if error:
    print(f"Error: {error}")
else:
    print(f"Email status: {data['status']}")

Update Email

data, error = client.emails.update("email_id", {
    "subject": "Updated subject",
    "html": "<p>Updated content</p>",
})

Cancel Scheduled Email

data, error = client.emails.cancel("email_id")

if error:
    print(f"Error: {error}")
else:
    print("Email cancelled successfully")

Managing Contacts

Create Contact

data, error = client.contacts.create("contact_book_id", {
    "email": "user@example.com",
    "firstName": "John",
    "lastName": "Doe",
    "metadata": {
        "company": "Acme Inc",
        "role": "Developer"
    }
})

Get Contact

data, error = client.contacts.get("contact_book_id", "contact_id")

Update Contact

data, error = client.contacts.update("contact_book_id", "contact_id", {
    "firstName": "Jane",
    "metadata": {
        "role": "Senior Developer"
    }
})

Upsert Contact

# Creates if doesn't exist, updates if exists
data, error = client.contacts.upsert("contact_book_id", "contact_id", {
    "email": "user@example.com",
    "firstName": "John",
    "lastName": "Doe",
})

Delete Contact

data, error = client.contacts.delete(
    book_id="contact_book_id",
    contact_id="contact_id"
)

Managing Domains

List Domains

data, error = client.domains.list()

if error:
    print(f"Error: {error}")
else:
    for domain in data:
        print(f"Domain: {domain['domain']}, Status: {domain['status']}")

Create Domain

data, error = client.domains.create({
    "domain": "example.com"
})

Verify Domain

data, error = client.domains.verify(domain_id=123)

if error:
    print(f"Error: {error}")
else:
    print(f"Verification status: {data['status']}")

Get Domain

data, error = client.domains.get(domain_id=123)

Error Handling

By default, the SDK raises exceptions on HTTP errors:

from unsent import unsent, unsentHTTPError

client = unsent("us_12345")

try:
    data, error = client.emails.send({
        "to": "invalid-email",
        "from": "hello@company.com",
        "subject": "Test",
        "html": "<p>Test</p>",
    })
except unsentHTTPError as e:
    print(f"HTTP {e.status_code}: {e.error['message']}")

To disable automatic error raising:

client = unsent("us_12345", raise_on_error=False)

data, error = client.emails.send({
    "to": "hello@acme.com",
    "from": "hello@company.com",
    "subject": "Test",
    "html": "<p>Test</p>",
})

if error:
    print(f"Error: {error['message']}")
else:
    print("Success!")

Custom Session

For advanced use cases, you can provide your own requests.Session:

import requests
from unsent import unsent

session = requests.Session()
session.verify = False  # Not recommended for production!

client = unsent("us_12345", session=session)

API Reference

Client Methods

  • unsent(key, url, raise_on_error=True, session=None) - Initialize the client

Email Methods

  • client.emails.send(payload) - Send an email (alias for create)
  • client.emails.create(payload) - Create and send an email
  • client.emails.batch(emails) - Send multiple emails in batch
  • client.emails.get(email_id) - Get email details
  • client.emails.update(email_id, payload) - Update a scheduled email
  • client.emails.cancel(email_id) - Cancel a scheduled email

Contact Methods

  • client.contacts.create(book_id, payload) - Create a contact
  • client.contacts.get(book_id, contact_id) - Get contact details
  • client.contacts.update(book_id, contact_id, payload) - Update a contact
  • client.contacts.upsert(book_id, contact_id, payload) - Upsert a contact
  • client.contacts.delete(book_id, contact_id) - Delete a contact

Domain Methods

  • client.domains.list() - List all domains
  • client.domains.create(payload) - Create a domain
  • client.domains.verify(domain_id) - Verify a domain
  • client.domains.get(domain_id) - Get domain details

Requirements

  • Python 3.8+
  • requests >= 2.32.0
  • typing_extensions >= 4.7

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unsent-0.25.3.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unsent-0.25.3-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file unsent-0.25.3.tar.gz.

File metadata

  • Download URL: unsent-0.25.3.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.7 Darwin/24.3.0

File hashes

Hashes for unsent-0.25.3.tar.gz
Algorithm Hash digest
SHA256 e8179c62167b56044d453ffaff99de9bf1851090bf4517c79455ae3e91193d6f
MD5 e0a3c72af06911a60a6d5f8979aa33cb
BLAKE2b-256 db46cb3481cc3ab1ba77d348ac3dfbeedf553e0ea1e36f72aa3ed51e5705c3a3

See more details on using hashes here.

File details

Details for the file unsent-0.25.3-py3-none-any.whl.

File metadata

  • Download URL: unsent-0.25.3-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.7 Darwin/24.3.0

File hashes

Hashes for unsent-0.25.3-py3-none-any.whl
Algorithm Hash digest
SHA256 db0416a1abfa9de7d45259261cb75285dccf6c30c686e255872ca396cdf6b0eb
MD5 bd7cf19f07c94a791f278001656270e7
BLAKE2b-256 c7cd949a7fe81217188b36588eb171e2e54a1499b2e7c30e825427c0074b2bf2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page