Skip to content

Jupyter Notebooks

Greg Svoboda edited this page Jun 8, 2026 · 1 revision

Using in Jupyter Notebooks

Jupyter already runs its own event loop, so do not use asyncio.run() in a notebook cell — it will raise RuntimeError: This event loop is already running. Instead, use await directly in a cell:

import postmark

async with postmark.ServerClient("your-server-token") as client:
    response = await client.outbound.send({
        "sender": "sender@example.com",
        "to": "recipient@example.com",
        "subject": "Hello from Postmark!",
        "text_body": "This is my first email via postmark-python.",
    })
    print(f"Message ID: {response.message_id}")

This works because IPython (since version 7) runs its own event loop, making await valid at the top level of any cell.

Prefer a synchronous API?

postmark.sync also works in notebooks — it runs on a separate background thread, so it doesn't conflict with Jupyter's event loop:

import postmark

with postmark.sync.ServerClient("your-server-token") as client:
    response = client.outbound.send({
        "sender": "sender@example.com",
        "to": "recipient@example.com",
        "subject": "Hello from Postmark!",
        "text_body": "This is my first email via postmark-python.",
    })
    print(f"Message ID: {response.message_id}")

See the Sync Client page for full documentation.

Clone this wiki locally