Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Courier: Your Complete Communication Stack

pypi fern shield

This is the official Python package for sending notifications with the Courier REST API.

Installation

Install from PyPI

pip install trycourier
# or 
poetry add trycourier

Python 3.7 or later is required.

Usage

Use the Courier class to access all of our endpoints.

import os
import courier

from courier.client import Courier

client = Courier(
  authorization_token="YOUR_TOKENY" # Defaults to COURIER_AUTH_TOKEN
)

response = client.send(
  message=courier.ContentMessage(
    to=courier.UserRecipient(
      email="marty_mcfly@email.com",
      data={
        name: "Marty",
      }
    ),
    content=courier.ElementalContentSugar(
      title="Back to the Future",
      body="Oh my {{name}}, we need 1.21 Gigawatts!",
    ),
    routing=courier.Routing(
      method=courier.RoutingMethod.ALL,
      channels=["email"]
    )
  )
)

print(response)

Async Client

The SDK also exports an asynchronous client, AsyncCourier.

import os
import courier
import asyncio

from courier.client import AsyncCourier

client = AsyncCourier(
  authorization_token="YOUR_TOKEN" # Defaults to COURIER_AUTH_TOKEN
)

async def main() -> None: 
  response = await client.send(
    message=courier.ContentMessage(
      to=courier.UserRecipient(
        email="marty_mcfly@email.com",
        data={
          name: "Marty",
        }
      ),
      content=courier.ElementalContentSugar(
        title="Back to the Future",
        body="Oh my {{name}}, we need 1.21 Gigawatts!",
      ),
      routing=courier.Routing(
        method=courier.RoutingMethod.ALL,
        channels=["email"]
      )
    )
  )

asyncio.run(main())

Timeouts

By default, the client is configured to have a timeout of 60 seconds. You can customize this value at client instantiation.

from courier.client import Courier

client = Courier(
  authorization_token="YOUR_TOKEN",
  timeout=30
)

Handling Exceptions

All exceptions thrown by the SDK will sublcass courier.ApiError.

import courier

from courier.core import ApiError

try:
  courier.send(...)
except APIError as e:  
  # handle any api related error

Idempotency Keys

For POST methods, you can supply an idempotencyKey in the config parameter to ensure the idempotency of the API Call. We recommend that you use a V4 UUID for the key. Keys are eligible to be removed from the system after they're at least 24 hours old, and a new request is generated if a key is reused after the original has been removed. For more info, see Idempotent Requests in the Courier documentation.

import courier

courier.send(
  message={...}, 
  idempotency_key"YOUR_KEY", 
  idempotency_expiration="YOUR_EXPIRATION")

Additional Usage Examples

Send Template Message

import courier

from courier import Courier

client = Courier(
  authorization_token="YOUR_TOKEN")

response = client.send(
    message=courier.TemplateMessage(
      template="my-template",
      to=courier.UserRecipient(email="foo@bar.com")
    )
)
print(response.message_id)

UTM Metadata with Message

import courier

from courier import Courier

client = Courier(
  authorization_token="YOUR_TOKEN")

response = client.send(
    message=courier.ContentMessage(
      content=courier.ElementalContent(
        version='2020-01-01',
        elements=[
          courier.ElementalNode_Action(
            content="ELEMENTAL",
            href="courier.com",
            style="button",
            align="center"
          )
        ]
      ),
      to=courier.UserRecipient(email="foo@bar.com"),
      routing=courier.Routing(
        method=courier.RoutingMethod.SINGLE,
        channels=["email"]
      ),
      metadata=courier.MessageMetadata(
        utm=courier.Utm(source="python")
      )
    )
)
print(response.request_id)

Advanced

Overriding HTTP Client

You can override the httpx client to customize it for your use case. Some common usecases include support for proxies and transports.

import httpx
from courier.client import Courier

client = Courier(
    http_client=httpx.Client(
        proxies="http://my.test.proxy.example.com",
        transport=httpx.HTTPTransport(local_address="0.0.0.0"),
    ),
)

Contributing

While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!

License

MIT License

Author

Courier (support@courier.com)

Metadata

Release files for trycourier 6.0.0b1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for trycourier 6.0.0b1
File Size Uploaded
trycourier-6.0.0b1.tar.gz 69.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for trycourier 6.0.0b1
File Interpreter ABI Platform
trycourier-6.0.0b1-py3-none-any.whl Python 3 none any Details

Total release size: 308.6 kB

Release files / trycourier-6.0.0b1.tar.gz

Download URL trycourier-6.0.0b1.tar.gz
Size 69.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a7963e059e0ce6d7505e917070eae78b829b743bcb394bdd0e02e981def92691
BLAKE2b-256 checksum
How to use checksums
035f97adfd0dbc0c6c24273657f2968f0312e85933811489ada182bf317d9bd2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.7.17 Linux/5.15.0-1053-azure

Release files / trycourier-6.0.0b1-py3-none-any.whl

Download URL trycourier-6.0.0b1-py3-none-any.whl
Size 238.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
383d5c7968456cc54f08b3d56cb3a178cef3c5a0788be04a9d752dd35d579a54
BLAKE2b-256 checksum
How to use checksums
0a5705281a376da97f22e6fd0a972fcac24052056d5d10ef67520ad43eabbb69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.7.17 Linux/5.15.0-1053-azure

Release history Release notifications | RSS feed

9.11.0

2 release files

9.10.0

2 release files

9.9.0

2 release files

9.8.0

2 release files

9.7.0

2 release files

9.6.0

2 release files

9.5.0

2 release files

9.4.4

2 release files

9.4.3

2 release files

9.4.2

2 release files

9.4.1

2 release files

9.4.0

2 release files

9.3.0

2 release files

9.2.0

2 release files

9.1.0

2 release files

9.0.0

2 release files

8.0.0

2 release files

7.26.2

2 release files

7.26.1

2 release files

7.26.0

2 release files

7.25.0

2 release files

7.23.0

2 release files

7.22.0

2 release files

7.21.0

2 release files

7.20.0

2 release files

7.19.2

2 release files

7.17.1

2 release files

7.17.0

2 release files

7.16.0

2 release files

7.15.0

2 release files

7.14.0

2 release files

7.13.0

2 release files

7.12.0

2 release files

7.11.2

2 release files

7.11.1

2 release files

7.9.0

2 release files

7.8.0

2 release files

7.7.1

2 release files

7.7.0

2 release files

7.6.1

2 release files

7.6.0

2 release files

7.5.0

2 release files

7.4.0

2 release files

7.3.0

2 release files

7.2.0

2 release files

7.1.1

2 release files

7.1.0

2 release files

7.0.1

2 release files

7.0.0

2 release files

6.2.1

2 release files

6.2.0

2 release files

6.1.0

2 release files

6.0.5

2 release files

6.0.4

2 release files

6.0.3

2 release files

6.0.2

2 release files

6.0.1

2 release files

6.0.0

2 release files

This release

6.0.0b1 This release

2 release files

5.0.0

2 release files

4.7.0

2 release files

4.6.0

2 release files

4.5.0

2 release files

4.4.0

2 release files

4.2.0

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.0.0

2 release files

2.0.0

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page