Metadata-Version: 2.5
Name: segmentflow
Version: 0.1.0
Summary: Send email from your server with Segmentflow
Project-URL: Homepage, https://docs.segmentflow.ai
Project-URL: Repository, https://github.com/segmentflow/segmentflow-python
Project-URL: Issues, https://github.com/segmentflow/segmentflow-python/issues
Author: Segmentflow
License-Expression: MIT
License-File: LICENSE
Keywords: email,sdk,segmentflow,transactional email
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Communications :: Email
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: httpx>=0.21.2
Requires-Dist: pydantic>=1.9.2
Requires-Dist: typing-extensions>=4.0.0
Description-Content-Type: text/markdown

# segmentflow

Send email from your server with [Segmentflow](https://segmentflow.ai).

```bash
pip install segmentflow
```

Needs Python 3.10 or later. Use it on your server only: an API Key can send
email as your Organization, so it must never reach a browser or a mobile app.

## Set up

1. In the Segmentflow dashboard, verify a Sending Domain and add a sender
   (Integrations → Email).
2. Create an API Key (Settings → API Keys → Create API Key) with purpose
   **Unified API** and scopes **Send emails**, and store it as
   `SEGMENTFLOW_API_KEY`. Only Organization owners and admins can create keys.

```python
from segmentflow import SegmentflowClient

segmentflow = SegmentflowClient()  # reads SEGMENTFLOW_API_KEY
```

## Send with a saved Template

Design the email in the dashboard, then send it by its id: the last part of
its editor address, `dashboard.segmentflow.ai/templates/email/<id>`. A Template
created with a stable key can use `template_key` instead. Profile details such
as first name fill in from the recipient's Profile; values for this one send go
in `data`, which the Template reads as `/data/<name>`.

```python
sent = segmentflow.emails.send(
    template_id="018f7a35-2c2f-7e13-9b10-35f6d96d7a70",
    to={"email": "ada@example.com"},
    data={"order_number": "1042", "total": "49.00"},
    idempotency_key="order-1042-receipt",
)
```

## Send your own HTML

Render the email however you like (Jinja, Django templates, MJML) and send the
result:

```python
segmentflow.emails.send_html(
    html=render_to_string("emails/receipt.html", {"order": order}),
    text=f"Thanks! Order {order.number} is on its way.",  # optional
    subject=f"Your order {order.number}",
    purpose="transactional",
    from_="Acme <orders@acme.com>",
    to={"email": "ada@example.com"},
)
```

`purpose` is `"transactional"` for receipts, password resets and other email a
person expects. `"marketing"` and `"newsletter"` need a `subscription_group_id`,
and Segmentflow adds the unsubscribe footer for you. `from_` must be a sender
on a verified Sending Domain.

## Async

```python
from segmentflow import AsyncSegmentflowClient

segmentflow = AsyncSegmentflowClient()

async def send_receipt(email: str) -> None:
    await segmentflow.emails.send(template_id="...", to={"email": email})
```

## Check what happened

`send` returns straight away with status `Queued`. Read the status later with
the id:

```python
email = segmentflow.emails.get(sent.id)
# email.status: Queued | Processing | Sent | Failed | Skipped | DeliveryUnknown
# email.skipped_reason, for example ProfileSuppressed or ProfileUnsubscribed
```

## Never send twice

Every `send` carries an idempotency key. If you pass none, the SDK creates one,
so its own retries (two by default) never send a second email. Pass your own
key, such as `"order-1042-receipt"`, to stay safe when your job restarts too:
Segmentflow returns the first send instead of sending again. Reusing a key with
different content fails with a `ConflictError`.

## Many emails at once

`emails.batch_send` takes up to 100 independent sends. Each item has its own
recipient, `data` and `idempotency_key`.

```python
segmentflow.emails.batch_send(
    defaults={"template_id": "..."},
    items=[
        {
            "idempotency_key": f"digest-2026-48-{user.id}",
            "to": {"email": user.email},
            "data": {"unread": user.unread_count},
        }
        for user in users
    ],
)
```

## Errors

Failed requests raise a typed error, such as `BadRequestError` or
`TooManyRequestsError`, each with `status_code` and `body`.

```python
from segmentflow import TooManyRequestsError

try:
    segmentflow.emails.send(template_id="...", to={"email": email})
except TooManyRequestsError:
    ...  # The daily sending limit or warming capacity is used up; try later.
```

## Options

```python
SegmentflowClient(
    api_key="...",  # defaults to SEGMENTFLOW_API_KEY
    timeout=30,
    max_retries=2,
)
```

Full API reference: <https://docs.segmentflow.ai>. The TypeScript SDK is
[`@segmentflow/node`](https://www.npmjs.com/package/@segmentflow/node).
